Finally it worked but I almost got lost


  • 0
    X

    It worked...but it is super confusing...

    SELECT e3.Department, e2.Name AS Employee, e2.Salary
    
    FROM (
    SELECT d.Name AS Department, e1.Salary AS Salary, e1.DepartmentId
    FROM Department d
    JOIN
    (
     SELECT MAX(e.Salary) AS Salary, e.DepartmentId
     FROM Employee e
     GROUP BY e.DepartmentId
    ) e1
     
     ON e1.DepartmentId = d.Id
              ) e3
    
    LEFT JOIN Employee e2
    
    ON e2.Salary = e3.Salary
    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.