Easy Solution. No joins. GROUP BY is enough. 916ms


  • 7
    R
    select
    d.Name, e.Name, e.Salary
    from
    Department d,
    Employee e,
    (select MAX(Salary) as Salary,  DepartmentId as DepartmentId from Employee GROUP BY DepartmentId) h
    where
    e.Salary = h.Salary and
    e.DepartmentId = h.DepartmentId and
    e.DepartmentId = d.Id;

  • 7
    P

    Hey,

    What do you mean by no JOIN? the comma separated tables represents a CROSS JOIN, while in your case it will be an equivalent to INNER JOIN as you are using the WHERE clause:

    https://en.wikipedia.org/wiki/Join_(SQL)#Cross_join

    Did I understand something wrong?

    Thank you!


  • 0
    K

    agree .
    It's join operator but doesn't use the join method in sql .


  • 0
    J
    SELECT (SELECT Name FROM Department WHERE Id = E.DepartmentId) AS Department, 
            E.Name AS Employee, 
            E.Salary AS Salary 
    FROM Employee AS E
    WHERE 
        E.Salary >= ALL(SELECT Salary FROM Employee WHERE DepartmentId = E.DepartmentId)
        AND EXISTS (SELECT * FROM Department WHERE Id = E.DepartmentId);
    

Log in to reply
 

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