面试题:查询所有的客户记录

难度:容易

给定CustomersOrders两个表,写一段SQL查询所有没有订购记录的客户。

表: Customers.

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

表: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

返回记录:

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

答案:

select a.Name from Customers a
where a.Id not in(
    select CustomerId from Orders
)

posted @ 2015-08-26 21:13  -小城-  阅读(162)  评论(0编辑  收藏  举报