Premium problem31. Top Three Salaries per Department

Medium Locked

employees(employee_id, employee_name, department_id, salary)

Return every employee whose salary is among the top three distinct salaries in their department.

Return department_id, employee_id, employee_name and salary, ordered by department, then salary descending, then employee id.

Distinct salaries, so two people on the same salary occupy one rank between them and both appear.

Tables

employees

employee_id  employee_name  department_id  salary
-----------  -------------  -------------  ---------
1            Ada            10             150000.00
2            Bo             10             120000.00
3            Cy             10             120000.00
4            Di             10             90000.00
5            Eli            10             70000.00
6            Fay            20             50000.00
7            Gus            20             40000.00

Expected result

department_id  employee_id  employee_name  salary
-------------  -----------  -------------  ---------
10             1            Ada            150000.00
10             2            Bo             120000.00
10             3            Cy             120000.00
10             4            Di             90000.00
20             6            Fay            50000.00
20             7            Gus            40000.00

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query