Can someone tell me where I'm going wrong?

  • 0

    I would appreciate if anyone can help point out where this code in wrong:-

    select Department, Employee, Salary, Rank from
          Department.Name as Department,
          Employee.Name as Employee,
          @rank := if (DepartmentId = @prevDeptId, @rank + 1, 1) as rank,
         (@prevDeptId := DepartmentId)
    from (select @rank:=0, @prevDeptId:=NULL) vars, Employee
    JOIN Department on Employee.DepartmentId = Department.Id
    order by departmentId asc, salary asc 
    ) q 
    where q.rank < 4

  • 0

    Hi Rohan,

    I worked on this problem.

    Pls try this-->

    select d.Name as Department, computed.Name as Employee, computed.Salary as Salary
    from (
    select Name, Salary, DepartmentId,

    (CASE WHEN ((DepartmentId=@did) and (Salary=@Salary)) THEN @row :=@row
    WHEN (DepartmentId=@did) THEN @row :=(@row +1)
    ELSE @row :=1
    as Rank ,
    @Salary:= Salary
    from (
    select Name, Salary, DepartmentId
    from Employee
    order by DepartmentId, Salary desc
    ) ordered, (select @row:=0, @did:=0 , @Salary:=0) variables
    ) computed
    join Department d
    on computed.DepartmentId=d.Id
    where computed.Rank<=3

    The above soln worked for me .
    The issue with ur solution is that it does not take into account when in a dept 2 or more people have same salary and they all are in top 3 bracket salaried people.

Log in to reply

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