Products bought together
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
Return every pair of products that appear together on the same order: the first product name, the second product name and the number of orders that contain both. List each pair once, with the lower product id first.
order_items (first 12 of 18 rows)
| order_id | product_id | qty |
|---|---|---|
| 101 | 3 | 4 |
| 101 | 4 | 1 |
| 102 | 7 | 2 |
| 103 | 2 | 1 |
| 104 | 6 | 1 |
| 105 | 5 | 2 |
| 105 | 4 | 3 |
| 106 | 1 | 2 |
| 107 | 3 | 6 |
| 108 | 5 | 1 |
| 109 | 7 | 1 |
| 109 | 6 | 2 |
products (8 rows)
| id | name | category | price |
|---|---|---|---|
| 1 | Desk Lamp | Home | 35 |
| 2 | Office Chair | Home | 180 |
| 3 | Notebook | Stationery | 4.5 |
| 4 | Pen Set | Stationery | 12 |
| 5 | Headphones | Electronics | 95 |
| 6 | Webcam | Electronics | 60 |
| 7 | Mug | Home | 9 |
| 8 | Backpack | Travel | 70 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
The a.product_id < b.product_id condition removes self-pairs and keeps each pair in one direction. This is the first step of a market basket analysis, and the same self-join pattern as customers from the same country.
A hint if you get stuck
Self join order_items on order_id with a.product_id < b.product_id, then join products twice for the names and group by the pair.
Background you need
Common mistakes on this exercise
- COUNT(*) counts order lines: a product listed twice on one order is counted twice.
- Each pair listed in both directions.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Interview classics
- Third highest salary
- Second highest salary per department
- Earns more than the manager
- Departments above average
- Salary against the department average
- Pivot the statuses
- Repeat purchase rate
- Median salary
- Orders in consecutive months
- Runs of the same status
- Cohort retention
- First order not paid
- A month with no sales still shows up (generate_series)
- Daily active users
- Monthly active users
- Funnel: users at each step
- Signup to purchase conversion
- New and returning users per day
- Day-1 retention
- Retention curve
- Sessions per user (30-minute rule)
- Rolling 7-day active users
- Longest streak of active days
- Monthly churn rate
- Reactivated subscribers
- Merge bookings into occupied periods
- Peak number of bookings at once