Labels

Wednesday, February 25, 2015

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       |
+-----------+ 
 
值得纪念的一次,第一次做出一道数据库的题,我没有任何数据库基础可言,全是自学youtube Standford 那个教程的(https://www.youtube.com/watch?v=D-k-h0GuFmE&list=PL6hGtHedy2Z4EkgY76QOcueU8lAC4o6c3),觉得这道题好简单啊。


 SELECT Name AS Customers  
 FROM Customers  
 WHERE Id NOT IN  
 (SELECT CustomerId AS Id FROM Orders);  

发图一张,特此纪念。

 
  

No comments:

Post a Comment