Department Highest Salary - Problem

You're working as a HR Analytics Specialist for a large corporation and need to identify the top earners in each department to help with budget planning and performance reviews.

You have access to two database tables:

  • Employee Table: Contains employee ID, name, salary, and their department ID
  • Department Table: Contains department ID and department name

Your task is to write a SQL query that finds all employees who have the highest salary within their respective departments. Note that multiple employees can have the same highest salary in a department, and you need to return all of them.

Example: If the Engineering department has two employees earning $90,000 (the highest in that department), both should be included in the result.

Input & Output

example_1.sql — Basic Example
$ Input: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 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 | | IT | Max | 90000 | | Sales | Henry | 80000 | +-----------+----------+--------+
šŸ’” Note: In the IT department, both Jim and Max have the highest salary of $90,000, so both are included. In Sales, Henry has the highest salary of $80,000.
example_2.sql — Single Employee Per Department
$ Input: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Alice | 75000 | 1 | | 2 | Bob | 80000 | 2 | | 3 | Carol | 70000 | 3 | +----+-------+--------+--------------+ Department table: +----+-------+ | id | name | +----+-------+ | 1 | HR | | 2 | IT | | 3 | Legal | +----+-------+
› Output: +-----------+----------+--------+ | Department| Employee | Salary | +-----------+----------+--------+ | HR | Alice | 75000 | | IT | Bob | 80000 | | Legal | Carol | 70000 | +-----------+----------+--------+
šŸ’” Note: Each department has only one employee, so each employee is automatically the highest paid in their respective department.
example_3.sql — All Employees Tied
$ Input: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | John | 50000 | 1 | | 2 | Jane | 50000 | 1 | | 3 | Jack | 50000 | 1 | +----+-------+--------+--------------+ Department table: +----+-------+ | id | name | +----+-------+ | 1 | Finance| +----+-------+
› Output: +-----------+----------+--------+ | Department| Employee | Salary | +-----------+----------+--------+ | Finance | John | 50000 | | Finance | Jane | 50000 | | Finance | Jack | 50000 | +-----------+----------+--------+
šŸ’” Note: All employees in Finance have the same salary of $50,000, so all three are tied for the highest salary and should be included in the result.

Time & Space Complexity

Time Complexity
ā±ļø
O(n log n)

Single pass with sorting for window function ranking

n
2n
⚔ Linearithmic
Space Complexity
O(n)

Space for intermediate ranking results

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ Employee table rows ≤ 104
  • 1 ≤ Department table rows ≤ 102
  • Employee.departmentId is a foreign key referencing Department.id
  • All salary values are positive integers
  • Department names are unique and not NULL
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen