SQL interview question: customers who bought every category
Problem
Shared data rulesFind buyers who bought from every catalog category in September 2026, and count their September orders.
Example
products| product_id | category | Row note |
|---|---|---|
| 11 | Produce | |
| 12 | Dairy | |
| 13 | Dairy | ← same category as 12 |
- Product 13: same category as 12
orders| order_id | user_id | created_at | status |
|---|---|---|---|
| 101 | 1 | 2026-09-03 10:00 | delivered |
| 102 | 1 | 2026-09-10 12:00 | delivered |
| 103 | 2 | 2026-09-12 13:00 | delivered |
order_items| order_id | category | Row note |
|---|---|---|
| 101 | Produce | |
| 102 | Dairy | |
| 102 | Dairy | ← second item, same category |
| 103 | Produce | |
| 103 | Produce | ← second item, same category |
- Row 3: second item, same category
- Row 5: second item, same category
| user_id | delivered_orders |
|---|---|
| 1 | 2 |
The catalog has two categories. Buyer 1 covered both over two orders; buyer 2 bought only produce.
Notes
- Catalog categories are all distinct products.category values, even for products no longer on sale.
Solve it free
Your query runs on PostgreSQL in your browser. No sign-up needed.