Customers Who Bought Products A and B but Not C - Problem
๐๏ธ Customer Product Recommendation Query
You're working for an e-commerce company that wants to implement a smart product recommendation system. Your task is to identify customers who have purchased both products "A" and "B" but have never bought product "C".
Given two tables:
- Customers: Contains customer information (ID and name)
- Orders: Contains order history (which customer bought which product)
Write a SQL query to find customers who:
- โ
Have bought product
"A" - โ
Have bought product
"B" - โ Have NOT bought product
"C"
Return the customer_id and customer_name ordered by customer_id in ascending order.
Business Context: These customers are perfect targets for recommending product "C" since they've shown interest in related products but haven't made that purchase yet!
Input & Output
example_1.sql โ Basic Case
$
Input:
Customers:
| customer_id | customer_name |
|-------------|---------------|
| 1 | Daniel |
| 2 | Diana |
| 3 | Elizabeth |
| 4 | Jhon |
Orders:
| order_id | customer_id | product_name |
|----------|-------------|---------------|
| 10 | 1 | A |
| 11 | 1 | B |
| 12 | 1 | D |
| 13 | 2 | A |
| 14 | 2 | B |
| 15 | 2 | C |
| 16 | 3 | A |
| 17 | 3 | C |
| 18 | 4 | B |
| 19 | 4 | C |
โบ
Output:
| customer_id | customer_name |
|-------------|---------------|
| 1 | Daniel |
๐ก Note:
Only Daniel (customer_id = 1) bought both products A and B but never bought product C. Diana bought A, B, and C (excluded). Elizabeth bought A and C but not B (excluded). Jhon bought B and C but not A (excluded).
example_2.sql โ Multiple Valid Customers
$
Input:
Customers:
| customer_id | customer_name |
|-------------|---------------|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
Orders:
| order_id | customer_id | product_name |
|----------|-------------|---------------|
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 2 | A |
| 4 | 2 | B |
| 5 | 3 | A |
| 6 | 3 | C |
โบ
Output:
| customer_id | customer_name |
|-------------|---------------|
| 1 | Alice |
| 2 | Bob |
๐ก Note:
Both Alice and Bob bought products A and B without ever buying C. Charlie bought A and C but not B, so he's excluded from the results.
example_3.sql โ No Valid Customers
$
Input:
Customers:
| customer_id | customer_name |
|-------------|---------------|
| 1 | John |
| 2 | Mary |
Orders:
| order_id | customer_id | product_name |
|----------|-------------|---------------|
| 1 | 1 | A |
| 2 | 1 | C |
| 3 | 2 | B |
| 4 | 2 | C |
โบ
Output:
| customer_id | customer_name |
|-------------|---------------||
(empty result)
๐ก Note:
No customers satisfy all conditions. John bought A and C but not B. Mary bought B and C but not A. Since both bought C, they're excluded from recommendations.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Purchase History
Gather all customer purchase records from the orders database
2
Group by Customer
Organize all purchases by individual customer to see their complete buying pattern
3
Count Product Types
For each customer, count how many times they bought products A, B, and C
4
Apply Filter Rules
Select customers where A_count > 0, B_count > 0, and C_count = 0
Key Takeaway
๐ฏ Key Insight: Using GROUP BY with conditional aggregation (SUM + CASE) allows us to process all product conditions in one efficient database scan, making it perfect for large-scale recommendation systems.
Time & Space Complexity
Time Complexity
O(n)
Single pass through Orders table with grouping
โ Linear Growth
Space Complexity
O(k)
Space for grouping by unique customers (k = number of customers)
โ Linear Space
Constraints
- 1 โค customer_id โค 105
- 1 โค order_id โค 106
- product_name can be any string, not limited to 'A', 'B', 'C'
- A customer can have multiple orders for the same product
- All customer_ids in Orders table exist in Customers table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code