My Solution


  • 0
    K
    SELECT Department, Employee, Salary
    FROM
    (
    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
    FROM
            (SELECT * FROM Employee ORDER BY DepartmentId, Salary DESC )  Emp,
            Department Dep,
            (SELECT @rw:=0, @prev:=NULL, @prev_sal:=NULL) r
    WHERE
            Emp.DepartmentId = Dep.ID
    ) bb
    WHERE Rank = 1;
    

Log in to reply
 

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