SQL Interview Questions – The Number of Employees Which Report to Each Employee

Spread the love

Problem Description –

For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.

Write an SQL query to report the ids and the names 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.

The query result format is in the following example.

Problem Link – Employee

Difficulty Level – Easy

Solution –

SELECT 
    e2.employee_id,
    e2.name,
    COUNT(*) as reports_count,
    ROUND(AVG(e1.age)) as average_age
FROM Employees as e1, Employees as e2
WHERE e1.reports_to = e2.employee_id
GROUP BY 1, 2
ORDER BY 1

Rating: 1 out of 5.

Leave a Reply