Customers Who Bought All Products
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
You have two tables: Customer and Product. Each row in Customer records a purchase of a product by a customer. Write a SQL query to report the IDs of all customers who have bought all the products listed in the Product table. Return the result in any order.
Database Schema
Customer
| Column Name | Type | Description |
|---|---|---|
| customer_id | INT | Customer identifier (not unique) |
| product_key | INT | FK to Product table |
Product
| Column Name | Type | Description |
|---|---|---|
| product_key | INT | Primary key |
Example
Customer
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
Product
| product_key |
|---|
| 5 |
| 6 |
Output
| customer_id |
|---|
| 1 |
| 3 |
Explanation:
Customers 1 and 3 both purchased products 5 and 6 - all available products. Customer 2 only bought product 6.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
SQL Editor
Loading...
Customer
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
Product
| product_key |
|---|
| 5 |
| 6 |
Output
| customer_id |
|---|
| 1 |
| 3 |