Complete Category Buyers
Beginner Mode
Objective
Write a SQL query to identify the customer IDs of those who have purchased at least one product from every product category.
Table Schema:
- customer_purchases
| Column | Type | Description |
|---|---|---|
| purchase_id | INTEGER | Unique identifier for each purchase |
| customer_id | INTEGER | ID of the customer |
| product_id | INTEGER | ID of the purchased product |
- product_catalog
| Column | Type | Description |
|---|---|---|
| product_id | INTEGER | Unique identifier for each product |
| product_name | VARCHAR | Name of the product |
| category | VARCHAR | Product category |
Task Requirements:
- Join purchase data with the product catalog to determine which categories each customer has bought from
- Identify customers who have covered every distinct category in the catalog
- A customer may have multiple purchases in the same category - this still counts as one category
- Sort the result by
customer_idin ascending order
Output columns: customer_id
Examples
Example 1:
Output:
Input:
| customer_purchases | ||
|---|---|---|
| customer_id | product_id | purchase_id |
| 1 | 1 | 1 |
| 1 | 3 | 2 |
| 1 | 5 | 3 |
| 2 | 1 | 4 |
| 2 | 2 | 5 |
| 3 | 2 | 6 |
| 3 | 4 | 7 |
| 3 | 5 | 8 |
| product_catalog | ||
|---|---|---|
| category | product_id | product_name |
| Electronics | 1 | Laptop |
| Electronics | 2 | Phone |
| Clothing | 3 | Shirt |
| Clothing | 4 | Pants |
| Kitchen | 5 | Blender |
| customer_id |
|---|
| 1 |
| 3 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Amazon