SQL 50, decoded
0 of 50 solved/50

570. Managers with at Least 5 Direct Reports

SQL 50 · Basic Joins (day 2). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.

MediumSolve it on LeetCode ↗

Your work

Hints

Open one at a time. Try to finish with as few as you can.

1. What does one output row represent?
One manager (name).
2. Approach
Self join reports to managers, GROUP BY the manager, HAVING COUNT(*) >= 5.
3. The trap
Group by the manager’s id, not just the name; two managers can share a name.

Reference solution

Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.

Show the MySQLPostgreSQL solution
SELECT m.name
FROM Employee m
JOIN Employee r ON r.managerId = m.id
GROUP BY m.id, m.name
HAVING COUNT(*) >= 5;

Patterns used

Which join · Decode a question