Leetcode Problem 185. Department Top Three Salaries

185. Department Top Three Salaries

Leetcode Solutions

Using DENSE_RANK() to Find Top Three Salaries in Each Department

  1. Use a Common Table Expression (CTE) or a subquery to select all employees along with their department information.
  2. Apply the DENSE_RANK() function over the partition of the department, ordering by salary in descending order.
  3. Assign the rank to each employee based on their salary within their department.
  4. From the ranked data, filter out employees who have a rank greater than 3, as we are only interested in the top three unique salaries.
  5. Select the department name, employee name, and salary from the filtered results to match the desired output format.
erDiagram
    Employee ||--o{ Department : belongs
    Employee {
        int id PK
        varchar name
        int salary
        int departmentId FK
    }
    Department {
        int id PK
        varchar name
    }

Using Correlated Subquery to Find Top Three Salaries in Each Department

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...