Skip to content

Commit 22a46e2

Browse files
Added Department Highest Salary
1 parent e4bb55c commit 22a46e2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Department Highest Salary
3+
https://leetcode.com/problems/department-highest-salary/
4+
5+
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
6+
7+
+----+-------+--------+--------------+
8+
| Id | Name | Salary | DepartmentId |
9+
+----+-------+--------+--------------+
10+
| 1 | Joe | 70000 | 1 |
11+
| 2 | Jim | 90000 | 1 |
12+
| 3 | Henry | 80000 | 2 |
13+
| 4 | Sam | 60000 | 2 |
14+
| 5 | Max | 90000 | 1 |
15+
+----+-------+--------+--------------+
16+
The Department table holds all departments of the company.
17+
18+
+----+----------+
19+
| Id | Name |
20+
+----+----------+
21+
| 1 | IT |
22+
| 2 | Sales |
23+
+----+----------+
24+
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).
25+
26+
+------------+----------+--------+
27+
| Department | Employee | Salary |
28+
+------------+----------+--------+
29+
| IT | Max | 90000 |
30+
| IT | Jim | 90000 |
31+
| Sales | Henry | 80000 |
32+
+------------+----------+--------+
33+
Explanation:
34+
35+
Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
36+
*/
37+
38+
SELECT DepartmentName AS Department, Employee.Name as Employee, Employee.Salary
39+
FROM Employee JOIN (
40+
SELECT Department.Name AS DepartmentName, Department.Id AS DepartmentId, Max(Salary) as Salary
41+
FROM Employee JOIN Department ON Employee.DepartmentId = Department.Id
42+
GROUP BY Employee.DepartmentId
43+
) AS t ON Employee.Salary = t.Salary AND Employee.DepartmentId = t.DepartmentId

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ To run a specific problem in your console, go to the file test, add the call to
8585
| [Second Highest Salary](/LeetcodeProblems/Databases/Second_highest_salary.sql)| Easy | https://leetcode.com/problems/second-highest-salary |
8686
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |
8787
| [Customers Who Never Order](/LeetcodeProblems/Databases/Customers_Who_Never_Order.sql)| Easy | https://leetcode.com/problems/customers-who-never-order |
88+
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary/ |
89+
8890

8991
### UtilsClasses
9092

0 commit comments

Comments
 (0)