LeetCode 數據庫面試題 – 從不訂購的客戶

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

Customers 表:

1Joe
2Henry
3Sam
4Max

Orders 表:

IdCustomerId
13
21

例如給定上述表格,你的查詢應返回:

Customers
Henry
Max

解答

SELECT
    Name as Customers
FROM Customers
WHERE Id NOT IN (
    SELECT
        DISTINCT CustomerId
    FROM Orders
)

原題

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/customers-who-never-order