1.题目
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.
翻译:假设一个网站包含两个表,Customers表和Orders表。写一个SQL查询语句,找到所有没订过货的顾客.
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 | +-----------+
2.思路
找到Customers表中的实体的Name,其Id从未出现在Orders表中的CustomerId中。
3.算法
select Name as Customers
from Customers where Customers.Id not in (Select CustomerId from Orders)