The Number of Employees Which Report to Each Employee - Problem
The Number of Employees Which Report to Each Employee
You're working with a company's employee database and need to generate a management report that shows key statistics about managers and their direct reports.
Given a table
Key Information:
Your Goal: Create a report showing for each manager:
Return results ordered by
You're working with a company's employee database and need to generate a management report that shows key statistics about managers and their direct reports.
Given a table
Employees with the following structure:| Column Name | Type |
|---|---|
| employee_id | int |
| name | varchar |
| reports_to | int |
| age | int |
Key Information:
employee_idis unique for each employeereports_tocontains the manager's employee_id (null if no manager)- A manager is defined as someone with at least 1 direct report
Your Goal: Create a report showing for each manager:
- Their
employee_idandname - The count of employees reporting directly to them
- The average age of their direct reports (rounded to nearest integer)
Return results ordered by
employee_id. Input & Output
example_1.sql โ Basic Company Structure
$
Input:
Employees table:\n| employee_id | name | reports_to | age |\n|-------------|-------|------------|-----|\n| 9 | Hercy | null | 43 |\n| 6 | Alice | 9 | 41 |\n| 4 | Bob | 9 | 36 |\n| 2 | Winston| null | 37 |
โบ
Output:
| employee_id | name | reports_count | average_age |\n|-------------|-------|---------------|-------------|\n| 9 | Hercy | 2 | 39 |
๐ก Note:
Hercy (ID: 9) is the only manager with direct reports. Alice (41) and Bob (36) both report to Hercy. Count = 2, Average age = (41 + 36) / 2 = 38.5, rounded to 39.
example_2.sql โ Multiple Managers
$
Input:
Employees table:\n| employee_id | name | reports_to | age |\n|-------------|---------|------------|-----|\n| 1 | Boss | null | 60 |\n| 2 | Alice | 1 | 30 |\n| 3 | Bob | 1 | 40 |\n| 4 | Charlie | 2 | 25 |\n| 5 | David | 2 | 35 |
โบ
Output:
| employee_id | name | reports_count | average_age |\n|-------------|-------|---------------|-------------|\n| 1 | Boss | 2 | 35 |\n| 2 | Alice | 2 | 30 |
๐ก Note:
Boss manages Alice and Bob (ages 30, 40) โ average 35. Alice manages Charlie and David (ages 25, 35) โ average 30. Results ordered by employee_id.
example_3.sql โ Edge Case with Rounding
$
Input:
Employees table:\n| employee_id | name | reports_to | age |\n|-------------|------|------------|-----|\n| 1 | John | null | 50 |\n| 2 | Jane | 1 | 33 |\n| 3 | Jack | 1 | 34 |\n| 4 | Jill | 1 | 35 |
โบ
Output:
| employee_id | name | reports_count | average_age |\n|-------------|------|---------------|-------------|\n| 1 | John | 3 | 34 |
๐ก Note:
John manages 3 employees with ages 33, 34, 35. Average = (33 + 34 + 35) / 3 = 34.0, which rounds to 34.
Constraints
- 1 โค employees.length โค 104
- 1 โค employee_id โค 106
- 1 โค age โค 100
- reports_to can be null (for top-level employees)
- All employee_id values are unique
- Employee names are non-empty strings
Visualization
Tap to expand
Understanding the Visualization
1
Identify Relationships
Map each employee to their direct manager using reports_to field
2
Group by Manager
Automatically group all employees under their respective managers
3
Calculate Statistics
Count direct reports and compute average age for each manager's team
4
Generate Report
Format results with manager details and sort by employee ID
Key Takeaway
๐ฏ Key Insight: GROUP BY with aggregate functions transforms hierarchical relationship data into management metrics efficiently, leveraging the database engine's optimization for O(n) performance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code