Description
Table: Employee
| Column Name | Type |
|---|---|
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
idis the primary key (column with unique values) for this table.departmentIdis a foreign key (reference columns) of the ID from theDepartmenttable.- Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
Table: Department
| Column Name | Type |
|---|---|
| id | int |
| name | varchar |
idis the primary key (column with unique values) for this table. It is guaranteed that department name is notNULL.- Each row of this table indicates the ID of a department and its name.
Problem Statement
Write a solution to find employees who have the highest salary in each of the departments. Return the result table in any order. The result format is in the following example.
Example 1:
Input:
Employeetable:
| id | name | salary | departmentId |
|---|---|---|---|
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
Departmenttable:
| id | name |
|---|---|
| 1 | IT |
| 2 | Sales |
Output:
| Department | Employee | Salary |
|---|---|---|
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
| IT | Max | 90000 |
Explanation: Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
Solution
There are two ways to solve this problem. If you’re comfortable with window operations, you can use window function to solve it in elegant manner. Alternatively, you can get max salary for each department using subquery and then retrieve the employees with highest salary for each department by using results of the subquery.
Approach 1: Using RANK Window Function
The problem can be divided into two parts.
- Find department for each employee. This can be done using JOIN operation with
Departmenttable. The join condition can beEmployee.departmentId = Department.id. - For each department, find the employee with highest salary. This requires that we partition the data by department. Here we also want to order the results by salary in descending order. So, you must include that in the window clause. Once we have partitioned the data per department, you can use
RANKorDENSE_RANKwindow function to assign ranks to each employee based on their salary.
1WITH cte AS (
2 SELECT e.name Employee, e.salary Salary, d.name Department,
3 RANK() OVER (PARTITION BY d.id ORDER BY e.salary DESC) rnk
4 FROM Employee e
5 JOIN Department d
6 ON e.departmentId = d.id
7) SELECT Department, Employee, Salary
8 FROM cte
9 WHERE rnk=1;
Approach 2: Using Aggregation and Subquery
You first join the tables Employee and Department using JOIN opeartion. Next, you can find the highest salary per department using subquery.
1SELECT departmentId, MAX(salary) FROM Employee
2 GROUP BY DepartmentId;
Now, you can filter records where the department and salary matches the results of the subquery.
1SELECT d.name Department,
2 e.name Employee,
3 e.salary Salary
4 FROM Employee e JOIN Department d
5 ON e.departmentId = d.id
6 WHERE (e.departmentId, e.salary) IN (
7 SELECT departmentId, MAX(salary) FROM Employee
8 GROUP BY DepartmentId
9 );


Comments