[mySQL] 리트코드 Customers Who Never Order

sehyunny·2023년 4월 21일

mySQL

목록 보기
5/26

https://leetcode.com/problems/customers-who-never-order/

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.
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+

조회할 데이터 : 한 번도 주문한 적 없는 고객의 이름
조건1. 고객의 이름 컬럼을 Customers라고 별칭을 줄 것

SELECT name as Customers
FROM customers
     LEFT JOIN orders ON customers.id = orders.customerid
WHERE orders.customerid IS NULL
  • INNER JOIN을 사용할 경우, orders 테이블에 데이터가 저장되어 있는(주문 이력이 있는) 고객인 Joe, Sam만 출력하게 됨
  • orders 테이블에 데이터가 없는(주문 이력이 없는) 고객까지 모두 출력해야 하므로 LEFT JOIN을 사용한다

☑️ point

- INNER JOIN과 LEFT JOIN의 속성, 차이점 이해

0개의 댓글