Skip to content
Advertisement

SQL query to display Count of records based in range from another table

I have two tables Range and Employee.

Range table consists of columns

minimum_salary, maximum salary, program

(Available programs for that salary like platinum and silver etc).

Employee table has columns

Emp_Id, Emp_Name, Salary

https://i.stack.imgur.com/RKaXP.png

enter image description here

I want to write a SQL query in such a way that it gets the count of employees who fall under the given salary range in range table, please refer screenshot for tables and result set.

Any help is appreciated. Thanks

Advertisement

Answer

You can join on inequality conditions:

select r.program, count(e.emp_id) no_employees
from ranges r
left join employees e on e.salary between r.min_salary and r.max_salary
group by r.program
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement