Skip to content
>_sqlbuddy
easyJOINsSubqueriesNULL handling

Customers Without Orders

List customers who have never placed an order using an anti-join.

Start practice

Prompt

Customers Without Orders

Prompt

Given the customers and orders tables below, write a query that returns the name of every customer who has never placed an order.

  • Each customer should appear at most once in the result.
  • Only the customer name should be returned — no other columns.
  • orders.customer_id may be NULL; such orders do not belong to any customer and must not rescue anyone.

Example

customersorders
idnameidcustomer_id
1Alice11
2Bob23
3Carol3NULL
4Dave

Expected result:

name
Bob
Dave

Constraints

  • customers.id is the primary key.
  • orders.customer_id references customers.id but is nullable.

Tips

  • This is an anti-join: keep rows from the left side that have no match on the right.
  • LEFT JOIN + WHERE orders.id IS NULL and NOT EXISTS are the two idiomatic forms.
  • If you use a subquery with NOT IN, remember how NULL values behave inside NOT IN (…).

Expected concepts

  • LEFT JOIN
  • NOT EXISTS
  • Anti-join
  • NULL filtering

Schema

CREATE TABLE customers (
    id   INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE orders (
    id          INTEGER PRIMARY KEY,
    customer_id INTEGER REFERENCES customers(id)  -- NULL allowed
);

Sample data

INSERT INTO customers (id, name) VALUES
  (1, 'Alice'),
  (2, 'Bob'),
  (3, 'Carol'),
  (4, 'Dave');

INSERT INTO orders (id, customer_id) VALUES
  (1, 1),
  (2, 1),
  (3, 3),
  (4, 3);

Additional hidden fixtures are applied during validation to test edge cases.