[Leet Code] Customers Who Never Order

정보희·2022년 3월 25일
0

SQL 공부

목록 보기
5/8

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key column 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 for this table.
customerId is a foreign key 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 an SQL query to report all customers who never order anything.

Return the result table in any order.

The query result format is in the following example.

[틀린 쿼리]

SELECT Customers.name FROM Customers
left join Orders on Customers.id=Orders.id
where Orders.customerID=NULL

[정답 쿼리]


SELECT name as customers FROM Customers AS C
LEFT JOIN Orders AS O ON C.ID=O.CustomerID
where O.CustomerID is null 

포인트
쿼리 작성 시 null 값은 ~~is null 이라 해야함 ( a = null x)
두 테이블에서 조인으로 입력할 부분 확실하게 알기

profile
데이터 다루는 마케터가 되는 것이 꿈 입니다!

0개의 댓글

관련 채용 정보