leetcode183. Customer never ordered (SQL)

A site contains two tables, Customers table and the Orders table. Write a SQL query to find all customers never ordered anything.

Customers 表:

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

+ ---- + ------------ +
| Id | CustomerId |
+ ---- + ------------ +
| 1 | 3 |
| 2 | 1 |
+ ---- + ------------ +
example given in the table above, your query should return:

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

Ideas: Isolated appeared CustomerId, which name corresponds id if not in this list, you qualify.

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

 

发布了552 篇原创文章 · 获赞 1万+ · 访问量 132万+

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104316206