You have two tables: Customers and Orders.
The Customers table contains customer information with unique customer IDs and names. The Orders table records each product purchase by customers.
Write a SQL solution to find customers who have bought both products "A" and "B" but have never bought product "C". These customers are good candidates for product C recommendations.
Return the result table ordered by customer_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
customer_id
PK
|
int | Unique customer identifier |
customer_name
|
varchar | Name of the customer |
| Column Name | Type | Description |
|---|---|---|
order_id
PK
|
int | Unique order identifier |
customer_id
|
int | Foreign key referencing Customers table |
product_name
|
varchar | Name of the product purchased |
Input & Output
| customer_id | customer_name |
|---|---|
| 1 | Daniel |
| 2 | Diana |
| 3 | Elizabeth |
| 4 | Jhon |
| order_id | customer_id | product_name |
|---|---|---|
| 10 | 1 | A |
| 20 | 1 | B |
| 30 | 1 | D |
| 40 | 1 | C |
| 50 | 2 | A |
| 60 | 3 | A |
| 70 | 3 | B |
| 80 | 3 | D |
| 90 | 4 | C |
| customer_id | customer_name |
|---|---|
| 3 | Elizabeth |
Customer 1 (Daniel) bought A, B, and C, so excluded because they bought C. Customer 2 (Diana) only bought A, missing B. Customer 3 (Elizabeth) bought both A and B but not C, so included. Customer 4 (Jhon) only bought C, missing A and B.
| customer_id | customer_name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
| order_id | customer_id | product_name |
|---|---|---|
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 1 | A |
| 4 | 2 | A |
| 5 | 2 | B |
| 6 | 2 | D |
| 7 | 3 | C |
| customer_id | customer_name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Alice bought products A and B (multiple times for A) but never bought C, so she's included. Bob bought A, B, and D but not C, so he's included. Charlie only bought C, missing both A and B, so excluded.
| customer_id | customer_name |
|---|---|
| 1 | Mark |
| 2 | Sarah |
| order_id | customer_id | product_name |
|---|---|---|
| 1 | 1 | A |
| 2 | 1 | C |
| 3 | 2 | B |
| 4 | 2 | D |
| customer_id | customer_name |
|---|
Mark bought A and C but not B (missing B). Sarah bought B and D but not A (missing A). Neither customer bought both A and B while avoiding C, so the result is empty.
Constraints
-
1 ≤ customer_id ≤ 1000 -
1 ≤ order_id ≤ 10000 -
product_nameconsists of only uppercase English letters - All customer names are unique