WHEN is not a standalone SQL statement; it is a keyword that lives inside a CASE expression (both simple and searched forms). Each CASE can contain one or more WHEN clauses that are evaluated sequentially from top to bottom. For a simple CASE, the WHEN value is compared to the CASE input expression using =. For a searched CASE, the WHEN is followed by any Boolean predicate. The first WHEN whose condition is TRUE (not NULL) causes the associated THEN expression to be returned. If no WHEN matches, control falls through to an optional ELSE branch; without ELSE, the CASE returns NULL. Only one WHEN branch is ever taken because evaluation stops after the first match. WHEN also appears in a few dialect-specific constructs, such as triggers in SQLite (CREATE TRIGGER … WHEN) and exception handling in PL/SQL (WHEN OTHERS). This documentation focuses on its standard usage in CASE expressions, which is portable across major databases. Key caveats: evaluation is short-circuited, predicate order matters, and NULL comparisons in simple CASE do not match unless the CASE expression itself is NULL and a WHEN NULL branch is provided.
condition
(BOOLEAN) - Predicate evaluated for a searched CASE.compare_value
(ANY COMPARABLE) - Value compared against input_expression in a simple CASE.result
(ANY) - Expression returned when its WHEN branch matches.CASE, THEN, ELSE, IIF, DECODE, IFNULL
SQL-92 standard
It introduces a condition to test. If the condition is met, the corresponding THEN result is returned and the rest of the CASE is skipped.
Yes. Place them sequentially. They are checked in order until one condition is TRUE.
If you included an ELSE branch, its expression is returned. Without ELSE, the CASE expression returns NULL.
Yes. WHEN inside CASE was added in SQL-92 and is implemented by all major relational databases.