Manager With at Least 5 Direct Reports
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
You have an Employee table where each employee may have a manager (identified by managerId). A manager is also an employee in the same table. Write a SQL query to find the names of managers who have at least 5 direct reports. Return the result in any order.
Database Schema
Employee
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| name | VARCHAR | Employee name |
| department | VARCHAR | Department code |
| managerId | INT | ID of this employee's manager (nullable) |
Example
Employee
| id | name | department | managerId |
|---|---|---|---|
| 101 | John | A | NULL |
| 102 | Dan | A | 101 |
| 103 | James | A | 101 |
| 104 | Amy | A | 101 |
| 105 | Anne | A | 101 |
| 106 | Ron | B | 101 |
Output
| name |
|---|
| John |
Explanation:
John (id=101) manages Dan, James, Amy, Anne, and Ron - that is 5 direct reports, so John qualifies.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
SQL Editor
Loading...
Employee
| id | name | department | managerId |
|---|---|---|---|
| 101 | John | A | NULL |
| 102 | Dan | A | 101 |
| 103 | James | A | 101 |
| 104 | Amy | A | 101 |
| 105 | Anne | A | 101 |
| 106 | Ron | B | 101 |
Output
| name |
|---|
| John |