Before continuing: This entry is part of the GROUP BY guide. If you are new to the topic, start with GROUP BY.
Query
The teaching database contains 18 orders. We will sort them into three bands and work out how many orders there are in each and how much they add up to.
SELECT
CASE
WHEN total < 100 THEN 'Less than £100'
WHEN total < 200 THEN '£100 to £199.99'
ELSE '£200 or more'
END AS band,
COUNT(*) AS orders,
ROUND(SUM(total), 2) AS amount
FROM orders
GROUP BY
CASE
WHEN total < 100 THEN 'Less than £100'
WHEN total < 200 THEN '£100 to £199.99'
ELSE '£200 or more'
END
ORDER BY MIN(total);The 9 + 4 + 5 orders add up to the 18 records in the table, a useful check for gaps. The amounts also add up to £3,931.90, the overall sum for the table. ORDER BY MIN(total) sorts the groups by the smallest real value in each band rather than by the label text.
How it works
Both occurrences of CASE must express exactly the same classification. Repeating the expression is verbose, but it avoids relying on an engine letting you use the SELECT within GROUP BYalias. If the classification gets long, a CTE or a subquery can work out the band once and group by it afterwards.
Cases that change the result
Design boundaries with no gaps or overlaps
A safe way to build increasing ranges is to use only the upper limit in each WHEN. Once a row fails total < 100, the second condition total < 200 implicitly means “100 or more and less than 200”. You do not need to repeat total >= 100.
Overlapping conditions
CASE
WHEN total <= 100 THEN 'low'
WHEN total >= 100 AND total < 200 THEN 'medium'
...
ENDThe value 100 meets both conditions. CASE will return the first one because it is evaluated from top to bottom, but the definition of the ranges is ambiguous. Decide in advance whether each boundary belongs to the band below or the one above.
Leaving values out
If you write a final WHEN that is too specific and leave out ELSE, the rows that are not covered get NULL. That can create a null group you did not expect. Use ELSE when there is a valid catch-all category, or handle the values that should be left out explicitly.
What to do with NULL
A comparison such as total < 100 is not true when total is NULL. If you have null data, decide what it means before grouping. You can give it its own category with a WHEN total IS NULL THEN 'No data' placed before the numeric ranges.
CASE
WHEN total IS NULL THEN 'No data'
WHEN total < 100 THEN 'Less than £100'
WHEN total < 200 THEN '£100 to £199.99'
ELSE '£200 or more'
ENDDo not turn NULL into zero for convenience if “unknown value” and “zero amount” mean different things in your domain.
Avoid repeating a long CASE with a CTE
When the classification has many branches, you can work out the band first and group afterwards. This separates two jobs: one stage classifies rows and another produces the summary.
WITH ranked_orders AS (
SELECT total,
CASE
WHEN total < 100 THEN 'Less than £100'
WHEN total < 200 THEN '£100 to £199.99'
ELSE '£200 or more'
END AS band
FROM orders
)
SELECT band,
COUNT(*) AS orders,
ROUND(SUM(total), 2) AS amount
FROM ranked_orders
GROUP BY band;This form is not “faster” by definition. Its teaching advantage is that it makes the sequence visible: classify → summarise. Real performance depends on the engine, the plan and the data.