-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path184. Department Highest Salary.sql
More file actions
49 lines (40 loc) · 1.36 KB
/
184. Department Highest Salary.sql
File metadata and controls
49 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Question 184. Department Highest Salary
Link: https://leetcode.com/problems/department-highest-salary/description/
Table: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference columns) 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 with unique values) for this table. It is guaranteed that department name is not NULL.
Each row of this table indicates the ID of a department and its name.
Write a solution to find employees who have the highest salary in each of the departments.
Return the result table in any order.
*/
SELECT
d.name AS Department,
e.name AS Employee,
e.salary
FROM Employee AS e
LEFT JOIN
Department AS d
ON e.departmentId = d.id
WHERE e.salary = (
SELECT MAX(em.salary)
FROM Employee AS em
WHERE em.departmentId = e.departmentId
)