Leecode SQL 183. Customers Who Never Order

  1. Customers Who Never Order

Write a solution to find all customers who never order anything.
Return the result table in any order.
The result format is in the following example.

Input

Customers =

id name
1 Joe
2 Henry
3 Sam
4 Max

Orders =

id customerId
1 3
2 1

Output

Customers
Henry
Max

My solution:

SELECT c.name AS Customers 
FROM customers c
WHERE c.id NOT IN(
    SELECT c.id
    FROM customers c
        JOIN orders o
            ON c.id = o.customerId
)

注意,不能按name筛选!会有重名!要按id!

上一篇:TDSQL-C电商可视化,重塑电商决策新纪元