Managers with at Least 5 Direct Reports - Problem
Corporate Hierarchy Analysis
You're working as a data analyst for a large corporation and need to identify managers who are overloaded with direct reports. The company wants to find managers who have at least 5 employees reporting directly to them, as these managers might need additional support or organizational restructuring.
Given an
Table Schema:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| department | varchar |
| managerId | int |
+-------------+---------+
Key Points:
•
•
• If
• Return manager names in any order
You're working as a data analyst for a large corporation and need to identify managers who are overloaded with direct reports. The company wants to find managers who have at least 5 employees reporting directly to them, as these managers might need additional support or organizational restructuring.
Given an
Employee table with employee information including their manager relationships, write a SQL query to find all managers with at least 5 direct reports.Table Schema:
Employee+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| department | varchar |
| managerId | int |
+-------------+---------+
Key Points:
•
id is the primary key•
managerId references another employee's id• If
managerId is null, the employee has no manager• Return manager names in any order
Input & Output
basic_corporate_hierarchy.sql
$
Input:
Employee table:
+-----+-------+------------+-----------+
| 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 |
| 107 | Bob | B | 101 |
| 108 | Alice | B | null |
+-----+-------+------------+-----------+
›
Output:
+------+
| name |
+------+
| John |
+------+
💡 Note:
John is the only manager with at least 5 direct reports. He manages Dan (102), James (103), Amy (104), Anne (105), Ron (106), and Bob (107) - that's 6 direct reports total. Alice has no direct reports.
multiple_managers.sql
$
Input:
Employee table:
+-----+-------+------------+-----------+
| 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 |
| 201 | Alice | B | null |
| 202 | Bob | B | 201 |
| 203 | Carol | B | 201 |
| 204 | David | B | 201 |
| 205 | Emma | B | 201 |
| 206 | Frank | B | 201 |
+-----+-------+------------+-----------+
›
Output:
+-------+
| name |
+-------+
| John |
| Alice |
+-------+
💡 Note:
Both John and Alice have at least 5 direct reports. John manages 5 employees (Dan, James, Amy, Anne, Ron) and Alice manages 5 employees (Bob, Carol, David, Emma, Frank).
no_qualifying_managers.sql
$
Input:
Employee table:
+-----+-------+------------+-----------+
| id | name | department | managerId |
+-----+-------+------------+-----------+
| 101 | John | A | null |
| 102 | Dan | A | 101 |
| 103 | James | A | 101 |
| 201 | Alice | B | null |
| 202 | Bob | B | 201 |
+-----+-------+------------+-----------+
›
Output:
+------+
| name |
+------+
(empty result)
💡 Note:
No manager has 5 or more direct reports. John has 2 direct reports (Dan, James) and Alice has 1 direct report (Bob). Since neither manager reaches the threshold of 5, the result is empty.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Relationships
Map each employee to their direct manager using managerId
2
Group by Manager
Cluster all employees under their respective managers
3
Count Team Sizes
Calculate how many direct reports each manager has
4
Apply Threshold Filter
Keep only managers with 5 or more direct reports
5
Extract Manager Names
Return the names of qualifying managers
Key Takeaway
🎯 Key Insight: Use SQL's GROUP BY and HAVING clauses to aggregate employee counts by manager and filter efficiently in a single query, avoiding the need for nested loops or multiple passes through the data.
Time & Space Complexity
Time Complexity
O(n log n)
Database optimizes GROUP BY with sorting/hashing, typically O(n log n)
⚡ Linearithmic
Space Complexity
O(n)
Temporary space needed for grouping and aggregation
⚡ Linearithmic Space
Constraints
- 1 ≤ Employee table rows ≤ 104
- id is unique for each employee
- managerId references a valid employee id or is null
- No employee is their own manager
- Employee names are unique within the company
- Result can be returned in any order
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code