1. 문제
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 |
+-----------+
2. 답
SELECT c.name AS Customers
FROM customers AS c
LEFT JOIN orders AS o ON c.id = o.customerid
WHERE o.id IS NULL
3. KEY POINT
- LEFT JOIN : 왼쪽 테이블 기준으로 합치기
- IS NULL : NULL값 인지 확인, True 일 경우 조건 해당됨
- AS : 별명 붙이기
leetcode.com/problems/customers-who-never-order/submissions/
'MySQL > 문제풀이' 카테고리의 다른 글
[LeetCode] Self JOIN : 197. Rising Temperature (0) | 2021.03.15 |
---|---|
[LeetCode] Self JOIN : 181. Employees Earning More Than Their Managers (0) | 2021.03.15 |
[HackerRank] INNER JOIN : Average Population of Each Continent (0) | 2021.03.15 |
[HackerRank] INNER JOIN : Asian Population (0) | 2021.03.15 |
[HackerRank] INNER JOIN : African Cities (0) | 2021.03.15 |
댓글