Im between cherry

Leet Code | MySQL | 185. Department Top Three Salaries 본문

데이터분석/practice_query

Leet Code | MySQL | 185. Department Top Three Salaries

meal 2020. 8. 30. 15:30

185. Department Top Three Salaries

 

https://leetcode.com/problems/department-top-three-salaries/

 

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

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows (order of rows does not matter).

 

# Write your MySQL query statement below
/* RANK(), DENSE_RANK() */
SELECT t.department
    , t.employee
    , t.salary
FROM(
    SELECT department.name AS department
        , employee.name AS employee
        , employee.salary AS salary
        , DENSE_RANK() OVER (PARTITION BY departmentid ORDER BY salary DESC) AS dr
    FROM employee
        INNER JOIN department ON employee.departmentid = department.id
    )t
WHERE t.dr <= 3

 

Comments