SQL CASE WHEN: How to Categorize Employee Salaries into Low, Medium, and High
91 SQL Challenge: Can You Use CASE WHEN to Group Employees by Salary?
Imagine your manager asks you to divide employees into three groups based on their salary.
Employees earning less than ₹40,000 should be in the Low category.
Those earning between ₹40,000 and ₹80,000 should be in Medium.
Anyone earning more than ₹80,000 should be in High.
Sounds easy, right?
But how would you write this using a SQL CASE expression?
Let's look at the data.
employees
emp_id| name| department| salary
101| Amit| IT| 35000
102| Priya| IT| 40000
103| Rahul| IT| 55000
104| Neha| HR| 80000
105| Sneha| HR| 85000
106| Kiran| HR| 95000
107| Meera| Finance| 75000
108| Arjun| Finance| 55000
Your challenge
Display employee name, salary and salary category using CASE WHEN.
Which query would you choose?
Option A
SELECT name, salary,
CASE
WHEN salary < 40000 THEN 'Low'
WHEN salary <= 80000 THEN 'Medium'
ELSE 'High'
END AS category
FROM employees;
Option B
SELECT name, salary,
CASE
WHEN salary <= 40000 THEN 'Low'
WHEN salary < 80000 THEN 'Medium'
ELSE 'High'
END AS category
FROM employees;
Option C
SELECT name, salary,
CASE
WHEN salary < 40000 THEN 'Low'
WHEN salary > 40000 THEN 'Medium'
ELSE 'High'
END AS category
FROM employees;
SQL Solution
Option A is correct.
SELECT name, salary,
CASE
WHEN salary < 40000 THEN 'Low'
WHEN salary <= 80000 THEN 'Medium'
ELSE 'High'
END AS category
FROM employees;
Output
name| salary| category
Amit| 35000| Low
Priya| 40000| Medium
Rahul| 55000| Medium
Neha| 80000| Medium
Sneha| 85000| High
Kiran| 95000| High
Meera| 75000| Medium
Arjun| 55000| Medium
Why does this work?
SQL checks each WHEN condition from top to bottom.
If salary is less than 40000, the result is Low.
If salary is up to 80000 and the first condition is not true, the result is Medium.
If neither condition matches, ELSE returns High.
One important thing to remember: CASE stops checking once it finds a matching condition.
This is why the order of WHEN conditions matters.
Your turn
Can you use CASE WHEN with GROUP BY to count how many employees belong to each salary category?
Would you use CASE WHEN for similar business reports?
Share your query in the comments.
Want to learn more SQL with practical example
