Sub topic 2
Comparing with NULL: three-valued logic and IS NULL
This chapter assumes you can write SELECT statements with WHERE clauses and column aliases, that you know the basic Oracle data types (NUMBER, VARCHAR2, DATE), and that you can use the DUAL table for quick experiments. You will learn why = NULL never matches a row, how Oracle evaluates AND, OR, and NOT when NULL is involved, and how to write filters that handle NULL on purpose.
Why ordinary comparisons break
NULL means a value is missing or unknown. It is not zero, not an empty string, and not a special value that equals itself. Suppose you compare two unknown values. The honest answer to "are they equal?" is "unknown", not "yes" and not "no".
Oracle follows that reasoning. Any comparison operator (=, <>, <, >, <=, >=, LIKE, BETWEEN) gives an unknown result when either side is NULL. This includes comparing NULL with NULL.
Run these two statements in SQL*Plus, SQL Developer, or any Oracle client:
SELECT 1 AS one FROM dual WHERE NULL = NULL;
SELECT 1 AS one FROM dual WHERE NULL IS NULL;The first query returns no rows. The second returns one row. NULL = NULL is not false. It is unknown, and WHERE keeps a row only when its condition is true.
Three-valued logic
Standard two-valued logic has TRUE and FALSE. SQL adds a third value, UNKNOWN. A condition in Oracle can therefore evaluate to one of three results:
- TRUE: the row passes the filter.
- FALSE: the row is rejected.
- UNKNOWN: the row is rejected as well.
WHERE, HAVING, and the ON clause of a join all keep only rows whose condition is TRUE. Rows that evaluate to UNKNOWN are dropped silently, with no error and no warning. That silence is the root of most NULL bugs.
A few comparisons show the pattern:
| Expression | Result |
|---|---|
| `5 = 5` | TRUE |
| `5 = 6` | FALSE |
| `5 = NULL` | UNKNOWN |
| `NULL = NULL` | UNKNOWN |
| `NULL <> NULL` | UNKNOWN |
| `NULL IS NULL` | TRUE |
| `5 IS NULL` | FALSE |
| `5 IS NOT NULL` | TRUE |
Note that NULL <> NULL is also unknown. Writing <> does not rescue you. A negated comparison with NULL is still a comparison with NULL.
Truth tables for AND, OR, and NOT
Logical operators combine the three values. Think of UNKNOWN as "could be either". The result is TRUE or FALSE only when the answer would be the same no matter which one UNKNOWN stands for.
| AND | TRUE | FALSE | UNKNOWN |
|---|---|---|---|
| TRUE | TRUE | FALSE | UNKNOWN |
| FALSE | FALSE | FALSE | FALSE |
| UNKNOWN | UNKNOWN | FALSE | UNKNOWN |
| OR | TRUE | FALSE | UNKNOWN |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| FALSE | TRUE | FALSE | UNKNOWN |
| UNKNOWN | TRUE | UNKNOWN | UNKNOWN |
| NOT | Result |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
| UNKNOWN | UNKNOWN |
Two rows of these tables explain a lot of real behavior:
FALSE AND UNKNOWNis FALSE. Whatever the unknown turns out to be, the AND cannot be true.TRUE OR UNKNOWNis TRUE. Whatever the unknown turns out to be, the OR is already true.
NOT UNKNOWN is still UNKNOWN. So WHERE NOT (commission_pct = 0.2) does not return employees whose commission is NULL. The inner comparison is unknown, and negating it leaves it unknown.
Testing for NULL with IS NULL and IS NOT NULL
Oracle provides two predicates that test for NULL and always return TRUE or FALSE, never UNKNOWN:
SELECT employee_id, last_name
FROM employees
WHERE commission_pct IS NULL;
SELECT employee_id, last_name
FROM employees
WHERE commission_pct IS NOT NULL;Together these two queries cover every row in the table exactly once, because the predicates are complements of each other. Compare that with commission_pct = 0.2 and commission_pct <> 0.2. Those two filters together miss every row where the commission is NULL.
You can also apply IS NULL to any expression, not only to a column:
SELECT employee_id
FROM employees
WHERE salary + salary * commission_pct IS NULL;This is useful, but be aware that an arithmetic expression is NULL as soon as one operand is NULL. The next chapter covers that propagation in detail.
A concrete failure: the filter that loses rows
Consider a request such as "list every employee who does not earn a 0.2 commission". The natural query is:
SELECT employee_id, last_name, commission_pct
FROM employees
WHERE commission_pct <> 0.2;In the standard HR sample schema, most employees have a NULL commission. None of those rows come back, because NULL <> 0.2 is UNKNOWN. The query answers a narrower question: "who has a known commission that is not 0.2?"
To include employees with no commission, say so explicitly:
SELECT employee_id, last_name, commission_pct
FROM employees
WHERE commission_pct <> 0.2
OR commission_pct IS NULL;Here the OR works in your favor. For an employee with a NULL commission, the first branch is UNKNOWN and the second is TRUE. UNKNOWN OR TRUE is TRUE, so the row is kept.
Other places where UNKNOWN appears
NULL-related UNKNOWN results show up beyond plain = and <>.
BETWEEN and LIKE
x BETWEEN 1 AND 10 is shorthand for x >= 1 AND x <= 10. If x is NULL, both parts are UNKNOWN and the row is rejected. x LIKE 'A%' is UNKNOWN when x is NULL, and so is x NOT LIKE 'A%'.
IN and NOT IN
x IN (1, 2, NULL) expands to x = 1 OR x = 2 OR x = NULL. If x is 1, the first branch is TRUE and the whole expression is TRUE. If x is 3, the result is FALSE OR FALSE OR UNKNOWN, which is UNKNOWN. So the NULL in the list never causes a match, and it also prevents a clean FALSE.
NOT IN is where this becomes dangerous, and a later chapter treats it in full. A short preview:
SELECT 'row returned' AS msg FROM dual WHERE 1 NOT IN (2, 3);
SELECT 'row returned' AS msg FROM dual WHERE 1 NOT IN (2, NULL);The first query returns a row. The second returns none. 1 NOT IN (2, NULL) expands to 1 <> 2 AND 1 <> NULL, which is TRUE AND UNKNOWN, which is UNKNOWN.
Joins and CASE
An equality join such as a.col = b.col never matches rows where either column is NULL. Two NULL keys do not join to each other.
A searched CASE expression evaluates its conditions in order and takes the first one that is TRUE. An UNKNOWN condition is skipped, just as in WHERE:
SELECT employee_id,
CASE
WHEN commission_pct > 0.15 THEN 'high'
WHEN commission_pct <= 0.15 THEN 'low'
ELSE 'none or unknown'
END AS commission_band
FROM employees;Employees with a NULL commission make both WHEN conditions UNKNOWN, so they fall through to the ELSE branch. Without an ELSE, the CASE would return NULL for them.
Where Oracle departs from strict NULL logic
Oracle has a few places where NULLs are treated as equal or as a value you can group on. These do not contradict three-valued logic. They are separate rules for specific operations.
- GROUP BY puts all NULLs in one group.
- DISTINCT treats NULLs as duplicates of each other, so it returns one NULL row.
- ORDER BY sorts NULLs last in ascending order and first in descending order by default. You can override this with
NULLS FIRSTorNULLS LAST. - A unique index or unique constraint on a single column allows any number of rows with NULL in that column.
- DECODE treats two NULLs as equal. The last chapter of this tutorial covers it.
Common mistakes
- Writing
= NULLor<> NULL. Oracle accepts the syntax and returns no rows. UseIS NULLandIS NOT NULL. - Assuming
<>is the opposite of=. For non-NULL values it is. For NULL values both are UNKNOWN. - Assuming NOT turns "no match" into "match".
NOT UNKNOWNis UNKNOWN, so NULL rows stay excluded. - Expecting an empty string comparison to work. Oracle treats
''as NULL, soWHERE name = ''never returns a row. UseWHERE name IS NULL. The next chapters explain this behavior. - Forgetting NULL keys in joins. Rows with NULL join columns vanish from inner joins. Use an outer join if you must keep them.
Summary
- NULL means unknown, so any comparison with NULL, including
NULL = NULL, gives UNKNOWN. - WHERE, HAVING, and join conditions keep only rows that evaluate to TRUE. UNKNOWN rows are dropped.
- AND, OR, and NOT follow three-valued truth tables.
FALSE AND UNKNOWNis FALSE,TRUE OR UNKNOWNis TRUE, andNOT UNKNOWNis UNKNOWN. IS NULLandIS NOT NULLare the only comparisons that always return TRUE or FALSE for a NULL input.- Filters on nullable columns need an explicit
IS NULLbranch when NULL rows belong in the result.
Exercise
The employees table has 107 rows, and 72 of them have a NULL commissionpct. Of the remaining 35 rows, 5 have commissionpct = 0.2. How many rows does each of these queries return? (a) WHERE commissionpct = 0.2, (b) WHERE commissionpct <> 0.2, (c) WHERE NOT (commissionpct = 0.2), (d) WHERE commissionpct <> 0.2 OR commission_pct IS NULL.
Show answer
(a) 5 rows. (b) 30 rows, because the 72 NULL rows evaluate to UNKNOWN and are dropped. (c) 30 rows, because NOT UNKNOWN is still UNKNOWN. (d) 102 rows: the 30 rows with a known commission other than 0.2, plus the 72 NULL rows, for which UNKNOWN OR TRUE is TRUE.
Exercise
Without running it, decide whether this query returns a row, and explain why: SELECT 'yes' FROM dual WHERE (1 = 2) AND (NULL = NULL) OR (1 = 1) AND (NULL IS NULL);
Show answer
It returns one row. AND binds tighter than OR. The left side is FALSE AND UNKNOWN, which is FALSE. The right side is TRUE AND TRUE, which is TRUE. FALSE OR TRUE is TRUE, so the row is kept.