Query with 5 variables beats 100% of submissions (660 ms)


  • 0
    Y

    SELECT Department, Employee, Salary
    FROM(
    SELECT Department, Employee, Salary, @preSalary:=@curSalary, @curSalary:=Salary,@preDept:=@curDept, @curDept:=DepartmentId, @deptRank:=IF(@preDept=@curDept,IF(@curSalary=@preSalary,@deptRank,@deptRank+1),1) AS DeptRank
    FROM (
    SELECT e.Id, e.Name AS Employee, e.Salary, e.DepartmentId, d.Name AS Department
    FROM Employee e LEFT JOIN Department d
    ON e.DepartmentId=d.Id
    WHERE d.Name IS NOT NULL
    ORDER BY e.DepartmentId, e.Salary DESC
    ) AS T1, (SELECT @curSalary:=NULL,@preSalary:=NULL,@curDept:=NULL,@preDept:=NULL,@deptRank:=0) AS Var
    )AS T2
    WHERE DeptRank<4;


Log in to reply
 

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