My Solution

  • 0

    Here is my solution. It executed in around 668ms.

    SELECT Department, Employee, Salary
    SELECT  Dep.Name AS Department,
            Emp.Name AS Employee,
            Emp.Salary AS Salary,
            CASE    WHEN Dep.Name = @prev AND Emp.Salary <> @prev_sal THEN @rw := @rw + 1
                    WHEN Dep.Name = @prev AND Emp.Salary = @prev_sal THEN @rw
                    ELSE @rw := 1 
            END AS Rank,
            @prev := Dep.Name,
            @prev_sal := Emp.Salary
            (SELECT * FROM Employee ORDER BY DepartmentId, Salary DESC )  Emp,
            Department Dep,
            (SELECT @rw:=0, @prev:=NULL, @prev_sal:=NULL) r
            Emp.DepartmentId = Dep.ID
    ) bb
    WHERE Rank < 4;

Log in to reply

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