Find customers without orders

Return customer_id and name for customers who have no matching order, regardless of order status or amount. An order with a NULL customer_id belongs to no customer. Row order does not matter.

Difficulty: intermediate

Show hint

Use a LEFT JOIN and check for a missing order_id, or use NOT EXISTS. A NULL in a NOT IN subquery can change its result.

Tables and sample data

Each test starts with fresh tables. Result rows may be returned in any order. Column names and column order must match.

Sample data: customers
customer_id (integer)name (text)city (text)
1AshaKathmandu
2BenLondon
3ChenSingapore
Sample data: orders
order_id (integer)customer_id (integer)amount (numeric)status (text)region (text)
1150paidEast
2370pendingWest
Expected sample result
customer_idname
2Ben

Your solution

SQL · PostgreSQL

Results

PostgreSQL loads when you Run or Submit. Write one read-only SELECT or WITH query. Your query runs in this browser.

Back to Lesson