My solution by getting rank first


  • 0
    W
    SELECT
        d.Name AS Department,
        t.Name AS Employee,
        t.Salary
    FROM (
        SELECT 
            a.Name,
            a.DepartmentId,
            a.Salary,
            COUNT(DISTINCT b.Salary) AS rank
        FROM Employee a
        JOIN Employee b
        ON a.DepartmentId = b.DepartmentId
            AND a.Salary <= b.Salary
        GROUP BY 1,2,3    
    ) t
    JOIN Department d
    ON t.DepartmentId = d.Id
    WHERE t.rank <= 3
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.