Self join, the idea is similar to most solution but faster


  • 0
    Y
    select 
    d.name Department
    , b.name Employee
    , b.salary
    from 
    (
    select e1.name
    , e1.salary
    , e1.DepartmentId
    , count(distinct(e2.salary)) cnt
    from Employee e1
    inner join Employee e2
    on e1.DepartmentId=e2.DepartmentId
    and e1.salary <= e2.salary
    group by e1.name
    , e1.salary
    , e1.DepartmentId
    ) b
    inner join Department d
    on b.DepartmentId=d.Id
    where b.cnt<4
    ;

Log in to reply
 

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