Leetcode - 183. Customers Who Never Order (서브쿼리로 다시풀기)

르네·2023년 11월 13일
0

SQL

목록 보기
52/63

문제

Leetcode - 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.

풀이

나의 풀이

SELECT c.name AS Customers
FROM customers c
    LEFT JOIN orders o ON c.id = o.customerId
WHERE o.id IS NULL

다른 사람 풀이

SELECT name as Customers
from Customers
where id not in (
    select customerId
    from Orders

배운점

  • orders 테이블에 주문 아이디가 없는 사람이 한번도 주문하지 않은 사람이라는 idea로 풀기. -> WHERE 서브쿼리 활용
profile
데이터분석 공부로그

0개의 댓글