Labels with CASE
Stage 2: Filtering · Lesson 9 of 17 · 5 min read · free, interactive
CASE turns values into labels. Conditions are checked top to bottom; the first one that is true wins, and ELSE covers everything else.
SELECT name, level,
CASE WHEN level >= 10 THEN 'veteran' ELSE 'rookie' END AS rank
FROM players;
With several WHENs, order matters — put the strictest condition first:
SELECT name, price,
CASE
WHEN price >= 50 THEN 'legendary'
WHEN price >= 10 THEN 'rare'
ELSE 'common'
END AS tier
FROM items;
Tip: Without an ELSE, rows that match no WHEN get NULL.
Practice
Each answer runs in your browser and is checked on hidden data.
- Fill in the blanks: Label each item
cheap(under 5),normal(5 to 20) orpricey(over 20). Returnnameandlabel. Row order does not matter. - Write the query: For every match return its
idandlength:'quick'if it lasted under 300 seconds, otherwise'long'. Row order does not matter. - Find the bug: Return each player's
nameandtier:'gold'for level 12+,'silver'for level 8+, otherwise'bronze'. Row order does not matter.
Previous: Text patterns with LIKE · Next: Counting and totals · All lessons