You are given two tables: Employee and Department.
The Employee table contains information about employees including their ID, name, salary, and department ID. The Department table contains department information with ID and name.
Write a SQL solution to find employees who have the highest salary in each department. If multiple employees share the highest salary in a department, include all of them.
Return the result table in any order with columns: Department, Employee, and Salary.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, unique employee ID |
name
|
varchar | Employee name |
salary
|
int | Employee salary |
departmentId
|
int | Foreign key referencing Department.id |
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, unique department ID |
name
|
varchar | Department name |
Input & Output
| id | name | salary | departmentId |
|---|---|---|---|
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| id | name |
|---|---|
| 1 | IT |
| 2 | Sales |
| Department | Employee | Salary |
|---|---|---|
| IT | Max | 90000 |
| Sales | Henry | 80000 |
In the IT department, Max has the highest salary (90000). In Sales department, Henry has the highest salary (80000). Each department's top earner is returned.
| id | name | salary | departmentId |
|---|---|---|---|
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 90000 | 1 |
| id | name |
|---|---|
| 1 | IT |
| 2 | Sales |
| Department | Employee | Salary |
|---|---|---|
| IT | Jim | 90000 |
| IT | Sam | 90000 |
| Sales | Henry | 80000 |
In the IT department, both Jim and Sam share the highest salary (90000), so both are included in the result. Henry is the sole highest earner in Sales with 80000.
Constraints
-
1 ≤ Employee.id, Department.id ≤ 100 -
1 ≤ Employee.salary ≤ 100000 -
Employee.nameandDepartment.nameare non-null varchar -
Each
departmentIdreferences a valid department