Results.

SCEPTRA.TECH

SQLite NOT IN Returns No Rows? Check the Subquery for NULL Before Changing Data

Original diagram of SQLite NOT IN with a NULL-containing list and WHERE filtering

You want IDs that are not in a second table. The query looks reasonable, the tables contain rows, but SQLite returns nothing. Before changing the data or removing the filter, check whether the subquery used by NOT IN includes a NULL.

This is a case where an unknown result matters as much as a true or false result. I would expose the comparison in a SELECT first, then decide what missing IDs should mean for the application.

Reproduce the missing-row problem safely

Run this example only in a fresh disposable database. It creates demonstration tables, not a repair script for existing records:

CREATE TABLE candidates (id INTEGER);
INSERT INTO candidates VALUES (1), (2);
CREATE TABLE blocked (id INTEGER);
INSERT INTO blocked VALUES (2), (NULL);

SELECT id
FROM candidates
WHERE id NOT IN (SELECT id FROM blocked);

The result is empty. ID 2 is clearly blocked, but why did ID 1 disappear too? Look at the expression without filtering its output away:

SELECT id, id NOT IN (SELECT id FROM blocked) AS allowed
FROM candidates
ORDER BY id;

For ID 1, allowed is NULL. For ID 2, it is 0. SQLite's SELECT rules include a row only when the WHERE expression is true; both false and NULL exclude it.

The expression reference documents this specific NOT IN case: when the left value is non-NULL, is not found on the right, and the right side contains NULL, the result is NULL rather than true.

Ask whether a matching row exists

If the rule is to return candidates for which no equal blocked ID exists, a correlated NOT EXISTS query expresses that rule directly:

SELECT c.id
FROM candidates AS c
WHERE NOT EXISTS (
    SELECT 1
    FROM blocked AS b
    WHERE b.id = c.id
)
ORDER BY c.id;

For the demonstration data, this returns ID 1. The subquery finds a matching row for ID 2 and no matching row for ID 1. SQLite documents EXISTS as returning either 0 or 1 based on whether its subquery returns rows.

There is another possible policy: ignore missing IDs in the blocked list. For this example's non-NULL candidate values, filtering the subquery also returns ID 1:

SELECT id
FROM candidates
WHERE id NOT IN (
    SELECT id FROM blocked WHERE id IS NOT NULL
)
ORDER BY id;

Do not treat these as interchangeable repairs for every schema. Choose the query that states the intended missing-value policy, then test it with your actual constraints.

Decide what a missing candidate ID means

The equality-based NOT EXISTS query above does not find a matching blocked row for a NULL candidate ID, because b.id = c.id is not true for that comparison. It can therefore include that candidate. If candidates must have known IDs, add an explicit c.id IS NOT NULL condition to the outer query.

Also test an empty blocked table. SQLite gives NOT IN a true result for an empty right-hand set, even when its left operand is NULL. That edge case is another reason to state your NULL policy explicitly instead of relying on what one sample happens to return.

Keep diagnosis separate from data cleanup

I ran the demonstration locally in an in-memory SQLite database. The original filter returned no rows; the exposed comparison showed NULL and 0; both revised queries returned ID 1 for the sample. These are correctness examples for this small dataset, not performance benchmarks.

Do not delete NULL rows or change a production schema simply to make a report populate. Check why those values exist and whether they are valid. Review query plans and workload separately before making performance claims. A query returning rows again is useful evidence only when those are the rows the rule actually requires.

Sources

Comments