Customers Who Bought All Products - Problem
Find the Complete Customers
You're managing an e-commerce database with two key tables:
The
The
Your task: Write a SQL query to return the
You're managing an e-commerce database with two key tables:
Customer and Product. Your goal is to identify the loyal customers who have purchased every single product available in your catalog.The
Customer table records each purchase transaction with columns customer_id and product_key (may contain duplicates if a customer bought the same product multiple times).The
Product table contains all available products with their unique product_key.Your task: Write a SQL query to return the
customer_id of customers who have bought all products from the Product table. Think of it as finding your "premium" customers who've experienced your entire product range! Input & Output
example_1.sql โ Basic Case
$
Input:
Customer table:\n| customer_id | product_key |\n|-------------|-------------|\n| 1 | 5 |\n| 2 | 6 |\n| 3 | 5 |\n| 3 | 6 |\n| 1 | 6 |\n\nProduct table:\n| product_key |\n|-------------|\n| 5 |\n| 6 |
โบ
Output:
| customer_id |\n|-------------|\n| 1 |\n| 3 |
๐ก Note:
Customer 1 bought products 5 and 6 (all products). Customer 3 also bought products 5 and 6 (all products). Customer 2 only bought product 6, missing product 5.
example_2.sql โ Single Product
$
Input:
Customer table:\n| customer_id | product_key |\n|-------------|-------------|\n| 1 | 5 |\n| 2 | 5 |\n| 3 | 5 |\n\nProduct table:\n| product_key |\n|-------------|\n| 5 |
โบ
Output:
| customer_id |\n|-------------|\n| 1 |\n| 2 |\n| 3 |
๐ก Note:
There's only one product (5) in the catalog, and all customers have purchased it, so all customers qualify.
example_3.sql โ No Complete Customers
$
Input:
Customer table:\n| customer_id | product_key |\n|-------------|-------------|\n| 1 | 5 |\n| 2 | 6 |\n| 3 | 7 |\n\nProduct table:\n| product_key |\n|-------------|\n| 5 |\n| 6 |\n| 7 |
โบ
Output:
| customer_id |\n|-------------|\n(empty result)
๐ก Note:
Each customer only bought one product, but there are three products total. No customer bought all products, so the result is empty.
Constraints
- 1 โค customer_id โค 100
- 1 โค product_key โค 100
- Customer table may contain duplicate rows
- All product_key values in Customer table exist in Product table
- Product table contains unique product_key values
Visualization
Tap to expand
Understanding the Visualization
1
Count Total Cards
First, determine how many cards are in the complete set
2
Group Collections
Group each customer's card purchases together
3
Count Unique Cards
For each customer, count how many different cards they own
4
Find Complete Sets
Identify customers whose unique card count equals the complete set size
Key Takeaway
๐ฏ Key Insight: Instead of checking each product individually, we count distinct products per customer and compare with the total - much more efficient!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code