본문 바로가기
데이터 분석/SQL 연습문제

185. Department Top Three Salaries (LeetCode)

2022. 9. 26.
 

Department Top Three Salaries - 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

high earner in a department is an employee who has a salary in the top three unique salaries for that department. Write an SQL query to find the employees who are high earners in each of the departments.

풀이

제출한 답

with sub as(
    select 
        d.name department, 
        e.name employee, 
        e.salary salary, 
        dense_rank() over (partition by d.id order by salary desc) as rn
    from employee as e join department as d on e.departmentId = d.id)
    
select department, employee, salary
from sub
where rn <= 3

-> dense_rank로 중복 포함 순위 구하기

2022.09.26

댓글