Managers with at Least 5 Direct Reports


  • 0

    Click here to see the full article post


  • 0
    C

    A simpler solution:
    select e1.Name
    from Employee e1 join Employee e2
    on e1.Id = e2.ManagerId
    group by e1.Id
    having count(e1.Id) >= 5;


  • 0
    L

    select r.name from employee l join employee r on l.managerid = r.id group by l.managerid having count(1) >= 5;


  • 0
    W

    Using Subquery is faster.

    SELECT
        Name
    FROM
        Employee WHERE Employee.Id IN
        (SELECT
            ManagerId
        FROM
            Employee
        GROUP BY ManagerId
        HAVING COUNT(ManagerId) >= 5)
    ;
    

  • 0
    S
    select Name
    from Employee 
    Where
    Id IN (select ManagerId from
        Employee e2
    GROUP BY e2.ManagerId
    HAVING count(ManagerId) > 4);```

  • 0
    R
    SELECT 
        Name
    FROM
        Employee e1
        
    WHERE EXISTS ( 
                    SELECT 
                        'x'
                    FROM 
                        Employee e2
                    WHERE
                            e2.managerid = e1.id
                          
                    GROUP BY 
                            e2.managerid
                    HAVING 
                        count(e2.id) >= 5
        )
    

  • 1
    SELECT e.Name
    FROM Employee e
    JOIN 
      (SELECT
        ManagerId, COUNT(*)
      FROM
        Employee
      GROUP BY ManagerID
      HAVING COUNT(*) >= 5) AS m
    ON
      e.Id = m.ManagerID
    ;

  • 0
    K

    select e1.name, count(e2.managerid)
    from employee e1
    join employee e2 on e1.id=e2.managerid
    group by e1.name
    having count(e2.managerid)>=5;


  • 0
    Y

    Write your MySQL query statement below

    SELECT Name FROM Employee
    WHERE Id IN
    (SELECT ManagerId
    FROM Employee
    GROUP BY ManagerId
    HAVING COUNT(ManagerId) >= 5)


Log in to reply
 

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