Sub topic 2
Comparing with NULL: Three-Valued Logic and IS NULL
Prerequisites
This chapter assumes that you can write basic SELECT statements with WHERE, use column aliases, and run quick tests against Oracle's DUAL table. You should also know that NULL is different from values such as 0, an empty string, or FALSE.
What NULL means
In Oracle, NULL represents a missing, unknown, or inapplicable value. It does not mean that the value is zero or that two values are equal to each other.
For example, a missing commission percentage does not tell us that the percentage is zero. It tells us that no percentage is currently known or stored.
This distinction affects comparisons. SQL does not use only two logical results. A condition can produce one of three results:
| Result | Meaning |
|---|---|
| `TRUE` | The condition is satisfied |
| `FALSE` | The condition is not satisfied |
| `UNKNOWN` | The result cannot be determined |
Oracle's WHERE clause returns only rows for which the condition is TRUE. It removes rows where the condition is either FALSE or UNKNOWN.
Why `= NULL` does not work
Any comparison with NULL produces UNKNOWN, including comparisons that appear to test whether two values are both missing.
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. Oracle cannot conclude that the two unknown values are equal. The second query returns one row because IS NULL specifically tests whether an expression contains NULL.
The same rule applies to ordinary columns:
SELECT employee_id, commission_pct
FROM employees
WHERE commission_pct = NULL;This query does not find employees whose commission is missing. Use IS NULL instead:
SELECT employee_id, commission_pct
FROM employees
WHERE commission_pct IS NULL;To find values that are present, use IS NOT NULL:
SELECT employee_id, commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;IS NULL and IS NOT NULL are predicates, which are expressions that produce a logical result for filtering. They are the correct operators for testing nullness.
Boolean logic with UNKNOWN
UNKNOWN also affects compound conditions:
TRUE AND UNKNOWNisUNKNOWN.FALSE AND UNKNOWNisFALSE.TRUE OR UNKNOWNisTRUE.FALSE OR UNKNOWNisUNKNOWN.NOT UNKNOWNis stillUNKNOWN.
For example:
SELECT employee_id, salary, commission_pct
FROM employees
WHERE commission_pct > 0
OR commission_pct IS NULL;This returns employees with a positive commission and employees whose commission is missing. The explicit IS NULL condition is necessary because commission_pct > 0 evaluates to UNKNOWN when commission_pct is NULL.
A common mistake is to write:
WHERE commission_pct <> 0This excludes NULL values because NULL <> 0 is UNKNOWN, not TRUE. If missing commissions should also be included, write the rule explicitly:
WHERE commission_pct <> 0
OR commission_pct IS NULL;Exercise
Why does WHERE department_id <> 10 not return rows whose department_id is NULL, and how would you include those rows?
Show answer
department_id <> 10 evaluates to UNKNOWN for NULL values, so the WHERE clause rejects them. Add OR department_id IS NULL.