[LeetCode-HARD] Department Top Three Salaries 부서의 top3 급여 구하기

LeetCode - 185. Department Top Three Salaries

Difficulty 분류 : Hard

My Solution

WITH a as (
    SELECT 
        e.name AS Employee,
        e.salary AS Salary,
        d.name AS Department
    FROM Employee e 
    JOIN Department d on e.departmentId = d.id
),
b as (
    SELECT 
        *, 
        /* DENSE_RANK(), RANK() 차이 이해 필요 */
        DENSE_RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS rk
    FROM a 
)
SELECT 
    Department,
    Employee,
    Salary
FROM b
WHERE rk<=3