Skip to main content

183. Customers Who Never Order

Giới thiệu bài toán

Chi tiết: https://leetcode.com/problems/customers-who-never-order/

Table: Customers

Column NameType
idint
namevarchar
  • id is the primary key (column with unique values) for this table.
  • Each row of this table indicates the ID and name of a customer.

Table: Orders

Column NameType
idint
customerIdint
  • id is the primary key (column with unique values) for this table.
  • customerId is a foreign key (reference columns) of the ID from the Customers table.
  • Each row of this table indicates the ID of an order and the ID of the customer who ordered it.
Yêu cầu

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.

Example 1:

Input: Customers table:

idname
1Joe
2Henry
3Sam
4Max

Orders table:

idcustomerId
13
21

Output:

Customers
Henry
Max

Giải quyết bài toán

SELECT name Customers
FROM Customers
WHERE id NOT IN (SELECT customerId FROM Orders);

Tham khảo: https://leetcode.com/submissions/detail/1039107765/