某網站包含兩個表,Customers 表和 Orders 表。編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。
Customers 表:
1 | Joe |
---|---|
2 | Henry |
3 | Sam |
4 | Max |
Orders 表:
Id | CustomerId |
---|---|
1 | 3 |
2 | 1 |
例如給定上述表格,你的查詢應返回:
Customers |
---|
Henry |
Max |
解答
1SELECT
2 Name as Customers
3FROM Customers
4WHERE Id NOT IN (
5 SELECT
6 DISTINCT CustomerId
7 FROM Orders
8)
原題
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/customers-who-never-order