← Back to SQL, page 2

sql-0030 · Hard senior

Why does SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders) return no rows when one order has a NULL customer_id?

x NOT IN (a, b, NULL) expands to x <> a AND x <> b AND x <> NULL. The last comparison is UNKNOWN, so the whole condition can never be TRUE and every row is filtered out. Use NOT EXISTS, which compares row by row and is not affected by NULLs, or filter NULLs out of the subquery.

Full answer

The goal is "customers who have never ordered". Dev has no orders, so he should appear. Instead:

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, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);
idname

(0 rows)

One guest order (id 106) has customer_id NULL. The subquery returns 1, 1, 2, 3, 2, NULL. For Dev, 4 NOT IN (1, 1, 2, 3, 2, NULL) means:

text
4 <> 1 AND 4 <> 1 AND 4 <> 2 AND 4 <> 3 AND 4 <> 2 AND 4 <> NULL
TRUE   AND TRUE   AND TRUE   AND TRUE   AND TRUE   AND UNKNOWN   =  UNKNOWN

UNKNOWN is not TRUE, so Dev is dropped. Every other customer is dropped too, because each matches some order. The result is empty once a single NULL appears in the list, and the query gives no warning.

NOT EXISTS asks a different question for each customer: "is there an order row whose customer_id equals this customer's id?". A NULL customer_id never equals anything, so it simply does not count:

sql
SELECT c.id, c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
idname
4Dev

Filtering NULLs out of the subquery also works, but it relies on everyone remembering to add it:

sql
SELECT id, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL);
idname
4Dev

A LEFT JOIN with IS NULL is a third way to write the same anti-join:

sql
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
idname
4Dev

IN (without NOT) does not have this problem, because x IN (..., NULL) is still TRUE when x matches a non-NULL value. The trap is specific to NOT IN.

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, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);

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

SELECT id, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL);

SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • NOT IN with any NULL in the list can never be TRUE
  • The query returns no rows and no error
  • NOT EXISTS is NULL-safe and is the usual fix
  • LEFT JOIN ... WHERE right.key IS NULL is an equivalent anti-join
  • Plain IN is not affected the same way

Common mistakes

  • Using NOT IN on a nullable column and testing only on data without NULLs
  • Fixing it by adding IS NOT NULL in one place and forgetting it in the next query

Follow-ups

  • Which of NOT EXISTS, NOT IN and LEFT JOIN performs best on your database?
  • Does a NOT NULL constraint on orders.customer_id remove the problem?

Tips

  • Write out the expanded AND chain; it makes the explanation obvious
  • Name NOT EXISTS as the default choice