본문 바로가기
Tests/Leetcode

[leetcode] MySQL > 183. Customers Who Never Order

by 코딩하는 금융인 2020. 9. 12.

문제. leetcode Database 183. Customers Who Never Order


SQL Schema & Explanation

Suppose that a website contains two tables, the Customers table and the Orders table.
Write a SQL query to find all customers who never order anything.

Table: Customers.

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

출처 : leetcode Database 183. Customers Who Never Order

 

Customers Who Never Order - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


나의 풀이

# Write your MySQL query statement below
SELECT Name Customers FROM Customers c
WHERE c.Id NOT IN (SELECT CustomerId FROM ORDERS);

Orders 테이블에 customerId가 들어 있지 않은 경우가 주문하지 않은 고객이므로 Where Not In 구문을 이용하여 풀었습니다.

반응형

댓글