My ugly solution

  • 0
    SELECT d.Name AS Department, e2.Name AS Employee, e2.Salary
    FROM Employee e2, Department d,
    ( SELECT DISTINCT(e1.Salary), e1.DepartmentId
      FROM Employee e1
      WHERE (
             SELECT COUNT(DISTINCT(e2.Salary)) FROM Employee e2
             WHERE e2.Salary >= e1.Salary AND e2.DepartmentId = e1.DepartmentId
             ) <= 3
     ) e3
    WHERE e2.Salary = e3.Salary
    AND e2.DepartmentId = d.Id
    AND e2.DepartmentId = e3.DepartmentId;

Log in to reply

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