Managers with at Least 5 Direct Reports
MediumLeetCodeSQL: Joins
Hint and exampleShowHide
Count the reports of each manager in a grouped subquery with HAVING, then join that result back to the same table to fetch the managers' names.
Example: staff(id, name, boss_id) — bosses with at least 2 reports
id | name | boss_id
1 | Rao | NULL
2 | Asha | 1
3 | Ravi | 1
4 | Meera | 2
SELECT s.name
FROM staff s
JOIN (SELECT boss_id
FROM staff
GROUP BY boss_id
HAVING COUNT(*) >= 2) b ON b.boss_id = s.id; -- returns Rao