Sub topic 9
The NOT IN Trap and the NOT EXISTS Fix
Assume you know basic SELECT, WHERE, and subqueries, along with tables, rows, and primary keys. This chapter focuses on how NULL changes a query that uses NOT IN.
Why `NOT IN` Can Return No Rows
NOT IN looks like a direct way to find values absent from a list:
SELECT name
FROM employees
WHERE id NOT IN (1, 2);This works when every value in the list is known. However, a subquery can produce NULL. SQL treats NULL as an unknown value, not as an ordinary value that equals anything.
Consider this table:
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INTEGER,
commission NUMERIC(8, 2)
);
INSERT INTO employees (id, name, manager_id, commission) VALUES
(1, 'Ada', NULL, NULL),
(2, 'Ben', 1, 100.00),
(3, 'Cy', 1, NULL),
(4, 'Di', 2, 300.00);
-- This subquery returns NULL, 1, 1, and 2.
SELECT manager_id
FROM employees;
-- This returns no rows.
SELECT name
FROM employees
WHERE id NOT IN (
SELECT manager_id
FROM employees
);SQL interprets id NOT IN (values) roughly as a series of comparisons joined by OR, followed by NOT:
NOT (id = NULL OR id = 1 OR id = 1 OR id = 2)For id = 4, the comparisons produce:
UNKNOWN OR FALSE OR FALSE OR FALSEThe result is UNKNOWN. Negating UNKNOWN still produces UNKNOWN. Because WHERE keeps only rows whose condition is TRUE, Di is removed. The same issue affects Cy. Ben and Ada have matching manager IDs and are excluded for the expected reason.
The `NOT EXISTS` Rewrite
NOT EXISTS asks whether the subquery produces at least one matching row. It does not compare the outer value against every returned value, so an unrelated NULL does not poison the result.
-- Returns Cy and Di: employees who manage nobody.
SELECT e.name
FROM employees AS e
WHERE NOT EXISTS (
SELECT 1
FROM employees AS report
WHERE report.manager_id = e.id
);For each employee, the database checks for a report whose manager_id equals that employee's id:
- Ada has Ben and Cy as reports, so she is excluded.
- Ben has Di as a report, so he is excluded.
- Cy has no reports, so he is included.
- Di has no reports, so he is included.
The SELECT 1 expression is conventional. EXISTS only cares whether a row exists, not which columns the subquery selects.
Choosing the Pattern
Use NOT EXISTS when the excluded values come from another query, especially when the source column may contain NULL. An equivalent anti-join can also work:
SELECT e.name
FROM employees AS e
LEFT JOIN employees AS report
ON report.manager_id = e.id
WHERE report.id IS NULL;Test a non-NULL column from the right table, such as its primary key. Testing report.manager_id IS NULL would be ambiguous if real matched rows were allowed to have a null manager ID.
Exercise
Why can id NOT IN (SELECT manager_id FROM employees) return no rows when the subquery contains one NULL, and which pattern should you normally use instead?
Show answer
Comparisons with NULL produce UNKNOWN, and NOT IN can therefore produce UNKNOWN rather than TRUE. WHERE removes UNKNOWN rows. Use NOT EXISTS, or filter NULLs explicitly when NOT IN is deliberate.