Watch 10 video solutions for Department Highest Salary, a medium level problem involving Database. This walkthrough by Frederik Müller has 15,937 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference columns) of the ID from the Department table.
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 |
+-------------+---------+
id is the primary key (column with unique values) for this table. It is guaranteed that department name is not NULL.
Each row of this table indicates the ID of a department and its name.
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: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Jim | 90000 | 1 | | 3 | Henry | 80000 | 2 | | 4 | Sam | 60000 | 2 | | 5 | Max | 90000 | 1 | +----+-------+--------+--------------+ Department table: +----+-------+ | 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.
Problem Overview: Given Employee and Department tables, return the employees who earn the highest salary within each department. The result should include the department name, employee name, and salary.
Approach 1: SQL Group By and Join (Time: O(n log n), Space: O(d))
This approach first computes the maximum salary for each department using GROUP BY. A sub-result like SELECT departmentId, MAX(salary) identifies the top salary per department. Then you JOIN this result back to the Employee table to retrieve employees whose salary matches the department maximum. Finally, join with the Department table to get department names. This approach is straightforward and commonly used in SQL interview questions involving grouped aggregates.
Approach 2: Nested Queries with Join (Time: O(n log n), Space: O(d))
A nested query calculates the highest salary per department inside a derived table. That derived table is then joined with the Employee table to filter matching records. The structure typically looks like a join between Employee and a subquery that returns departmentId with MAX(salary). Compared to the first method, this version simply embeds the aggregation logic directly inside the join statement. It's useful when writing compact SQL queries and appears frequently in database interview problems.
Approach 3: SQL Subquery Approach (Time: O(n log n), Space: O(1))
This solution filters rows using a correlated subquery. For each employee row, a subquery calculates the maximum salary within that employee's department. The query keeps rows where salary = (SELECT MAX(salary) ...). The advantage is readability: the condition directly expresses the business rule. However, correlated subqueries may execute repeatedly depending on the optimizer, which can make them less efficient on very large datasets.
Approach 4: SQL Window Functions Approach (Time: O(n log n), Space: O(n))
Window functions provide a clean and modern solution. Use RANK() or DENSE_RANK() with PARTITION BY departmentId ORDER BY salary DESC. This ranks employees within each department by salary. After ranking, filter rows where the rank equals 1 to get the top earners. Window functions avoid additional joins or subqueries and make the intent very clear. Many production SQL systems prefer this pattern, especially when working with analytical queries using window functions.
Recommended for interviews: The GROUP BY + JOIN solution is the most commonly expected answer because it demonstrates strong understanding of aggregation and relational joins. Window functions are equally valid and often considered the cleanest modern SQL approach. Showing both indicates strong SQL proficiency.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Group By and Join | O(n log n) | O(d) | Most common SQL interview solution when aggregating max values per group |
| Nested Queries with Join | O(n log n) | O(d) | Compact query structure when combining aggregation results directly in joins |
| Subquery Approach | O(n log n) | O(1) | Readable solution using correlated subqueries for filtering rows |
| Window Functions | O(n log n) | O(n) | Best modern SQL approach when window functions like RANK or DENSE_RANK are supported |