HAZEL

[SQL : Leetcode] LEFT JOIN 문 본문

DATA ANALYSIS/SQL

[SQL : Leetcode] LEFT JOIN 문

Rmsid01 2021. 2. 13. 00:31

1. 183. Customers Who Never Order


>> 문제 

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       |
+-----------+

 

>> 문제 푼 코드 

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

 

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

 

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