Powered By GitBook
183.Customers Who Never Order

183.Customers Who Never Order

难度:Easy
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
1
Customers 表:
2
3
+----+-------+
4
| Id | Name |
5
+----+-------+
6
| 1 | Joe |
7
| 2 | Henry |
8
| 3 | Sam |
9
| 4 | Max |
10
+----+-------+
11
Orders 表:
12
13
+----+------------+
14
| Id | CustomerId |
15
+----+------------+
16
| 1 | 3 |
17
| 2 | 1 |
18
+----+------------+
19
例如给定上述表格,你的查询应返回:
20
21
+-----------+
22
| Customers |
23
+-----------+
24
| Henry |
25
| Max |
26
+-----------+
Copied!
需要注意Orders表中的去重,
1
# Write your MySQL query statement below
2
SELECT Name as Customers from Customers WHERE Id NOT IN (SELECT DISTINCT CustomerId from Orders)
Copied!
Last modified 2yr ago
Copy link