183. Customers Who Never Order - 從不訂購的客戶 <easy>

Coder阿飛發表於2020-10-21

某網站包含兩個表,Customers 表和 Orders 表。編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
例如給定上述表格,你的查詢應返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

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.

select Name as `Customers`
from Customers 
where Id not in (select CustomerId from Orders)

 

相關文章