Sub topic 12
The NOT IN Trap and Other Filter Pitfalls
This chapter assumes that you can write basic SELECT statements with WHERE, use IS NULL, and understand NUMBER, VARCHAR2, and DATE values. You should also know basic aggregate functions such as COUNT, SUM, and AVG.
Why `NOT IN` can return no rows
A NULL value means missing or unknown data. It is not equal to zero, an empty string, or any other value. Oracle uses three-valued logic when NULL appears in a condition:
| Condition result | Meaning | Kept by `WHERE`? |
|---|---|---|
| `TRUE` | The condition is known to be true | Yes |
| `FALSE` | The condition is known to be false | No |
| `UNKNOWN` | The result cannot be determined because of `NULL` | No |
NOT IN is especially easy to misuse. Oracle evaluates this condition:
SELECT 'row returned' AS message
FROM dual
WHERE 1 NOT IN (2, 3);It is equivalent to:
WHERE 1 <> 2
AND 1 <> 3Both comparisons are true, so the row is returned. However, adding NULL changes the result:
SELECT 'row returned' AS message
FROM dual
WHERE 1 NOT IN (2, NULL);Oracle evaluates this as:
WHERE 1 <> 2
AND 1 <> NULLThe first comparison is TRUE. The second is UNKNOWN, because no value can be compared reliably with an unknown value. TRUE AND UNKNOWN is UNKNOWN, so the WHERE clause rejects the row.
The same problem occurs with a subquery. If even one row returned by the subquery is NULL, every outer row may be rejected:
SELECT e.employee_id
FROM employees e
WHERE e.department_id NOT IN (
SELECT d.department_id
FROM departments d
);If departments.department_id contains a NULL, the predicate is unsafe. The left value is also a problem when e.department_id itself is NULL.
Prefer `NOT EXISTS` for subqueries
NOT EXISTS asks whether a matching row exists. It does not turn the complete result set into a list of comparisons, so an unrelated NULL does not poison the predicate.
SELECT d.department_id,
d.department_name
FROM departments d
WHERE NOT EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
);This query returns departments with no matching employee. A NULL department ID on an employee row does not match a particular department ID, and it does not invalidate the entire query.
If you must use NOT IN with a subquery, remove NULL values explicitly and decide how to handle a NULL on the outer side:
SELECT e.employee_id
FROM employees e
WHERE e.department_id IS NOT NULL
AND e.department_id NOT IN (
SELECT d.department_id
FROM departments d
WHERE d.department_id IS NOT NULL
);Other filter pitfalls
Comparisons with NULL must use IS NULL or IS NOT NULL:
-- Correct
SELECT employee_id
FROM employees
WHERE commission_pct IS NULL;
-- Incorrect: this condition is UNKNOWN for every row
SELECT employee_id
FROM employees
WHERE commission_pct = NULL;A condition such as commission_pct <> 0 does not mean “zero or missing values are excluded.” It excludes missing values too, because NULL <> 0 is UNKNOWN. To include missing values, state that rule:
SELECT employee_id
FROM employees
WHERE commission_pct <> 0
OR commission_pct IS NULL;Be careful when using NVL in a filter. This expression treats missing commission as zero:
WHERE NVL(commission_pct, 0) = 0That may be correct, but it combines two different facts: an actual zero and an unknown commission. Use it only when those facts have the same business meaning.
Exercise
Rewrite this query so it reliably returns departments that have no employees, even if employees.department_id contains NULL values: SELECT d.departmentid FROM departments d WHERE d.departmentid NOT IN (SELECT e.department_id FROM employees e);
Show answer
SELECT d.departmentid FROM departments d WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.departmentid = d.department_id);