×

sql · level 2 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

CASE stops at the first WHEN that passes.

A CASE can hold many WHENs. SQLite tries them from the top down and stops at the first one that passes. The tests after it are never looked at.

That makes bands tidy to write. Test the top first, WHEN points >= 100, then WHEN points >= 50, then ELSE. A score of 120 stops at the first test, so it never reaches the second.

Put the tests the other way round and 120 stops at >= 50 instead. Order your WHENs from the top band down.

SELECT player, game, points,
  CASE
    WHEN points >= 100 THEN 'gold'
    WHEN points >= 50 THEN 'silver'
    ELSE 'bronze'
  END AS medal
FROM scores
ORDER BY points DESC;

Group by a CASE alias, like GROUP BY medal, to count how many rows land in each band.