employees-reporting-to-managers.sql (422B)
1 --Given a table with all employees 2 --return all employees who have 3 --at least one report along with 4 --the reports name. 5 SELECT DISTINCT Employees.employee_id, Employees.name, 6 COUNT(E.name) AS reports_count, 7 ROUND(AVG(E.age),0) as average_age 8 FROM Employees 9 LEFT JOIN Employees as E 10 ON Employees.employee_id = E.reports_to 11 WHERE E.employee_id IS NOT NULL 12 GROUP BY Employees.employee_id 13 ORDER BY Employees.employee_id