Magicsheet logo

The Number of Employees Which Report to Each Employee

Easy
88.5%
Updated 6/1/2025

The Number of Employees Which Report to Each Employee

What is this problem about?

Organizational structures are often hierarchical, and analyzing these relationships is a common task in enterprise software. "The Number of Employees Which Report to Each Employee" is a database problem where you are given an employees table containing IDs, names, and the IDs of their managers. Your task is to identify all managers (employees who have at least one other employee reporting to them) and for each manager, report their ID, name, the number of direct reports they have, and the average age of those reports rounded to the nearest integer.

Why is this asked in interviews?

This the Number of Employees Which Report to Each Employee interview question is frequently asked by companies like Microsoft and Amazon. It tests your ability to perform self-joins or correlated subqueries in SQL. It also evaluates your proficiency with aggregation and rounding functions. Understanding how to navigate a parent-child relationship within a single table is a fundamental skill for managing relational data in business applications.

Algorithmic pattern used

This problem follows the Database interview pattern, specifically focusing on self-referential relationships.

  1. You can perform a self-join by joining the employees table with itself. One alias (e.g., mgr) represents the managers, and the other alias (e.g., emp) represents the subordinates.
  2. The join condition is mgr.employee_id = emp.reports_to.
  3. Group the results by mgr.employee_id and mgr.name.
  4. Use COUNT(emp.employee_id) to get the number of reports and ROUND(AVG(emp.age)) to calculate the average age.
  5. Filter out anyone who isn't a manager (which the join does automatically if it's an INNER JOIN).

Example explanation

Employees Table:

  • 1: Alice, Age 40, ReportsTo NULL
  • 2: Bob, Age 25, ReportsTo 1
  • 3: Charlie, Age 35, ReportsTo 1
  • 4: David, Age 30, ReportsTo 2
  1. Self-Join:
    • Alice (1) is a manager for Bob (2) and Charlie (3).
    • Bob (2) is a manager for David (4).
  2. Aggregation:
    • For Alice: 2 reports, Avg Age = (25+35)/2 = 30.
    • For Bob: 1 report, Avg Age = 30. The result lists Alice and Bob with their respective metrics.

Common mistakes candidates make

In "The Number of Employees Which Report to Each Employee coding problem," a common mistake is using the manager's age in the average calculation instead of the subordinates' ages. Another error is not rounding the average age correctly or using an incorrect join type that fails to filter out employees who aren't managers.

Interview preparation tip

Practice working with recursive data structures in SQL. Self-joins are the most common way to handle hierarchies, but some databases also support recursive Common Table Expressions (CTEs), which are useful for deeper hierarchical analysis. Always be careful about which table alias you are pulling data from during a self-join.

Similar Questions