WITH RECURSIVE cte AS(
SELECT d.ID, d.NameDepartment, d.ParentDepartmentID, e.ID AS Emp_ID, 1 AS Level
FROM departments d
INNER JOIN employees e ON e.DepartmentID = d.ID
UNION ALL
SELECT d.ID, d.NameDepartment, d.ParentDepartmentID, cte.Emp_ID, cte.Level + 1
FROM cte
INNER JOIN departments d ON cte.ID = d.ParentDepartmentID
)
SELECT id, count(*) AS Emp_Count
FROM cte
GROUP BY id, level;