Department Highest Salary LeetCode Solution

Problem – Department Highest Salary LeetCode Solution

SQL Schema

Table: Employee

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| id           | int     |
| name         | varchar |
| salary       | int     |
| departmentId | int     |
+--------------+---------+
id is the primary key column for this table.
departmentId is a foreign key 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 for this table.
Each row of this table indicates the ID of a department and its name.

Write an SQL query to find employees who have the highest salary in each of the departments.

Return the result table in any order.

The query 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.

Department Highest Salary LeetCode Solution in MySQL

SELECT b.Name AS Department, a.Name AS Employee, Salary FROM
(SELECT *, MAX(Salary) OVER(PARTITION BY DepartmentId) AS max_val
FROM Employee) a
JOIN Department b
ON a.DepartmentId = b.Id
WHERE Salary = max_val;

Department Highest Salary LeetCode Solution in MS SQL Server

with max_salary as (select e.DepartmentId, max(Salary) Salary 
from Employee as e 
group by e.DepartmentId)

select d.Name Department, e.Name Employee, e.Salary
from max_salary join Employee e on e.DepartmentId = max_salary.DepartmentId and max_salary.Salary = e.Salary
join Department d
on e.DepartmentId = d.id 

Department Highest Salary LeetCode Solution in Oracle

select d.Name as Department, e.Name as Employee, e.Salary
from Department d inner join Employee e on d.Id = e.DepartmentId
where (e.Salary, e.DepartmentId)  in (select max(Salary), DepartmentId from Employee group by DepartmentId)
Department Highest Salary LeetCode Solution Review:

In our experience, we suggest you solve this Department Highest Salary LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Department Highest Salary LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Department Highest Salary LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.