230208(window x), 230220(window o)
https://leetcode.com/problems/department-highest-salary/description/
내가 이해한 문제:
각 부서별로 가장 높은 급여를 받는 직원 뽑기
주의) 가장 높은 급여를 받는 직원이 2명 이상일 수도 있음
원본:
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.
SELECT d.name Department
, e.name Employee
, e.salary
FROM Employee e, Department d
WHERE e.departmentId = d.id AND
(DepartmentId, salary) IN (
SELECT DepartmentId, MAX(salary)
FROM Employee
GROUP BY DepartmentId
)
이렇게 푸는게 과연 최선일까? 하고 고민하다가 냈는데 다른 사람들도 이와 비슷하게 풀어서 다행이다 싶었다.
문제는 한 문제를 푸는데 굉장히 오랜 시간이 걸린다는 것이다.
내가 생각했을 때 원인은 SQL 문제를 굉장히 오랜만에 풀어서 문제 푸는 감각이 다 떨어진 듯하다. 꾸준함이 필요하다...
SELECT d.name Department
, e.name Employee
, e.salary
FROM Employee e
-- 1.Employee 테이블에서 조건 찾은 후
INNER JOIN (
--부서에서 가장 많이 벌 때에 그 임금과 부서id
SELECT DepartmentId, MAX(salary) max_salary
FROM Employee
GROUP BY DepartmentId
) dh ON e.departmentId = dh.departmentId
AND e.salary = dh.max_salary
-- 2. Department 테이블과 join
INNER JOIN Department d on d.id = e.departmentId
INNER JOIN을 쓸 때에도 서브쿼리 형태로 작성할 수 있구나!
SELECT Department
, Employee
, Salary
FROM (
SELECT d.name Department
, e.name Employee
, e.salary Salary
, Max(e.salary) OVER (PARTITION BY d.name) MaxSalary
FROM Employee e, Department d
WHERE e.departmentId = d.id
) s
WHERE Salary = MaxSalary
window 함수를 이용해 MaxSalary 열을 만든 테이블을 가져와 WHERE 조건에 충족하는 행만 추출하기
문제 풀이 확인 결과, 나랑 비슷하다.
SELECT Department
, Employee
, Salary
FROM (
SELECT d.name Department
, e.name Employee
, e.salary Salary
, RANK() OVER (PARTITION BY d.name ORDER BY e.salary DESC) ran
FROM Employee e, Department d
WHERE e.departmentId = d.id
) s
WHERE ran = 1
이런 식으로 MAX() 부분을 RANK()로 바꾸면 된다.
만약 DENSE_RANK()를 이용하려면 RANK -> DENSE_RANK로만 하면 돌아간다.
+)
from 절 서브쿼리 출력시,
Department | Employee | Salary | ran |
---|---|---|---|
IT | Jim | 90000 | 1 |
IT | Max | 90000 | 1 |
IT | Joe | 70000 | 3 |
Sales | Henry | 80000 | 1 |
Sales | Sam | 60000 | 2 |
이렇게 되기 때문에 WHERE에 ran = 1인 것만 뽑으면 된다.