Algorithm/MySQL
[LeetCode] 183. Customers Who Never Order (MySQL)
yujin.me
2021. 7. 5. 14:45
문제
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.
코드
SELECT Customers.Name as Customers
FROM Customers
LEFT JOIN Orders
ON Customers.Id = Orders.CustomerId
WHERE orders.Id is null
- 아무것도 주문하지 않은 모든 고객을 찾아라
- 그렇다면 Customer테이블에는 데이터가 있지만 Orders테이블에는 데이터가 없을 것이다
- 그렇기 때문에 데이터가 있는 Customers를 기준으로 LEFT JOIN을 해준다
- Customer 테이블의 Id와 Orders 테이블의 CustomerId가 같으므로 둘을 조건으로 걸어준다
- Order테이블에서 Id가 null인 것을 찾아준다 > column in null
출처
반응형