순간을 성실히, 화려함보단 꾸준함을

184. Department Highest Salary 본문

알고리즘,SQL/leetcode

184. Department Highest Salary

폭발토끼 2021. 12. 4. 18:07

https://leetcode.com/problems/department-highest-salary/

 

Department Highest Salary - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1) Employee 테이블에서 departmentId 별로 group by 를 실행해준다.

 

2) group by 한 테이블에서 max(salary) 와 departmentId 값을 뽑아서 온다.

 

3) 새로운 Employee 테이블과 max(salary) 를 뽑아가지고 온 테이블을 wherer 절로 

E1.salary = E2.salary AND E1.departmentId = E2.departmentId 를 해준다.

 

그러면 각 그룹별로 salary가 최대인 사원들이 '다수' 뽑혀서 나온다.

 

4) Department 테이블에서 id 와 위에서 처리한 테이블의 departmentId 를 비교해 줘서 같은 애들만 출력해 준다.

 

/* Write your PL/SQL query statement below */
select D.name Department,E.name Employee,E.salary Salary
from Department D join (
    select E1.departmentId,E1.name,E2.salary
    from Employee E1,(
        select departmentId,max(salary) salary
        from Employee
        group by departmentId
    )E2
    where E1.salary=E2.salary and E1.departmentId=E2.departmentId
)E on D.id=E.departmentId;