My 2 layer solution


  • 0
    R

    2 steps:

    1. create a table R that contains the the DepartmentId, Employee's name and salary of the employees who have the highest salary in each department.
    2. Join R and Department by DepartmentId.
    SELECT Department.Name AS Department, R.Employee, R.Salary
    
    FROM 
    Department,
    (
        SELECT t.DepartmentId, E.Name AS Employee, t.NewSalary AS Salary
        FROM 
         Employee E,
         (SELECT DepartmentId, max(Salary) AS NewSalary FROM Employee GROUP BY DepartmentId) t
        WHERE 
         E.DepartmentId = t.DepartmentId AND E.Salary = t.NewSalary
    ) R 
    
    WHERE Department.Id = R.DepartmentId
    
    

Log in to reply
 

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