Data & BI/SQL Coding Test

[LeetCode](초급) 183. Customers Who Never Order

무료한하늘 2025. 5. 16. 12:49
728x90

문제

Column Name Type
id int
name varchar
[Table : Customers]
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.

 

 

Column Name Type
id int
customerId int
[Table : Orders]
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.

 

Customers 테이블에는 고객 명단이 있다. Orders 테이블에는 customerId를 통해 주문한 고객 정보가 있다.

모든 고객 중 주문을 하지 않은 고객들을 출력해라.

 

풀이

SELECT C.NAME AS Customers
FROM CUSTOMERS C
LEFT OUTER JOIN ORDERS O ON C.ID = O.CUSTOMERID
WHERE O.ID IS NULL

 

Customers 테이블의 고객 ID와 주문 테이블의 customerId를 조인하여 Customers 테이블 기준 ID 가 null 인 고객을 출력하였다. 주문을 하지 않았으면 null 이 반환되기 때문.

300x250