// SQL
NOT IN subquery returns no rows: the NULL problem in SQL
Every example on this page was run on 25 November 2026 in the SQL database on this page, which is close to PostgreSQL, and the output under each one is what it printed. How we test these posts.
A NOT IN subquery that returns no rows almost always means the subquery’s column holds a NULL. x NOT IN (1, 2, NULL) is never true: SQL cannot say that x differs from an unknown value, so every row of the outer query is filtered out and you get an empty result with no error. The most common fix is to rewrite the query with NOT EXISTS, which ignores the NULL.
Why does my NOT IN subquery return no rows?
A NOT IN subquery returns no rows when at least one value it returns is NULL. Here the task is to find customers with no orders. Ben and Dev have none, but one order was saved without a customer, and that single NULL empties the whole result:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders)
ORDER BY name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders)
ORDER BY name;Output
name
The query is valid, so the database prints no error and no warning, only the header with nothing under it. The bug usually shows up when real data arrives: the query worked on test data with no NULL in it and returns nothing in production. Run the subquery on its own and look for a NULL in what it returns; that is the cause.
Why does NOT IN with NULL return nothing?
NOT IN with a NULL returns nothing because a NOT IN (b, c, d) means a <> b AND a <> c AND a <> d. Any comparison with NULL gives NULL, meaning unknown, not true. An AND that holds one NULL and no false is NULL as a whole, and WHERE keeps only rows where the condition is true:
SELECT 2 NOT IN (1, 3) AS no_null,
2 NOT IN (1, 3, NULL) AS with_null,
1 NOT IN (1, 3, NULL) AS matched,
2 <> NULL AS compare_null;
SELECT 2 NOT IN (1, 3) AS no_null,
2 NOT IN (1, 3, NULL) AS with_null,
1 NOT IN (1, 3, NULL) AS matched,
2 <> NULL AS compare_null;Output
| no_null | with_null | matched | compare_null |
|---|---|---|---|
| true | NULL | false | NULL |
So with a NULL in the list, NOT IN can only be false (the value is in the list) or NULL (it is not, as far as SQL can tell). It is never true, so no row passes. The PostgreSQL subquery expressions reference states this rule for NOT IN, and it comes from the SQL standard, so MySQL, SQL Server and Oracle return the same empty result.
IN does not have this problem. 2 IN (1, 3, NULL) is also NULL, but a value that is in the list still gives true, so IN finds the rows you expect and only NOT IN breaks.
How do I fix NOT IN with NULL values?
Fix NOT IN with NULL values by using NOT EXISTS, by removing the NULL values inside the subquery, or by rewriting it as a LEFT JOIN. All three return Ben and Dev here.
Use NOT EXISTS instead of NOT IN
Replace WHERE id NOT IN (SELECT ...) with WHERE NOT EXISTS (SELECT 1 ... WHERE inner.id = outer.id). NOT EXISTS asks whether any matching row exists. The order with a NULL customer never matches anyone, so it is simply not found:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)
ORDER BY c.name;Output
name Ben Dev
NOT EXISTS is the safe default for “rows in A with no match in B”. It also works with a match on two or more columns, which NOT IN handles awkwardly.
Filter out NULL inside the subquery
Adding WHERE customer_id IS NOT NULL to the subquery is the smallest change when you want to keep NOT IN:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT name
FROM customers
WHERE customer_id NOT IN (
SELECT customer_id FROM orders WHERE customer_id IS NOT NULL
)
ORDER BY name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT name
FROM customers
WHERE customer_id NOT IN (
SELECT customer_id FROM orders WHERE customer_id IS NOT NULL
)
ORDER BY name;Output
name Ben Dev
The filter has to stay there for as long as the query lives. If a later edit drops it, or the subquery is changed to read a column that allows NULL, the empty result comes back. A NOT NULL constraint on the column is the only lasting guarantee.
Rewrite it as a LEFT JOIN with IS NULL
A LEFT JOIN keeps every customer and fills the order columns with NULL where nothing matched, so WHERE o.order_id IS NULL keeps the customers with no orders:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen'), (4, 'Dev')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, 1), (103, 3), (104, NULL)
)
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL
ORDER BY c.name;Output
name Ben Dev
Test a column that can never be NULL in a real match, such as the primary key of the right table. Testing o.customer_id IS NULL also works here, but testing a nullable column like a discount code would keep customers whose order simply had no code.
Does NOT EXISTS give the same result as NOT IN?
NOT EXISTS gives the same result as NOT IN with IS NOT NULL only when the outer column has no NULL either. A customer whose own customer_id is NULL matches no order, so NOT EXISTS keeps that row. NOT IN drops it, because NULL NOT IN (1, 3) is NULL:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (NULL, 'Guest')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, NULL)
)
SELECT 'not exists' AS method, c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id)
UNION ALL
SELECT 'not in', c.name
FROM customers c
WHERE c.customer_id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL)
ORDER BY method, name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (NULL, 'Guest')
),
orders(order_id, customer_id) AS (
VALUES (101, 1), (102, NULL)
)
SELECT 'not exists' AS method, c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id)
UNION ALL
SELECT 'not in', c.name
FROM customers c
WHERE c.customer_id NOT IN (SELECT customer_id FROM orders WHERE customer_id IS NOT NULL)
ORDER BY method, name;Output
| method | name |
|---|---|
| not exists | Ben |
| not exists | Guest |
| not in | Ben |
Decide which answer you want. For “customers with no orders”, the guest row usually belongs in the result, which is one more reason to prefer NOT EXISTS.
How do I catch NOT IN NULL bugs before they ship?
Catch NOT IN bugs by searching your SQL for NOT IN (SELECT and checking whether the subquery’s column can be NULL. If it can, switch to NOT EXISTS. Add a test row with a NULL to the data your query tests run on, as the snippets above do: a query that only meets clean data never shows the bug. A NOT NULL constraint on foreign key columns that must always be filled removes the cause at the source.
The same three-valued logic is behind other surprises: WHERE col <> 'x' skips rows where col is NULL, and COUNT(col) skips them too. The PostgreSQL comparison operators page explains IS NULL and IS DISTINCT FROM. The find duplicate rows in SQL post covers another query that goes wrong on NULL, and more tasks like this are in the SQL interview practice guide.
Related errors
NOT IN versus NOT EXISTS is a question of its own, about which one to choose and how each performs; the short answer is above. A LEFT JOIN that acts like an inner join, because a WHERE condition on the right table removes the rows with NULL, is the same logic seen from the join side.