Menu

SQL CASE WHEN: How to Categorize Employee Salaries into Low, Medium, and High
📰

SQL CASE WHEN: How to Categorize Employee Salaries into Low, Medium, and High

DEV Community: database·Kailas Warade·10 days ago
#T7TkY2TD
Reading 0:00
15s threshold

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…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.