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


Success.

> If others can write the same codes as mine, coding won't become a difficult thing.

___

Runtime
748 ms

Beats
86.65%


```mysql
# Write your MySQL query statement below
#2022/12/24 09:19

/*
SELECT * FROM
(
    SELECT new_table.Department, new_table.Employee, new_table.Salary 
    FROM  
    (
            SELECT Department.name AS Department, Employee.name AS Employee, Employee.salary AS Salary 
            FROM Department, Employee 
            WHERE Employee.departmentId = Department.id
    ) new_table
    #GROUP BY new_table.Salary
    ORDER BY new_table.Department, new_table.Salary DESC
) new_new_table
;
*/

/*
SELECT * FROM
(
    SELECT new_table.Department, new_table.Employee, new_table.Salary 
    FROM  
    (
            SELECT Department.name AS Department, Employee.name AS Employee, Employee.salary AS Salary,
            ROW_NUMBER() OVER (PARTITION BY Department.name ORDER BY Employee.salary DESC) AS salary_rank
            FROM Department, Employee 
            WHERE Employee.departmentId = Department.id
    ) new_table
    #GROUP BY new_table.Salary
    #ORDER BY new_table.Department, new_table.Salary DESC
    WHERE salary_rank <= 3
) new_new_table
;
*/

SELECT * FROM
(
    SELECT new_table.Department, new_table.Employee, new_table.Salary 
    FROM  
    (
            SELECT Department.name AS Department, Employee.name AS Employee, Employee.salary AS Salary,
            DENSE_RANK() OVER (PARTITION BY Department.name ORDER BY Employee.salary DESC) AS salary_rank
            FROM Department, Employee 
            WHERE Employee.departmentId = Department.id
    ) new_table
    WHERE salary_rank <= 3
) new_new_table
;

#2022/12/24 11:32
```