[Leetcode] [Database] Customers Who Never Order解題

題目如下

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.

Id Name
1 Joe
2 Henry
3 Sam
4 Max

Table: Orders.

Id CustomerId
1 3
2 1

Using the above tables as example, return the following:

Customers
Henry
Max

題目意思: 選出沒有訂單的用戶


解題思路: 選出不在order中存在的用戶

select Name 
    from Customers c 
        where not exists (
            select 1 from Orders where c.Id=CustomerId);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章