Skip to main content

Rank Salary by Department

MediumPremium

Given the schema shown below, write a solution to rank each employee based on their salary (highest salary first) within their respective departments. The results should be ordered by department name.

employees projects +---------------+---------+ +---------------+---------+ | id | int |<----+ +->| id | int | | first_name | varchar | | | | title | varchar | | last_name | varchar | | | | start_date | date | | salary | int | | | | end_date | date | | department_id | int |--+ | | | budget | int | +---------------+---------+ | | | +---------------+---------+ | | | departments | | | employees_projects +---------------+---------+ | | | +---------------+---------+ | id | int |<-+ | +--| project_id | int | | name | varchar | +-----| employee_id | int | +---------------+---------+ +---------------+---------+

Your function should return a result in the following format:

department_name | rank | employee_id | first_name | last_name | salary ----------------+------+-------------+------------+-----------+-------- varchar | int | int | varchar | varchar | int