CASE WHEN lets a query label rows based on conditions. It is how you turn a column of raw amounts into bands such as low, medium and high, without changing the source table.
The structure
CASE WHEN condition THEN result WHEN another condition THEN another result ELSE default END
Example
Label each order as Small if the amount is under 1,000, Medium if it is under 5,000, and Large otherwise. Put the CASE expression in the SELECT list, and you can group by its result to count each band.
Two checks
- The conditions are checked in order, so put the most specific one first.
- Always include ELSE, or missing values become NULL and disappear from your totals.
Practise on the SQL basics you already know, then see the data analytics course. Book a free demo class, and ask a counsellor for the current schedule.
