[코드카타] SQL 57 Department Top Three Salaries

Data_Student·2024년 12월 5일
0

코드카타

목록 보기
67/82

[코드카타] SQL 57 Department Top Three Salaries

57. Department Top Three Salaries
https://leetcode.com/problems/department-top-three-salaries/

A company's executives are interested in seeing who earns the most money 
in each of the company's departments. A high earner in a department is 
an employee who has a salary in the top three unique salaries for that department.
Write a solution to find the employees who are high earners in each 
of the departments.
Return the result table in any order.
with temp as (
    Select d.name Department, e.name Employee, salary Salary, 
    dense_rank() over(partition by e.departmentId order by e.salary desc) rnk
    From Employee e join Department d on e.departmentId=d.id
)
select Department, Employee, Salary
from temp
where rnk <= 3
부서별 Salary가 높은 3개의 값을 가진 이름, 부서, 급여를 확인
상위 3개의 값 중에 중복값이 있기 때문에
window 함수인 dense_rank()를 활용
- Rank() 함수 : 동률 순위가 있을 경우 다음 값은 동률의 수만큼 넘긴 후 진행
  ex) 1, 2, 2, 4, 5
- dense_rank() 함수 : 동률 순위가 있어도 다음 값은 이어서 진행
  ex) 1, 2, 2, 3, 4

0개의 댓글