Customers Who Have Never Placed an Order
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
You are given two tables: Customers and CustomerOrders. Write a SQL query to find all customers who have never placed any order. Return the customer id, name, and country, ordered by id. Table: Customers
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| name | VARCHAR | Customer name |
| country | VARCHAR | Customer country |
Table: CustomerOrders
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| customer_id | INT | References Customers.id |
| amount | DECIMAL | Order amount |
| order_date | DATE | Date of order |
Database Schema (Inferred)
Customers
| Column Name | Example Value |
|---|---|
| id | 1 |
| name | Alice |
| country | US |
CustomerOrders
| Column Name | Example Value |
|---|---|
| id | 1 |
| customer_id | 1 |
| amount | 200 |
| order_date | 2023-01-01 |
Example
Customers
| id | name | country |
|---|---|---|
| 1 | Alice | US |
| 2 | Bob | UK |
| 3 | Carol | US |
| 4 | Dave | DE |
CustomerOrders
| id | customer_id | amount | order_date |
|---|---|---|---|
| 1 | 1 | 200 | 2023-01-01 |
| 2 | 1 | 150 | 2023-02-01 |
| 3 | 3 | 300 | 2023-01-15 |
Output
| id | name | country |
|---|---|---|
| 2 | Bob | UK |
| 4 | Dave | DE |
Explanation:
Alice (id=1) and Carol (id=3) have orders. Bob and Dave have no orders and are returned.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
SQL Editor
Loading...
Customers
| id | name | country |
|---|---|---|
| 1 | Alice | US |
| 2 | Bob | UK |
| 3 | Carol | US |
| 4 | Dave | DE |
CustomerOrders
| id | customer_id | amount | order_date |
|---|---|---|---|
| 1 | 1 | 200 | 2023-01-01 |
| 2 | 1 | 150 | 2023-02-01 |
| 3 | 3 | 300 | 2023-01-15 |
Output
| id | name | country |
|---|---|---|
| 2 | Bob | UK |
| 4 | Dave | DE |