How can I write a SQL query for categorizing the salary of employees in SQL?

132    Asked by DanielBAKER in SQL Server , Asked on Jan 17, 2024

 I am assigned a task which is related to analyzing data in SQL. In this task, there is a table whose name is “employees” with the following columns: “employee_id” “employee_name” and “salary”. How can I write an SQL query for categorizing employees' salaries based on the categories of low, medium, and high? Here are the following criteria:

  • If the salary is less than 30000 then it should be counted as low

  • If the salary is between 30000 and 60000 then it should be counted as medium

  • If the salary is above 60000 then it should be categorized as high. 

The query should retrieve the “employee_name” and their respective category.

Answered by Bernadette Bond

In the context of SQL, you can achieve your objective by using the multiple CASE WHEN function in SQL.

Here is the SQL query for getting the specified categorization:

SELECT 
    Employee_name,
    CASE
        WHEN salary < 30000> 60000 THEN ‘High’
    END AS salary_category
FROM

    Employees;

This above query will certainly use the CASE WHEN statement for categorizing the salary of employees based on the provided criteria as low, medium and high. The results would include the “employee name” and their respective “salary_category”.



Your Answer

Interviews

Parent Categories