Medium Window functions and CASE Employees database

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

Press Run to see your query's result, or Check answer to compare it with the expected result.

Expected result

namesalaryband
Tom Becker8800High
Ana Souza6500Medium
Priya Nair9200High
Lucas Moreau6800Medium
Mei Chen7600Medium
Omar Haddad5600Low
Sofia Rossi6100Medium
Jonas Berg5400Low
Aisha Bello4700Low
Diego Ruiz4100Low
Hana Kim7300Medium
Liam Walsh4300Low

Rows can come in any order. Column names do not have to match.

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.

Want to try the query on MySQL or PostgreSQL? Open it in the SQL Online Compiler or SQLite Online.