Post

[LeetCode 183] Customers Who Never Order

LeetCode 183 (SQL)
[Customers Who Never Order] 문제 풀이

[LeetCode 183] Customers Who Never Order

문제 바로가기


Description


Table: Customers

1
2
3
4
5
6
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+

id is the primary key (column with unique values) for this table. Each row of this table indicates the ID and name of a customer.

Table: Orders

1
2
3
4
5
6
+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| customerId  | int  |
+-------------+------+

id is the primary key (column with unique values) for this table. customerId is a foreign key (reference columns) of the ID from the Customers table. Each row of this table indicates the ID of an order and the ID of the customer who ordered it.

Write a solution to find all customers who never order anything.

Return the result table in any order.

The result format is in the following example.


Example 1


  • Input:
    • Customers table:
      1
      2
      3
      4
      5
      6
      7
      8
      
      +----+-------+
      | id | name  |
      +----+-------+
      | 1  | Joe   |
      | 2  | Henry |
      | 3  | Sam   |
      | 4  | Max   |
      +----+-------+
      
    • Orders table:
      1
      2
      3
      4
      5
      6
      
      +----+------------+
      | id | customerId |
      +----+------------+
      | 1  | 3          |
      | 2  | 1          |
      +----+------------+
      
  • Output:
    1
    2
    3
    4
    5
    6
    
    +-----------+
    | Customers |
    +-----------+
    | Henry     |
    | Max       |
    +-----------+
    


Constraints








Code


내 제출


1
2
3
4
5
# Write your MySQL query statement below

SELECT name AS Customers 
FROM Customers 
WHERE id NOT IN(SELECT customerId FROM Orders);


RuntimeMemory
530 ms0.0 B


다른 풀이


1
2
3
4
5
6
# Write your MySQL query statement below

SELECT c.name as Customers
FROM Customers c
LEFT JOIN Orders o ON c.id = o.customerId
WHERE o.customerId IS NULL;


Reference


This post is licensed under CC BY 4.0 by the author.