[LeetCode/SQL] 184. Department Highest Salary

Sooyeon·2024년 1월 11일
0

문제풀이 

목록 보기
74/95
post-thumbnail

[LeetCode/SQL]


📌 184. Department Highest Salary

문제

풀이

부서별 가장 높은 연봉을 가진 직원을 찾아서 출력하라. 

방법 I

SELECT d.name AS Department
      ,e.name AS Employee
      ,e.salary AS Salary
FROM Department AS d,Employee AS e
WHERE e.departmentId=d.id  AND (e.departmentId,salary) IN
      (SELECT departmentId,MAX(salary)
       FROM Employee
       GROUP BY departmentId) 

방법 II

SELECT d.name AS Department
       ,e.name AS Employee
       ,Salary
FROM (
       SELECT *,MAX(salary) OVER (PARTITION BY DepartmentId) AS max_val
        FROM Employee) AS e
JOIN Department AS d
ON e.DepartmentId=d.Id
WHERE salary=max_val

0개의 댓글