1398. Customers Who Bought Products A and B but Not C

https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c

Description

Table: Customers

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| customer\_id         | int     |
| customer\_name       | varchar |
+---------------------+---------+
customer\_id is the primary key for this table.
customer\_name is the name of the customer.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order\_id      | int     |
| customer\_id   | int     |
| product\_name  | varchar |
+---------------+---------+
order\_id is the primary key for this table.
customer\_id is the id of the customer who bought the product "product\_name".

Write an SQL query to report the customer_id and customer_name of customers who bought products "A", "B" but did not buy the product "C" since we want to recommend them buy this product.

Return the result table ordered by customer_id.

The query result format is in the following example.

Customers table:
+-------------+---------------+
| customer\_id | customer\_name |
+-------------+---------------+
| 1           | Daniel        |
| 2           | Diana         |
| 3           | Elizabeth     |
| 4           | Jhon          |
+-------------+---------------+
Orders table:
+------------+--------------+---------------+
| 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         |
+------------+--------------+---------------+
Result table:
+-------------+---------------+
| customer\_id | customer\_name |
+-------------+---------------+
| 3           | Elizabeth     |
+-------------+---------------+
Only the customer\_id with id 3 bought the product A and B but not the product C.

ac

Last updated