Label rows with CASE WHEN
Question
Show each employee's name, salary and a salary band - 'High' for 8000 or more, 'Medium' for 6000 to 7999, and 'Low' for the rest.
employeesid, name, salary, department_id, manager_id, hired_on
departmentsid, name, city
Your query
Ctrl+Enter to run
Result
Expected result
| name | salary | band |
|---|---|---|
| Tom Becker | 8800 | High |
| Ana Souza | 6500 | Medium |
| Priya Nair | 9200 | High |
| Lucas Moreau | 6800 | Medium |
| Mei Chen | 7600 | Medium |
| Omar Haddad | 5600 | Low |
| Sofia Rossi | 6100 | Medium |
| Jonas Berg | 5400 | Low |
| Aisha Bello | 4700 | Low |
| Diego Ruiz | 4100 | Low |
| Hana Kim | 7300 | Medium |
| Liam Walsh | 4300 | Low |
Hint
CASE checks the conditions from top to bottom and returns the first match.
Show the solution
solution.sql
Run this code
SELECT name, salary,
CASE
WHEN salary >= 8000 THEN 'High'
WHEN salary >= 6000 THEN 'Medium'
ELSE 'Low'
END AS band
FROM employees;
Why it works
CASE is SQL's if / else. Because the first true WHEN wins, the second condition only needs salary >= 6000; anyone at 8000 or more has already been labelled High.
Common mistake
Writing the conditions in the opposite order (>= 6000 first) labels everyone above 6000 as Medium, because the first match wins.