Customers Who Never Order Problem


Description

LeetCode Problem 183.

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.

1
2
3
4
5
6
7
8
+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Table: Orders.

1
2
3
4
5
6
+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

Using the above tables as example, return the following:

1
2
3
4
5
6
+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+


MySQL Solution

1
2
3
select c.Name as Customers
from Customers c
where c.Id not in (select CustomerId from Orders)




Related Posts

Department Top Three Salaries Problem

LeetCode 185. Write a SQL query to find employees who...

Department Highest Salary Problem

LeetCode 184. Write a SQL query to find employees who...

Rising Temperature Problem

LeetCode 197. Write an SQL query to find all dates’...

Delete Duplicate Emails Problem

LeetCode 196. Write a SQL query to delete all duplicate...

Duplicate Emails Problem

LeetCode 182. Write a SQL query to find all duplicate...

Customers Who Never Order Problem

LeetCode 183. Write a SQL query to find all customers...