[LeetCode][MySQL] 183. Customers Who Never Order
Easy 🔗183. Customers Who Never Order
📝문제 요약
Table: Customers
+-------------+---------+
| 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
+-------------+------+
| 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:
+----+-------+
| id | name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
Output:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
✏️문제 풀이
SELECT
SELECT name AS Customers
Customersname: 아무것도 주문하지 않은 모든 고객의 이름
FROM
FROM Customers C LEFT JOIN Orders O ON C.id = O.customerId
Customers테이블과Orders테이블을LEFT JOIN으로 병합- 두 테이블의 외래키는
Customers테이블의id와Orders테이블의customerId
WHERE
WHERE O.id IS NULL
Orders의id가NULL인 데이터만 필터링- 두 테이블을 병합할때
LEFT JOIN을 사용하였으므로,Orders에 존재하지 않는 데이터인 경우NULL값이기 때문
- 두 테이블을 병합할때
💯제출 코드
SELECT name AS Customers
FROM Customers C LEFT JOIN Orders O ON C.id = O.customerId
WHERE O.id IS NULL
댓글남기기