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) and name
  • Orders: Contains order information with id (primary key) and customerId (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.customerId references valid Customers.id
  • Customer names are non-empty varchar strings

Visualization

Tap to expand
Finding Customers Who Never OrderCustomersidname1Joe2Henry3Sam4MaxOrdersidcustomerId1321No MatchNo MatchFind Non-MatchesResultCustomersHenryMaxTwo Main ApproachesApproach 1: LEFT JOINSELECT c.nameFROM Customers cLEFT JOIN Orders o ON c.id = o.customerIdWHERE o.customerId IS NULL;Keep all customers, filter NULL matchesApproach 2: NOT EXISTSSELECT c.nameFROM Customers cWHERE NOT EXISTS ( SELECT 1 FROM Orders o WHERE o.customerId = c.id);Check if orders exist for each customer
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.
Asked in
Amazon 28 Google 15 Microsoft 12
125.0K Views
High Frequency
~8 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen