Using GROUP_CONCAT and FIND_IN_SET


  • 0
    T
    SELECT Department.Name 'Department', e1.Name 'Employee', e1.Salary 'Salary'
    FROM
      Employee e1
      JOIN
        (SELECT DepartmentId, GROUP_CONCAT(DISTINCT(Salary) ORDER BY ~Salary) GroupedByDept
         FROM Employee
         GROUP BY DepartmentId) DeptMax
      ON e1.DepartmentId = DeptMax.DepartmentId
        AND FIND_IN_SET(e1.Salary, DeptMax.GroupedByDept) BETWEEN 1 AND 3
      JOIN Department
      ON e1.DepartmentId = Department.Id
    ORDER BY Department.Id, e1.Salary DESC;
    

Log in to reply
 

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