Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

This query returns no rows even though many customers have never ordered: SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders). What is wrong and how do you fix it?

If orders.customer_id contains even one NULL, then for every customer whose id is not in the list, id NOT IN (..., NULL) evaluates to UNKNOWN instead of TRUE, and WHERE drops the row, so the query returns nothing. The fix is NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id), which is null-safe and usually planned as an efficient anti-join; filtering the subquery with customer_id IS NOT NULL also works, and if the column should never be NULL, it should be declared NOT NULL.

Why this question is asked

This is one of the best-known traps in SQL, and it still reaches production regularly, because the query is correct on clean test data and breaks the moment one row with a NULL foreign key appears. A report of customers who never ordered suddenly shows zero, and nothing errors. Interviewers ask it because the explanation requires real understanding of three-valued logic, not memorisation. In this answer, we will reproduce the problem, expand NOT IN to see exactly why it returns nothing, fix it with NOT EXISTS and with a filtered subquery, compare the plans, and add the test row that would have caught it.

1Reproducing the Problem

We need only three customers and three orders, one of which has no customer, for example a guest checkout or a row from an import that did not resolve the customer.

SQL
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int); -- nullable
INSERT INTO customers VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Cleo');
INSERT INTO orders VALUES (10, 1), (11, 1), (12, NULL); -- Ben and Cleo never ordered
SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);
-- (0 rows)
DELETE FROM orders WHERE id = 12;
SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);
-- 2 | Ben
-- 3 | Cleo

One NULL in the subquery result is enough to make the whole query return nothing. When we delete that one row, the expected answer comes back. That is the clearest possible sign that the NULL is the cause.

2Why NOT IN Returns Nothing

x NOT IN (a, b, c) is defined as x <> a AND x <> b AND x <> c. All parts must be TRUE for the row to pass. Let us expand it for Ben, id 2, with the subquery result (1, 1, NULL).

Properties
-- 2 NOT IN (1, 1, NULL)
-- = 2 <> 1 AND 2 <> 1 AND 2 <> NULL
-- = TRUE AND TRUE AND UNKNOWN
-- = UNKNOWN -> WHERE drops Ben
-- 1 NOT IN (1, 1, NULL)
-- = 1 <> 1 AND ...
-- = FALSE AND ...
-- = FALSE -> Ana is correctly dropped
Step throughHow NOT IN evaluates one customer
  1. Is id equal to some listed value?
    YesFALSE: row droppedNoLook at the NULLs
  2. Does the list contain a NULL?
    YesUNKNOWN: row droppedNoTRUE: row kept

2 steps in order. Play them, or step through them one at a time.

With a NULL in the list, no row can ever reach TRUE.

The comparison 2 <> NULL is UNKNOWN, because we cannot say an unknown value differs from 2; it might be 2. TRUE AND UNKNOWN is UNKNOWN, and WHERE keeps only TRUE. So every customer who is not in the list becomes UNKNOWN, and every customer who is in the list becomes FALSE. Nobody is TRUE, so no row is returned. In plain words, the database is saying: there is an order whose customer we do not know, so we cannot be sure Ben has no orders.

3Fix 1: Use NOT EXISTS

NOT EXISTS asks a different question: is there any order row for this customer? The correlated condition o.customer_id = c.id is UNKNOWN for the NULL row, so that row simply does not match, and it no longer poisons the result.

SQL
SELECT c.*
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.id
);
-- 2 | Ben
-- 3 | Cleo

EXISTS only ever returns TRUE or FALSE, never UNKNOWN, because it checks whether the subquery produced at least one row. That is why NOT EXISTS is null-safe, and why it is the form I write by default for this kind of question. A LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL gives the same answer and is also common.

4Fix 2: Remove NULLs From the Subquery, or From the Schema

If we want to keep NOT IN, we can filter the NULLs out of the subquery. Then the list contains only known values and NOT IN behaves as expected.

SQL
SELECT * FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL);
-- 2 | Ben
-- 3 | Cleo
-- If every order must have a customer, fix the schema instead
ALTER TABLE orders ALTER COLUMN customer_id SET NOT NULL;

