ABOUT ME

Today
Yesterday
Total
  • leetcode - 1398. Customers Who Bought Products A and B but Not C
    SQL 문제풀이 2022. 8. 10. 15:34

    난이도 : Medium

     

    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 to purchase this product.

    Return the result table ordered by customer_id.

    The query result format is in the following example.

     

    Example 1:

    Input: 
    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         |
    +------------+--------------+---------------+
    Output: 
    +-------------+---------------+
    | customer_id | customer_name |
    +-------------+---------------+
    | 3           | Elizabeth     |
    +-------------+---------------+
    Explanation: Only the customer_id with id 3 bought the product A and B but not the product C.

     

     

    SELECT Orders.customer_id
         , customer_name
    FROM Orders
         RIGHT JOIN Customers ON Orders.customer_id = Customers.customer_id
    GROUP BY 1
    HAVING SUM(product_name = 'A') >= 1
    AND SUM(product_name = 'B') >= 1
    AND SUM(product_name = 'C') = 0

    Accepted (43.41%)

     

     

    SELECT DISTINCT customer_id
         , customer_name
    FROM Customers
    WHERE customer_id IN (SELECT customer_id FROM Orders WHERE product_name = 'A')
    AND customer_id IN (SELECT customer_id FROM Orders WHERE product_name = 'B')
    AND customer_id NOT IN (SELECT customer_id FROM Orders WHERE product_name = 'C')

    Accepted (51.85%)

Designed by Tistory.