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.
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
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code