Learn SQL
-- tables.case_expr

CASE — Conditional Expressions in SQL

If-then in SQL: bucket, pivot, custom-sort

--concept

CASE is SQL’s conditional value: simple form matches one value (CASE x WHEN 1 THEN …), searched form tests conditions (CASE WHEN salary > 90000 THEN …). It returns a value anywhere values go — SELECT, WHERE, ORDER BY, GROUP BY. Always include ELSE (default NULL surprises) and close with END.

--how it works
1

Searched CASE buckets

WHEN conditions tested in order; first TRUE wins. Always ELSE + END.

2

Simple CASE matches

CASE x WHEN val THEN … — equality shorthand for one column.

3

Pivot in one pass

SUM(CASE WHEN … THEN 1 ELSE 0 END) counts categories without N queries.

4

Sort custom orders

ORDER BY CASE status WHEN … encodes priority sequences indexes cannot.

--visualization
idnamesalary1Alice850002Bob520003Cynthia910004David470005Elena580006Farid73000midjuniorseniorjuniorjuniormid
senior ≥90k mid ≥60k junior
Stage 1 / 8
CASE tests WHEN branches top-down per row — first TRUE wins. ELSE closes the rest.
--example
query.sql
1SELECT name, CASE WHEN salary >= 90000 THEN 'senior' WHEN salary >= 60000 THEN 'mid' ELSE 'junior' END AS band FROM employees;
Result
nameband
Alicemid
Bobjunior
Cynthiasenior
Davidjunior
Elenajunior
Faridmid

Each row is bucketed by its salary. Cynthia clears 90k → senior; Alice and Farid land mid; the rest junior. Same pattern pivots rows to columns and encodes custom sort orders.

--variants

Salary bands

Bucket every row into senior/mid/junior.

query.sql
1SELECT name, CASE WHEN salary >= 90000 THEN 'senior' WHEN salary >= 60000 THEN 'mid' ELSE 'junior' END AS band FROM employees;
Result
nameband
Alicemid
Bobjunior
Cynthiasenior
Davidjunior
Elenajunior
Faridmid
1Conditions test top-down per row.
2Cynthia clears 90k → senior on first branch.
3No match + no ELSE would yield NULL — ELSE closes it.

Pivot + custom sort

Count Engineering in one scan; sort urgent first.

query.sql
1SELECT SUM(CASE WHEN department = 'Engineering' THEN 1 ELSE 0 END) AS eng FROM employees;
Result
eng
3
1Each row contributes 1 or 0; SUM tallies.
2One grouped scan replaces two filtered queries.
3Same CASE in ORDER BY would push urgent statuses first.
--key facts
Two formssimple CASE x WHEN … vs searched CASE WHEN cond …
Needs ELSE+ENDno match + no ELSE yields NULL
Swiss-armybucket, pivot, custom-sort — one expression
--use cases
Band/bucket reports: seniority, age tiers, status labels
Portable pivots where PIVOT syntax is unavailable
--under the hood
CASE is evaluated per row — index-neutral in SELECT, but CASE in WHERE can defeat seeks; prefer bare-column predicates.
FILTER (WHERE …) aggregates compile to the same plan with cleaner text on PG/SQLite.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.