The CASE expression lets you embed IF-THEN-ELSE style logic directly in SQL. It evaluates conditions in the order written and returns the first matching result; if nothing matches, the optional ELSE branch is used. CASE is not a control-of-flow statement (it does not short-circuit query execution) but an expression that yields a single scalar value, so it can appear wherever a value is allowed: SELECT lists, WHERE clauses, ORDER BY, GROUP BY, HAVING, UPDATE/INSERT/DELETE assignments, and even CHECK constraints.There are two forms:1. Simple CASE compares one expression to a list of potential values.2. Searched CASE evaluates independent Boolean conditions.All result expressions must be type-compatible so the database can determine a single output data type. When multiple WHEN branches match (possible only in searched CASE), the first one wins. NULL comparisons follow normal SQL rules (="NULL" never matches in simple CASE without IS NULL).
input_expression
(any scalar) - The expression being compared in a simple CASE.valueN
(same type) - Literal or expression compared to input_expression.conditionN
(boolean) - Predicate evaluated in searched CASE.resultN
(any scalar) - Value returned when its WHEN matches.default_result
(any scalar) - Value returned when no WHEN matches (optional).IF, IIF, DECODE (Oracle), COALESCE, NULLIF, ISNULL, Boolean logic, conditional aggregation
SQL-92 standard
Simple CASE compares one expression to several values. Searched CASE evaluates independent Boolean predicates. Choose searched CASE for complex conditions.
The first matching WHEN decides the returned value, but most engines still compute all branch expressions. Keep heavy calculations outside CASE when possible.
Anywhere a single scalar value is allowed: SELECT, WHERE, GROUP BY, HAVING, ORDER BY, JOIN conditions, and data-modification statements like UPDATE.
CASE returns NULL, which may propagate through expressions or filter rows out if compared with regular equality (remember to use IS NULL).