Interview Questions/SQL/Customers Who Have Never Placed an Order

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 NameTypeDescription
idINTPrimary key
nameVARCHARCustomer name
countryVARCHARCustomer country

Table: CustomerOrders

Column NameTypeDescription
idINTPrimary key
customer_idINTReferences Customers.id
amountDECIMALOrder amount
order_dateDATEDate of order

Database Schema (Inferred)

Customers

Column NameExample Value
id1
nameAlice
countryUS

CustomerOrders

Column NameExample Value
id1
customer_id1
amount200
order_date2023-01-01

Example

Customers

idnamecountry
1AliceUS
2BobUK
3CarolUS
4DaveDE

CustomerOrders

idcustomer_idamountorder_date
112002023-01-01
211502023-02-01
333002023-01-15

Output

idnamecountry
2BobUK
4DaveDE

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

idnamecountry
1AliceUS
2BobUK
3CarolUS
4DaveDE

CustomerOrders

idcustomer_idamountorder_date
112002023-01-01
211502023-02-01
333002023-01-15

Output

idnamecountry
2BobUK
4DaveDE