Stay button (LeetCode) of the title 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 |
+-----------+

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/customers-who-never-order
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

 

 

answer:

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

Guess you like

Origin www.cnblogs.com/mmfang/p/12587076.html