Ingredients Frequently Used Together
Problem
Table: `recipe_uses`
```text
+-------------+------+
| Column Name | Type |
+-------------+------+
| cook_id | int |
| item_id | int |
| grams | int |
+-------------+------+
There is no single-column primary key. Each row means a cook used a given
pantry item in one of their recipes.
```
Table: `pantry_items`
```text
+-------------+---------------+
| Column Name | Type |
+-------------+---------------+
| item_id | int |
| aisle | varchar |
| unit_cost | decimal(10,2) |
+-------------+---------------+
item_id is the primary key for this table.
```
Find every pair of pantry items that were used together by at least three different cooks. Treat each pair only once, with the smaller `item_id` first. Return the two item ids, the aisle of each item, and the number of distinct cooks who used both.
Return `item1_id`, `item2_id`, `item1_aisle`, `item2_aisle`, and `cook_count`, ordered by `cook_count` descending, then `item1_id` ascending, then `item2_id` ascending.
Tables
Example rows — the live problem includes the full dataset.
| cook_id | item_id | grams |
|---|---|---|
| 1 | 101 | 200 |
| 1 | 102 | 100 |
| 1 | 103 | 300 |
| item_id | aisle | unit_cost |
|---|---|---|
| 101 | Produce | 1 |
| 102 | Baking | 0.2 |
| 103 | Dairy | 0.35 |
Expected output
Your answer should return 3 rows with the columns item1_id, item2_id, item1_aisle, item2_aisle, cook_count.
Starter code (SQL)
SELECT *
FROM recipe_uses;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock