You are given a table Employee that contains information about employees, their departments, and their managers.
Each row represents an employee with their unique id, name, department, and the managerId of their direct manager. If managerId is NULL, the employee has no manager.
Task: Write a SQL query to find managers who have at least 5 direct reports.
Return the result in any order containing the names of these managers.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key - unique employee ID |
name
|
varchar | Employee's full name |
department
|
varchar | Department where employee works |
managerId
|
int | ID of the employee's direct manager (NULL if no manager) |
Input & Output
| id | name | department | managerId |
|---|---|---|---|
| 101 | John | A | |
| 102 | Dan | A | 101 |
| 103 | Brad | A | 101 |
| 104 | George | A | 101 |
| 105 | Kyle | A | 101 |
| 106 | Lisa | A | 101 |
| name |
|---|
| John |
John (id=101) has exactly 5 direct reports: Dan, Brad, George, Kyle, and Lisa all have managerId=101. Since this meets our criteria of at least 5 direct reports, John is included in the result.
| id | name | department | managerId |
|---|---|---|---|
| 101 | Alice | Engineering | |
| 102 | Bob | Engineering | |
| 103 | Charlie | Engineering | 101 |
| 104 | David | Engineering | 101 |
| 105 | Eve | Marketing | 102 |
| 106 | Frank | Marketing | 102 |
| name |
|---|
Alice has 2 direct reports (Charlie, David), and Bob has 2 direct reports (Eve, Frank). Neither manager has at least 5 direct reports, so the result is empty.
| id | name | department | managerId |
|---|---|---|---|
| 201 | Sarah | Sales | |
| 202 | Tom | Sales | 201 |
| 203 | Jerry | Sales | 201 |
| 204 | Mary | Sales | 201 |
| 205 | Peter | Sales | 201 |
| 206 | Anna | Sales | 201 |
| 207 | Mike | Sales | 201 |
| 208 | Linda | Sales | 201 |
| name |
|---|
| Sarah |
Sarah (id=201) has 7 direct reports: Tom, Jerry, Mary, Peter, Anna, Mike, and Linda. Since 7 ≥ 5, Sarah qualifies as a manager with at least 5 direct reports.
Constraints
-
1 ≤ Employee.id ≤ 500 -
1 ≤ Employee.name.length ≤ 20 -
1 ≤ Employee.department.length ≤ 15 -
All employee
idvalues are unique -
managerIdisNULLor references a valid employeeid