CASE Expressions in SQL
From the SQL cheat sheet ยท CASE & NULL Handling ยท verified Jul 2026
CASE Expressions
Add conditional logic (if/else) to queries.
sql
-- CASE WHEN
SELECT name,
CASE
WHEN age < 18 THEN 'Minor'
WHEN age < 65 THEN 'Adult'
ELSE 'Senior'
END AS age_group
FROM users;
-- Simple CASE
SELECT name,
CASE status
WHEN 'A' THEN 'Active'
WHEN 'I' THEN 'Inactive'
ELSE 'Unknown'
END AS status_label
FROM users;๐ก CASE works everywhere: SELECT, WHERE, ORDER BY, UPDATE, and even inside aggregates
โก Use CASE inside COUNT/SUM for pivot-style crosstab reports
๐ CASE evaluates conditions in order โ the first match wins, then it stops
๐ข Always include ELSE to handle unexpected values โ otherwise you get NULL
caseconditionalwhen-then