Sub topic 5
CASE and NULL: why WHEN NULL never matches
This chapter assumes you can write simple and searched CASE expressions, and that you know NULL means "unknown or missing value" rather than zero or an empty string. It explains why WHEN NULL never matches, how NULL slips into the wrong branch of a searched CASE, and how to write NULL-safe conditions.
The core rule: only TRUE selects a branch
SQL uses three-valued logic. A comparison with NULL returns neither true nor false. It returns UNKNOWN. For example, NULL = NULL, NULL = 5 and NULL <> 5 are all UNKNOWN.
CASE only takes a branch when its test is TRUE. FALSE and UNKNOWN are treated the same way: CASE skips that WHEN and tries the next one. If nothing matches, CASE returns the ELSE value, or NULL when there is no ELSE.
The important point is that UNKNOWN follows the same path as FALSE. CASE never raises an error or a warning when a NULL makes a test UNKNOWN. It silently moves on.
Why a simple CASE cannot match NULL
A simple CASE compares one expression with each WHEN value using equality. This:
CASE x WHEN NULL THEN 'matched' ENDis evaluated as x = NULL. That test is UNKNOWN for every value of x, including when x is itself NULL. The branch can never be chosen.
The following query shows this. It uses a small inline table so you can run it without any sample schema:
WITH t (x) AS (
SELECT 1 FROM dual
UNION ALL
SELECT CAST(NULL AS NUMBER) FROM dual
)
SELECT x,
CASE x
WHEN NULL THEN 'matched NULL'
WHEN 1 THEN 'one'
ELSE 'no match'
END AS simple_result,
CASE
WHEN x IS NULL THEN 'is null'
WHEN x = 1 THEN 'one'
ELSE 'other'
END AS searched_result
FROM t;| X | SIMPLE_RESULT | SEARCHED_RESULT |
|---|---|---|
| 1 | one | one |
| (null) | no match | is null |
The simple CASE sends the NULL row to ELSE. The WHEN NULL branch is dead code. Only the IS NULL predicate in a searched CASE detects the NULL.
The fix: use a searched CASE with IS NULL
IS NULL and IS NOT NULL are the only predicates that return TRUE or FALSE, never UNKNOWN, when applied to a possibly-NULL value. To test for NULL, switch to the searched form:
SELECT employee_id,
commission_pct,
CASE
WHEN commission_pct IS NULL THEN 'No commission'
WHEN commission_pct = 0 THEN 'Zero commission'
ELSE 'Has commission'
END AS commission_status
FROM employees;Put the IS NULL branch first, or at least before any branch that could silently catch NULL rows through ELSE.
NULLs hiding in comparison branches
The WHEN NULL mistake is easy to spot once you know about it. The more common problem is a NULL value that reaches a branch you never intended. Consider the salary bands from earlier in the tutorial:
SELECT employee_id,
salary,
CASE
WHEN salary < 5000 THEN 'Low'
WHEN salary < 10000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees;If salary is NULL, both salary < 5000 and salary < 10000 are UNKNOWN. The row falls through to ELSE and is labelled High. A row with no salary data is reported as a top earner.
The same thing happens when the final branch is a test instead of ELSE:
CASE
WHEN salary >= 10000 THEN 'High'
ELSE 'Not high'
ENDA NULL salary gets Not high. That may or may not be what you want, but it is a decision you should make explicitly. A safer version names the NULL case:
SELECT employee_id,
salary,
CASE
WHEN salary IS NULL THEN 'Unknown'
WHEN salary < 5000 THEN 'Low'
WHEN salary < 10000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees;Because CASE returns the first TRUE branch, putting IS NULL first removes NULL rows before any comparison can misclassify them.
Summary of how NULL behaves in each form
| Situation | Result |
|---|---|
| Simple CASE, `WHEN NULL` | Never matches, even when the tested expression is NULL |
| Simple CASE, tested expression is NULL, `WHEN 1` | Not equal, so branch skipped |
| Searched CASE, `WHEN col = NULL` | Never TRUE |
| Searched CASE, `WHEN col IS NULL` | TRUE when `col` is NULL |
| Searched CASE, `WHEN col < 5` with NULL `col` | UNKNOWN, branch skipped |
| No branch matches, no ELSE | CASE returns NULL |
NULL as a result value
A NULL in the result position is allowed and often useful. Use it when you want a branch to deliberately produce "no value":
SELECT employee_id,
CASE
WHEN department_id = 50 THEN salary
ELSE NULL
END AS shipping_salary
FROM employees;This is equivalent to leaving out ELSE NULL, because CASE returns NULL when no branch matches. Writing it out documents your intent. Oracle does not allow the literal NULL in every result position, including ELSE, so at least one result must be a real value or expression.
When a value comes out as NULL, you cannot tell whether it came from a matching branch that returned NULL or from no match at all. If that distinction matters, add an explicit ELSE with a marker value.
Oracle's empty string is NULL
In Oracle, a zero-length VARCHAR2 value is treated as NULL. This means a test such as the following never matches:
CASE WHEN middle_name = '' THEN 'blank' ELSE 'has value' ENDmiddle_name = '' is the same as middle_name = NULL, which is UNKNOWN. Use middle_name IS NULL instead. The same applies to WHEN '' in a simple CASE.
Working around NULL in a simple CASE
Sometimes you want a simple CASE for its brevity but the expression may be NULL. You have two options.
- Convert to a searched CASE and add an
IS NULLbranch. This is the clearest choice. - Replace NULL with a sentinel value using NVL, for example
CASE NVL(commission_pct, -1) WHEN -1 THEN ....
The second option works only if the sentinel can never appear as real data. If someone later stores -1, the logic breaks with no error. Prefer the first option unless you have a strong reason.
Testing your own CASE expressions
A quick way to check how a CASE handles NULL is to run it against a row where each input is NULL. Build a throwaway test table with WITH ... FROM dual, as in the first example. Test these cases for every CASE you write:
- A value that matches the first branch.
- A value that matches only a later branch.
- A value that matches nothing.
- A NULL value.
If you cannot predict the NULL result before running the query, the expression needs an explicit IS NULL branch.
Summary
- Only TRUE selects a CASE branch. FALSE and UNKNOWN both move on to the next WHEN.
- A simple CASE compares with
=, soWHEN NULLcan never match. - Use a searched CASE with
IS NULLto detect NULL. - A NULL input to a comparison branch falls through to later branches or to ELSE, which can mislabel it.
- Put the
IS NULLbranch first, and write ELSE deliberately. - Oracle treats an empty string as NULL, so
= ''never matches.
Exercise
A query uses CASE status WHEN NULL THEN 'Missing' WHEN 'A' THEN 'Active' ELSE 'Other' END. What does it return for a row where status is NULL, and how do you change it so that row returns 'Missing'?
Show answer
It returns 'Other'. The simple CASE evaluates status = NULL, which is UNKNOWN, so the first branch is skipped. The NULL is not equal to 'A', so it falls to ELSE. Rewrite as a searched CASE: CASE WHEN status IS NULL THEN 'Missing' WHEN status = 'A' THEN 'Active' ELSE 'Other' END.
Exercise
For employees with a NULL salary, what band does this expression return, and why? CASE WHEN salary < 5000 THEN 'Low' WHEN salary < 10000 THEN 'Medium' ELSE 'High' END. Write a corrected version that labels those employees 'Unknown'.
Show answer
It returns 'High'. Both comparisons are UNKNOWN for a NULL salary, so CASE skips them and uses ELSE. Corrected version: CASE WHEN salary IS NULL THEN 'Unknown' WHEN salary < 5000 THEN 'Low' WHEN salary < 10000 THEN 'Medium' ELSE 'High' END.