Skip to content
Codewilds

Lesson 27 of 30 · Advanced Queries

SQL CASE WHEN: If-Then-Else Logic in Your Queries

medium20 XP2 min read

Reports are full of rules: a hero counts as a veteran from level 20, expensive items get a flag, quests should be listed easy-to-hard instead of alphabetically. CASE is SQL's if-then-else. It picks a value for each row, based on conditions.

CASE WHEN … THEN … ELSE … END

SELECT name, level,
       CASE
         WHEN level >= 20 THEN 'veteran'
         WHEN level >= 10 THEN 'adept'
         ELSE 'novice'
       END AS tier
FROM heroes;

SQLite checks the WHEN conditions from top to bottom and returns the THEN value of the first one that is true. If none is true, it returns the ELSE value. Hugo (level 23) is a veteran, Aria (14) an adept and Finn (5) a novice. END closes the expression, and AS tier names the new column.

Leave out the ELSE and rows that match no WHEN get NULL.

The short form

When you compare one column with fixed values, there's a shorter form, CASE column WHEN value THEN …. CASE works anywhere an expression does, so you can also sort by it:

SELECT title, difficulty
FROM quests
ORDER BY CASE difficulty
           WHEN 'easy' THEN 1
           WHEN 'medium' THEN 2
           ELSE 3
         END,
         title;

The quests now come easy first, then medium, then hard. Sorted alphabetically, hard would come before medium.

CASE inside aggregates

Put a CASE inside SUM to count only the rows that match a condition. Every matching row adds 1, every other row adds 0:

SELECT guild_id,
       COUNT(*) AS heroes,
       SUM(CASE WHEN gold >= 500 THEN 1 ELSE 0 END) AS rich
FROM heroes
WHERE guild_id IS NOT NULL
GROUP BY guild_id;
guild_id heroes rich
1 3 0
2 3 2
3 3 2
4 3 2

This "conditional counting" puts several counts side by side in one row per group, which is the shape most summary tables need. heroes has the columns id, name, class, level, gold, guild_id and joined.

Common mistakes

  • Forgetting END. Without it, the query is a syntax error.
  • Conditions in the wrong order. If WHEN level >= 10 comes first, Hugo (23) stops there and becomes an adept. Put the strictest condition first.
  • No ELSE 0 inside SUM. SUM(CASE WHEN … THEN 1 END) adds up only NULLs when no row matches, and the result is NULL, not 0.
  • CASE guild_id WHEN NULL … never matches, because nothing equals NULL. Write CASE WHEN guild_id IS NULL THEN ….

Assignment

For every hero class, count how many of its heroes are in each tier:

  • novices: level below 10
  • adepts: level 10 to 19
  • veterans: level 20 or higher

Show the columns class, novices, adepts and veterans, one row per class, sorted by class from A to Z. Every count must be a number, so a tier without heroes shows 0.

Premium lesson

Unlock the full Learn SQL course

  • All 30 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.

Lesson last updated on .

Write code ↓
SQL+20 XP

Lesson 27 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn SQL course

  • All 30 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.