Department Highest Salary
Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

The Department table holds all departments of the company.

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

Solution :


Since the Employee table contains the Salary and DepartmentId information, we can query the highest salary in a department.

Note: There might be multiple employees having the same highest salary, so it is safe not to include the employee name information in this query.

Then, we can join table Employee and Department, and query the (DepartmentId, Salary) are in the temp table using IN statement as below.

Optimized: using Inner Join