面试题:查询所有的客户记录
难度:容易
给定Customers
和Orders
两个表,写一段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
)