You are given a table Employees containing information about employees and their managers.
Table: Employees
| Column Name | Type |
|---|---|
| employee_id | int |
| name | varchar |
| reports_to | int |
| age | int |
employee_id is the primary key for this table. This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
Write a solution to report the employee_id and name of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
Return the result table ordered by employee_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
employee_id
PK
|
int | Unique identifier for each employee |
name
|
varchar | Employee name |
reports_to
|
int | Employee ID of the manager this employee reports to (NULL if no manager) |
age
|
int | Employee age |
Input & Output
| employee_id | name | reports_to | age |
|---|---|---|---|
| 9 | Hercy | 43 | |
| 6 | Alice | 9 | 41 |
| 4 | Bob | 9 | 36 |
| 2 | John | 20 |
| employee_id | name | reports_count | average_age |
|---|---|---|---|
| 9 | Hercy | 2 | 39 |
Employee 9 (Hercy) is a manager with 2 direct reports: Alice (age 41) and Bob (age 36). The average age is (41 + 36) / 2 = 38.5, rounded to 39. Employee 2 (John) has no reports, so is not included.
| employee_id | name | reports_to | age |
|---|---|---|---|
| 1 | Boss | 50 | |
| 2 | Manager1 | 1 | 40 |
| 3 | Manager2 | 1 | 35 |
| 4 | Employee1 | 2 | 25 |
| 5 | Employee2 | 2 | 30 |
| employee_id | name | reports_count | average_age |
|---|---|---|---|
| 1 | Boss | 2 | 38 |
| 2 | Manager1 | 2 | 28 |
Boss (ID 1) manages 2 people with average age (40 + 35) / 2 = 37.5 → 38. Manager1 (ID 2) manages 2 people with average age (25 + 30) / 2 = 27.5 → 28. Manager2 has no direct reports.
Constraints
-
1 ≤ employee_id ≤ 10^4 -
1 ≤ name.length ≤ 20 -
1 ≤ age ≤ 100 -
reports_tocan benullfor employees with no manager