The second option is the deeper fix when the business rule says every order belongs to a customer. A NOT NULL constraint states that rule once, and then NOT IN, joins and reports all behave. If guest orders are legitimate, the column stays nullable, and we must write queries that handle it, which brings us back to NOT EXISTS.

5Why NOT EXISTS Is Usually Faster Too

Correctness is the main reason, but plans matter on large tables. PostgreSQL turns NOT EXISTS into an anti-join, a join that returns the left rows with no match on the right, and it can run it as a hash anti-join that reads each table once. With a nullable column, NOT IN cannot be rewritten as an anti-join, because the NULL rule changes its meaning, so PostgreSQL uses a hashed or plain subplan instead. When the subquery result is too large to hash in memory, that subplan can become catastrophically slow, checking the list for every customer.

SQL
EXPLAIN SELECT c.* FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
-- Hash Right Anti Join (cost=...)
-- Hash Cond: (o.customer_id = c.id)
EXPLAIN SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);
-- Seq Scan on customers
-- Filter: (NOT (hashed SubPlan 1)) -- or a plain SubPlan when the list is too big to hash

The exact plan text depends on the version and statistics, so check your own EXPLAIN output, but the anti-join for NOT EXISTS is the consistent pattern.

6Catching It With a Test

The query passed every test the team had, because none of the test orders had a NULL customer. The test data should include the case that changes the meaning: one order with customer_id NULL, plus at least one customer with no orders. With that row present, the NOT IN version returns an empty list and the test fails immediately, which is exactly what a test is for.

7Follow-up questions

Does IN have the same problem?

Not in the same way. x IN (a, b, NULL) means x = a OR x = b OR x = NULL. If x matches a or b, one part is TRUE and TRUE OR UNKNOWN is TRUE, so the row is kept correctly. If x matches nothing, the result is UNKNOWN instead of FALSE, but WHERE drops UNKNOWN anyway, so the rows returned are the same. The difference only shows if we negate it, which is exactly what NOT IN does, or if we use the IN result as a value, for example in a SELECT list, where it shows NULL instead of false.

Is the same true in JPQL?

Yes. A JPQL query with NOT IN on a subquery is translated to SQL NOT IN, so the same NULL rule applies. JPQL also supports NOT EXISTS with a correlated subquery, which is the safe form.

Does MySQL behave the same way?

Yes, MySQL follows the same three-valued logic, so NOT IN with a NULL in the subquery returns no rows there too. MySQL 8 also turns NOT EXISTS into an anti-join.

8Conclusion

In this answer, we saw that NOT IN expands to a chain of <> comparisons joined by AND, so one NULL in the subquery makes the result UNKNOWN for every customer who is not in the list, and WHERE drops them all. NOT EXISTS is null-safe and usually planned as an anti-join, filtering NULLs from the subquery or declaring the column NOT NULL also works, and a test row with a NULL foreign key catches the bug before production does.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is a NULL in orders.customer_id. NOT IN is defined as id not equal to each value in the list, combined with AND. If the list contains a NULL, then for any customer who isn't in the list, one of those comparisons is id not equal to NULL, which is UNKNOWN, and TRUE AND UNKNOWN is UNKNOWN.

Then, if they want more

WHERE only keeps TRUE, so every customer is dropped and the query returns nothing. Customers who are in the list are FALSE anyway, so no row can pass. I would rewrite it with NOT EXISTS, SELECT 1 FROM orders o WHERE o.customer_id equals c.id, which only returns TRUE or FALSE and therefore ignores the NULL rows.

It's also usually planned as an anti-join, while NOT IN on a nullable column often can't be, so NOT EXISTS is typically faster on big tables. Alternatively I can add WHERE customer_id IS NOT NULL inside the subquery, and if every order must have a customer, the real fix is a NOT NULL constraint.

I'd also add a test with an order that has a NULL customer, because that's the row that exposes the bug. IN doesn't have the same visible problem, since a match still gives TRUE.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "This query returns no rows even though many customers have never ordered: SELECT * FROM...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/5f72928176/