문제풀이/SQL
184. Department Highest Salary
monawa
2023. 8. 20. 20:05
728x90
Department Highest Salary - LeetCode
Can you solve this real interview question? Department Highest Salary - Table: Employee +--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------+---
leetcode.com
문제
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.
The result format is in the following example.
풀이
SELECT
D.name AS 'Department',
E.name AS 'Employee',
Salary
FROM Employee E JOIN Department D
ON E.departmentId = D.id
WHERE
(E.DepartmentId, Salary) IN
(SELECT DepartmentId, MAX(Salary) FROM Employee Group by DepartmentId)
728x90