Top Salaries by Department
HardUnlock detailed company stats for this questionUpgrade
Given the database with the schema shown below, write a SQL query to fetch the top earning employee by department, 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 query should return a result in the following format:
department_name | employee_id | first_name | last_name | salary ----------------+-------------+------------+-----------+-------- varchar | int | varchar | varchar | int
Our solution will involve using the window function RANK() to find the highest salary per department and an inner join to return employees with their associated departments. Here's our full solution:
Related courses

Course

Course
