20. Employees with the Highest Salary in Each Department

We have two tables: Employee and Department.

Table: Employee

╔══════════════╦═════════╗
ā•‘ Column Name  ā•‘  Type   ā•‘
╠══════════════╬═════════╣
ā•‘      id      ā•‘   int   ā•‘
║──────────────┼─────────║
ā•‘     name     ā•‘ varchar ā•‘
║──────────────┼─────────║
ā•‘    salary    ā•‘   int   ā•‘
║──────────────┼─────────║
ā•‘ departmentId ā•‘   int   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • id is the primary key for the Employee table.
  • departmentId is a foreign key that references the id of the Department table.
  • Each row in this table represents an employee's id, name, salary, and the id of the department they belong to.

Table: Department

╔═════════════╦═════════╗
ā•‘ Column Name ā•‘  Type   ā•‘
╠═════════════╬═════════╣
ā•‘     id      ā•‘   int   ā•‘
║─────────────┼─────────║
ā•‘    name     ā•‘ varchar ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • id is the primary key for the Department table. It is guaranteed that the department name is not NULL.
  • Each row in this table contains the id and name of a department.

Task:

Your task is to find the employees who have the highest salary in each department. The result should be ordered by salary in descending order. If two or more employees have the same salary, order them by employee name in ascending order.

Example 1:

Example

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  ā•‘
║────────────┼──────────┼────────║
ā•‘     IT     ā•‘   Max    ā•‘ 90000  ā•‘
║────────────┼──────────┼────────║
ā•‘   Sales    ā•‘  Henry   ā•‘ 80000  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

For the IT department, both Jim and Max have the highest salary of 90000.

For the Sales department, Henry has the highest salary of 80000.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Department
Employee