CASE brings if-else logic into SQL. Analysts use it constantly to bucket values into categories, build pivot-style reports, and compute conditional metrics — all inside a single query.
Key Points
- Syntax: CASE WHEN salary >= 100000 THEN 'High' WHEN salary >= 50000 THEN 'Mid' ELSE 'Low' END
- Works in SELECT, WHERE, ORDER BY and inside aggregates
- Conditional aggregation: SUM(CASE WHEN status='paid' THEN amount ELSE 0 END)
- Pivot rows into columns using one CASE per column
- Custom sort: ORDER BY CASE priority WHEN 'urgent' THEN 1 … END
- Conditions evaluate top-down — order your WHENs carefully
.png)