Full answer
The goal is "customers who have never ordered". Dev has no orders, so he should appear. Instead:
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);| id | name |
|---|
(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:
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 = UNKNOWNUNKNOWN 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:
SELECT c.id, c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);| id | name |
|---|---|
| 4 | Dev |
Filtering NULLs out of the subquery also works, but it relies on everyone remembering to add it:
SELECT id, name
FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL);| id | name |
|---|---|
| 4 | Dev |
A LEFT JOIN with IS NULL is a third way to write the same anti-join:
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;| id | name |
|---|---|
| 4 | Dev |
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.
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