← Back to SQL, page 7

sql-0130 · Easy fresher

Which customers have never placed an order? Solve it with EXCEPT.

SELECT id FROM customers EXCEPT SELECT customer_id FROM orders returns customer ids missing from orders. EXCEPT is NULL-safe because set operations treat NULLs as equal, which avoids the NOT IN trap. Oracle calls the operator MINUS.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
SELECT id FROM customers
EXCEPT
SELECT customer_id FROM orders
ORDER BY id;
id
4

Dev (4) has no orders. Note that orders contains a guest order with a NULL customer_id. That NULL would break NOT IN:

sql
SELECT id FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders)
ORDER BY id;
id

(0 rows)

NOT IN returns nothing, because 4 NOT IN (1, 2, 3, NULL) evaluates to unknown. EXCEPT compares rows with "is not distinct" semantics, so the NULL does no harm.

To show names, join back or use NOT EXISTS, which most engines also plan as an anti join:

sql
SELECT c.name FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ORDER BY c.name;
name
Dev
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

SELECT id FROM customers
EXCEPT
SELECT customer_id FROM orders
ORDER BY id;

SELECT id FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders)
ORDER BY id;

SELECT c.name FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ORDER BY c.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • EXCEPT returns rows in the first result but not the second
  • Set operations treat NULLs as equal
  • NOT IN with a NULL in the list returns no rows
  • Oracle uses MINUS for EXCEPT

Common mistakes

  • Using NOT IN against a nullable column
  • Putting the larger set second and reversing the meaning

Follow-ups

  • How would you return the customer names?
  • How do EXCEPT and NOT EXISTS differ in handling duplicates?

Tips

  • Point out the NULL guest order before choosing an approach