AnalystPath

Ingredients Frequently Used Together

SQLMediumMid level~15 min

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.

recipe_uses
cook_iditem_idgrams
1101200
1102100
1103300
pantry_items
item_idaisleunit_cost
101Produce1
102Baking0.2
103Dairy0.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

Related SQL questions