Select, Filter and Sort · Problem 13 of 118

Account Tiers

MediumSelect, Filter and SortBank data

Customer service groups accounts into tiers by balance:

balance_cents tier
below 50,000 'low'
50,000 up to (but not including) 200,000 'medium'
200,000 or more 'high'

For the accounts with account_id from 1 to 12, return account_id, balance_cents and the tier, sorted by account_id.

Hint 1

CASE WHEN condition THEN value ... ELSE value END goes in the select list. Conditions are tested from the top, and the first true one wins.

Reference solution

SELECT account_id,
       balance_cents,
       CASE
         WHEN balance_cents < 50000 THEN 'low'
         WHEN balance_cents < 200000 THEN 'medium'
         ELSE 'high'
       END AS tier
FROM accounts
WHERE account_id BETWEEN 1 AND 12
ORDER BY account_id;

CASE checks its WHEN conditions from top to bottom and returns the value of the first one that is true. Because of that order, the second test only needs < 200000: anything below 50,000 was already caught. In the wrong version the second condition >= 50000 is also true for the rich accounts, so none of them is ever labelled 'high'.