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.

Previous: Text patterns with LIKE · Next: Counting and totals · All lessons