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 Employees with the following structure:

Column NameType
employee_idint
namevarchar
reports_toint
ageint

Key Information:
  • employee_id is unique for each employee
  • reports_to contains 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_id and name
  • 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
Employee Management HierarchyCEOEmployee ID: 1MGRID: 2, Age: 35MGRID: 3, Age: 40EMPAge: 25EMPAge: 30EMPAge: 28EMPAge: 32Manager 2 StatsReports: 2Avg Age: 28Manager 3 StatsReports: 2Avg Age: 30
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.
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
24.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen