본문 바로가기
MySQL/문제풀이

[LeetCode] LEFT JOIN : 183. Customers Who Never Order

by MINNI_ 2021. 3. 15.

1. 문제

 

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

 

Table: Customers.

+----+-------+

| Id | Name |

+----+-------+

| 1  | Joe |

| 2  | Henry |

| 3  | Sam |

| 4  | Max |

+----+-------+

 

Table: Orders.

+----+------------+

| Id | CustomerId |

+----+------------+

| 1 | 3 |

| 2 | 1 |

+----+------------+

 

Using the above tables as example, return the following:

+-----------+

| Customers |

+-----------+

| Henry |

| Max |

+-----------+


2. 답

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

3. KEY POINT

 

  • LEFT JOIN : 왼쪽 테이블 기준으로 합치기
  • IS NULL : NULL값 인지 확인, True 일 경우 조건 해당됨
  • AS : 별명 붙이기

 

leetcode.com/problems/customers-who-never-order/submissions/

 

Customers Who Never Order - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

댓글