183. Customers who never order
Suppose a Web site contains two tables, Customers tables and Orders tables. Write an SQL statement that finds all customers who never order anything.
tables and data SQL used:
Create table If not Exists Customers (idint, Name varchar (255));
Create table If not Exists Orders (Id int,customerid int);
Truncate table Customers;
Insert into Customers (Id, Name) VALUES (' 1 ', ' Joe ');
Insert into Customers (Id, Name) VALUES (' 2 ', ' Henry ');
Insert into Customers (Id, Name) VALUES (' 3 ', ' Sam ');
Insert into Customers (Id, Name) VALUES (' 4 ', ' Max ');
Truncate table Orders;
Insert into Orders (Id, CustomerId) VALUES (' 1 ', ' 3 ');
Insert into Orders (Id, CustomerId) VALUES (' 2 ', ' 1 ');
Answer:
This problem is very simple, directly let two tables left outer connection, and then as long as the right side of the CustomerID is a NULL customer is not placed orders of customers.
Select C.name Customers from
Customers C-left
join Orders o ' O.customerid = c.id
where O.customerid is Nu ll
Write more rough, there is no understanding can scan two-dimensional code plus QQ Group to find my answer.