Customers Who Never Order - Problem
Write a SQL solution to find all customers who have never placed an order.
You are given two tables:
- Customers: Contains customer information with
id(primary key) andname - Orders: Contains order information with
id(primary key) andcustomerId(foreign key to Customers)
Return the result table in any order showing customers who don't appear in the Orders table.
Table Schema
Customers
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key - unique customer identifier |
name
|
varchar | Customer name |
Primary Key: id
Orders
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key - unique order identifier |
customerId
|
int | Foreign key reference to Customers.id |
Primary Key: id
Input & Output
Example 1 — Basic Case with Multiple Customers
Input Tables:
Customers
| id | name |
|---|---|
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
Orders
| id | customerId |
|---|---|
| 1 | 3 |
| 2 | 1 |
Output:
| Customers |
|---|
| Henry |
| Max |
💡 Note:
Joe (id=1) and Sam (id=3) have orders in the Orders table, so they are excluded. Henry (id=2) and Max (id=4) have no orders, so they are returned as customers who never order.
Example 2 — All Customers Have Orders
Input Tables:
Customers
| id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Orders
| id | customerId |
|---|---|
| 1 | 1 |
| 2 | 2 |
Output:
| Customers |
|---|
💡 Note:
Both Alice and Bob have orders, so no customers are returned. This demonstrates the edge case where all customers have placed at least one order.
Example 3 — No Orders Exist
Input Tables:
Customers
| id | name |
|---|---|
| 1 | John |
| 2 | Jane |
Orders
| id | customerId |
|---|
Output:
| Customers |
|---|
| John |
| Jane |
💡 Note:
The Orders table is empty, so all customers are returned as they have never placed any orders.
Constraints
-
1 ≤ Customers.id ≤ 1000 -
1 ≤ Orders.id ≤ 1000 -
Orders.customerIdreferences validCustomers.id - Customer names are non-empty varchar strings
Visualization
Tap to expand
Understanding the Visualization
1
Input Tables
Customers and Orders with foreign key relationship
2
Join/Filter
LEFT JOIN or NOT EXISTS to find non-matches
3
Output
Customer names with no orders
Key Takeaway
🎯 Key Insight: Use LEFT JOIN or NOT EXISTS to find records in one table that don't have corresponding records in another table - a common pattern for finding 'missing' relationships.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code