Conditional expression returning different values based on evaluated conditions.
The SQL conditional expression, supporting searched CASE (WHEN condition THEN result) and simple CASE (WHEN expression = value THEN result). Used for pivot-like transformations, conditional aggregation, and business rule encoding. Evaluate NULLIF and COALESCE first for simpler cases.
Compatibility
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | Native | all | Both simple and searched CASE. Also supports IF(condition, true_val, false_val) and IIF() as a 2-arg shorthand. |
| PostgreSQL | Native | all | Both simple and searched CASE. CASE can appear in SELECT, WHERE, ORDER BY, HAVING, and aggregate arguments. |
| SQL Server | Native | all | Both simple and searched CASE. Also IIF(condition, true_val, false_val) since SQL Server 2012. |
| Oracle | Native | all | Both simple and searched CASE. Legacy DECODE(expr, search, result, ...) function also works. |
| SQLite | Native | all | Both simple and searched CASE fully supported. |
Details
CASE is SQL-standard and works identically across all major engines. Dialect-specific shorthands (IF, IIF, DECODE) exist but are not portable.
Standard Syntax
Version Support
Per-Database Syntax & Notes
MySQL Native syntax
Both simple and searched CASE. Also supports IF(condition, true_val, false_val) and IIF() as a 2-arg shorthand.
PostgreSQL Native syntax
Both simple and searched CASE. CASE can appear in SELECT, WHERE, ORDER BY, HAVING, and aggregate arguments.
SQL Server Native syntax
Both simple and searched CASE. Also IIF(condition, true_val, false_val) since SQL Server 2012.
Oracle Native syntax
Both simple and searched CASE. Legacy DECODE(expr, search, result, ...) function also works.
SQLite Native syntax
Both simple and searched CASE fully supported.