570 Managers with at Least 5 Direct Reports
To determine managers who have at least 5 direct reports, we need to analyze the Employee table by grouping employees under their respective managers.
Approach
- Use a
LEFT JOINto self-join theEmployeetable:- We join the table on
managerIDandidto associate employees with their respective managers:
FROM Employee AS e1 LEFT JOIN Employee AS e2 ON e1.managerID = e2.id- This ensures that every employee is matched with their corresponding manager.
- We join the table on
- Group by manager and count direct reports:
- We use
GROUP BYto count the number of employees under each manager:
GROUP BY e1.managerID HAVING COUNT(*) >= 5 - We use
Conclusion
By structuring the query in this way, we can efficiently identify managers with 5 or more direct reports, which is useful for organizational analysis and HR reporting.
