Customers Who Never Order (题目标题)
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 |
+-----------+
solution
the first solution that i used is 'select row name from table name where not exists( )'
SELECT Name
FROM Customers
WHERE NOT EXISTS (
SELECT * FROM Orders
WHERE Customers.id = Orders.CustomerId);
the second solution that i used is 'not in '
select a.Name Customers from Customers where a.Id not in (select b.CustomerId from Orders b )
'SELECT Name FROM Customers c WHERE c.Id NOT IN (SELECT CustomerId FROM Orders o);'