SQL isn’t just about selecting and aggregating data—it can also apply conditional logic to transform or categorize results. That’s where the CASE expression comes in.
CASE lets you create new values based on conditions, similar to an IF/ELSE statement in programming.
Basic CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
ENDYou can use CASE in a SELECT statement, inside aggregation functions, or even in ORDER BY.
Example: Categorizing Paintings by Period
Suppose you want to group paintings into Renaissance, 19th Century, or Modern categories based on YearCreated.
SELECT Title,
Artist,
YearCreated,
CASE
WHEN YearCreated BETWEEN 1400 AND 1600 THEN 'Renaissance'
WHEN YearCreated BETWEEN 1800 AND 1899 THEN '19th Century'
WHEN YearCreated >= 1900 THEN 'Modern'
ELSE 'Other'
END AS PeriodCategory
FROM Gallery.Paintings;Result:
| Title | Artist | YearCreated | PeriodCategory |
|---|---|---|---|
| The Last Supper | Leonardo da Vinci | 1498 | Renaissance |
| Mona Lisa | Leonardo da Vinci | 1503 | Renaissance |
| Girl with a Pearl Earring | Johannes Vermeer | 1665 | Other |
| The Starry Night | Vincent van Gogh | 1889 | 19th Century |
| The Scream | Edvard Munch | 1893 | 19th Century |
| The Persistence of Memory | Salvador Dalà | 1931 | Modern |
| Nighthawks | Edward Hopper | 1942 | Modern |
📌 Important Note: Best practice is to always include an ELSE default condition for handling any values that fall outside all WHEN conditions. While the CASE expression without ELSE will complete without error, this can unintentionally exclude records from results.
- If you don’t want to assign a value when no conditions are met, use
ELSE NULL. - If it’s your intention to exclude records that don’t meet specific conditions, use a
WHEREcondition to make that explicitly clear.
Example: Using CASE in ORDER BY
You can also use CASE to customize sort order.
This query will sort results by PeriodCategory, then by Artist.
SELECT Title,
Artist,
YearCreated,
CASE
WHEN YearCreated BETWEEN 1400 AND 1600 THEN 'Renaissance'
WHEN YearCreated BETWEEN 1800 AND 1899 THEN '19th Century'
WHEN YearCreated >= 1900 THEN 'Modern'
ELSE 'Other'
END AS PeriodCategory
FROM Gallery.Paintings
ORDER BY CASE
WHEN YearCreated BETWEEN 1400 AND 1600 THEN 'Renaissance'
WHEN YearCreated BETWEEN 1800 AND 1899 THEN '19th Century'
WHEN YearCreated >= 1900 THEN 'Modern'
ELSE 'Other'
END,
Artist;Notice that the CASE statement ending on line 9 includes an alias AS PeriodCategory to define the column name in the results, but the CASE statement ending on line 16 does not include an alias. This is intentional.
📌 Important Note: The ORDER BY clause will return an error when an alias is included.
Example: Using CASE with GROUPING()
When using ROLLUP, CUBE, or GROUPING SETS, subtotal and total rows show up with NULL values. You can use CASE with the GROUPING() function to replace those NULLs with descriptive labels.
SELECT
CASE
WHEN GROUPING(CountryOfOrigin) = 1 THEN 'All Countries'
ELSE CountryOfOrigin
END AS CountryLabel,
CASE
WHEN GROUPING(Artist) = 1 THEN 'All Artists'
ELSE Artist
END AS ArtistLabel,
COUNT(1) AS PaintingCount
FROM Gallery.Paintings
WHERE CountryOfOrigin IN ('Italy','Netherlands','Spain')
GROUP BY ROLLUP (CountryOfOrigin, Artist)
ORDER BY CountryOfOrigin,
Artist;Result:
| CountryLabel | ArtistLabel | PaintingCount |
|---|---|---|
| Italy | Leonardo da Vinci | 2 |
| Italy | All Artists | 2 |
| Netherlands | Johannes Vermeer | 3 |
| Netherlands | Vincent van Gogh | 3 |
| Netherlands | All Artists | 6 |
| Spain | Salvador Dalà | 3 |
| Spain | All Artists | 3 |
| All Countries | All Artists | 11 |
This makes reports more user-friendly by replacing ambiguous NULL values with meaningful text.
Key Takeaways
CASElets you apply conditional logic directly in SQL queries.- Use
CASEinSELECT,ORDER BY, or aggregation expressions to categorize or transform data. - Combine
CASEwithGROUPING()to label subtotal and total rows when usingROLLUP,CUBE, orGROUPING SETS. - This technique improves readability and is highly useful for reporting and dashboards.

