You have two tables: Customer and Product.
The Customer table contains information about customers and the products they bought. This table may have duplicate rows, and customer_id is never NULL. The product_key is a foreign key referencing the Product table.
The Product table contains all available products, where product_key is the primary key.
Write a SQL query to find all customers who bought ALL products available in the Product table.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
customer_id
|
int | ID of the customer (not NULL) |
product_key
|
int | Foreign key referencing Product table |
| Column Name | Type | Description |
|---|---|---|
product_key
PK
|
int | Primary key for products |
Input & Output
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 2 | 6 |
| 1 | 6 |
| 1 | 3 |
| product_key |
|---|
| 5 |
| 6 |
| 3 |
| customer_id |
|---|
| 1 |
Customer 1 bought products 5, 6, and 3 (all available products). Customer 2 only bought product 6. Therefore, only customer 1 qualifies.
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| product_key |
|---|
| 5 |
| 6 |
| 3 |
| customer_id |
|---|
No customer bought all three products (5, 6, and 3). Each customer only purchased one product, so the result is empty.
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 1 | 6 |
| 2 | 5 |
| 2 | 6 |
| product_key |
|---|
| 5 |
| 6 |
| customer_id |
|---|
| 1 |
| 2 |
Both customers 1 and 2 purchased all available products (5 and 6), so both are included in the result.
Constraints
-
customer_idandproduct_keyare integers -
customer_idis not NULL -
product_keyis a foreign key to Product table - Customer table may contain duplicate rows