Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Department Highest Salary problem solution

YASH PAL, 31 July 2024

In this Leetcode Department Highest Salary problem solution, The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. The Department table holds all departments of the company.

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter).

+————+———-+——–+

| Department | Employee | Salary |

+————+———-+——–+

| IT         | Max      | 90000  |

| IT         | Jim      | 90000  |

| Sales      | Henry    | 80000  |

+————+———-+——–+

Leetcode Department Highest Salary problem solution

Topics we are covering

Toggle
  • Problem solution in Oracle.
  • Problem solution in Mysql.
  • Problem solution in C++.

Problem solution in Oracle.

select d.Name as Department, e.NAME as Employee, e.SALARY as Salary  from Employee e 
inner join Department d
on e.DepartmentId=d.Id
inner join
(
select   
DepartmentId ,
max(Salary) as Salary
from Employee 
group by  DepartmentId
) a
on e.Salary=a.Salary and e.DepartmentId=a.DepartmentId

Problem solution in Mysql.

SELECT d.Department, e.Name as Employee, e.Salary
FROM (
    SELECT d.Name AS Department, d.Id AS dId, MAX(e.Salary) AS Salary
    FROM Employee e
    LEFT JOIN Department d
    ON e.DepartmentID = d.Id
    GROUP BY Department, dId
    ) d
INNER JOIN Employee e
ON d.Salary = e.Salary
AND d.dId = e.DepartmentId

Problem solution in C++.

select c.Name as Department, a.Name as Employee, a.Salary as Salary from (
select * from Employee
) a
inner join (
select max(Salary) as Salary, DepartmentId from Employee group by DepartmentId
) b on a.Salary = b.Salary and a.DepartmentId = b.DepartmentId
left join (
select * from Department
) c on a.DepartmentId = c.Id where c.Name is not null

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes