Sub topic 9
NULLs and the `NOT IN` Trap
Assume you know basic SELECT, filtering, joins, and aggregate functions. This chapter focuses on Oracle's NULL rules and on choosing NOT EXISTS when a subquery may return null values.
Why `NOT IN` Can Return No Rows
NULL means that a value is missing or unknown. It is not equal to zero, an empty string, or another NULL.
Oracle SQL uses three-valued logic for comparisons:
TRUE: the condition is satisfied.FALSE: the condition is not satisfied.UNKNOWN: the result cannot be determined becauseNULLis involved.
A WHERE clause keeps only rows for which its condition is TRUE. It removes rows for both FALSE and UNKNOWN.
Consider this query:
SELECT last_name
FROM employees
WHERE employee_id NOT IN (SELECT manager_id
FROM employees);The subquery returns the IDs of managers. In the HR sample schema, at least one employee has no manager, so manager_id contains NULL.
Oracle evaluates NOT IN as a series of not-equal comparisons. For example:
employee_id NOT IN (101, 102, NULL)means:
employee_id != 101
AND employee_id != 102
AND employee_id != NULLThe comparison employee_id != NULL is UNKNOWN, not TRUE. The complete AND condition therefore cannot become TRUE, so the query returns no rows.
Removing the Null from the Subquery
If NOT IN expresses the exact logic you need, exclude nulls explicitly:
SELECT e.last_name
FROM employees e
WHERE e.employee_id NOT IN
(SELECT r.manager_id
FROM employees r
WHERE r.manager_id IS NOT NULL);Now the subquery returns only known IDs. The outer query can compare every employee ID with those IDs normally.
This solution is useful when the subquery column is guaranteed to be non-null by a constraint or by its filter. However, NOT EXISTS usually communicates the intended relationship more clearly.
Prefer `NOT EXISTS` for Anti-Matches
EXISTS checks whether a subquery returns at least one matching row. NOT EXISTS checks that no matching row exists. The selected value inside the subquery does not matter, so SELECT 1 is conventional.
SELECT e.last_name
FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM employees r
WHERE r.manager_id = e.employee_id
);For each employee, Oracle asks whether a row exists whose manager_id equals that employee's ID. A null manager_id does not match any employee ID, so it does not poison the result.
This query returns employees who manage nobody. It is an anti-match: it keeps an outer row when no related inner row exists.
Choosing Between the Forms
Use NOT IN when the subquery result is known to be non-null and set comparison is the clearest expression. Use NOT EXISTS when the relationship is correlated or when nullable data is possible.
Remember that NOT EXISTS and NOT IN can also differ when the outer expression itself is NULL. Test the required business meaning, not just the syntax.
Exercise
How can you safely rewrite employee_id NOT IN (SELECT manager_id FROM employees) when manager_id may contain NULL?
Show answer
Use NOT EXISTS with a correlated equality, or filter the subquery with WHERE manager_id IS NOT NULL.