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

More SQL tasks

Back to the full SQL cheat sheet