Customers Who Bought All Products - Problem
Find the Complete Customers

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
Customer 1ABCComplete Set โœ“3/3 cardsCustomer 2ABCIncomplete โœ—2/3 cardsSQL MagicGROUP BY customer_idCOUNT(DISTINCT product_key)HAVING count = total_productsProcessComplete Set RequirementsCard A: Required โœ“Card B: Required โœ“Card C: Required โœ“
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!
Asked in
Amazon 45 Google 32 Meta 28 Microsoft 25
48.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen