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:

  1. โœ… Have bought product "A"
  2. โœ… Have bought product "B"
  3. โŒ 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
Customer 1DanielOrders: A, B, DCustomer 2DianaOrders: A, B, CCustomer 3ElizabethOrders: A, CAnalysis EngineGroup by customer_idCount A purchasesCount B purchasesCount C purchasesApply filtersโœ“ DanielA:1, B:1, C:0โœ— DianaA:1, B:1, C:1โœ— ElizabethA:1, B:0, C:1RecommendationTarget ListDaniel โ†’ Product C
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for grouping by unique customers (k = number of customers)

n
2n
โœ“ 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
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.0K Views
High Frequency
~15 min Avg. Time
1.5K 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