Find the bugs: employee reporting hierarchy

Build an employee reporting hierarchy, starting with the highest-level manager, who reports to no one. Show the level of each employee: 0 for the top-level manager, 1 for direct reports, 2 for the next level, and so on. Identify the lines that contain errors.

Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONMiddleData EngineerBackendAnalyst
Input data · iv_employees
idnamedepartment_idmanager_idsalaryhired_at
1Anna1NULL5000.002020-03-01 09:00:00
2Boris115000.002021-06-15 09:00:00
3Clara114000.002022-01-31 23:59:00
4Dmitry216000.002019-09-01 09:00:00
5Elena245500.002021-02-01 00:00:00
6Foma245500.002023-04-10 10:30:00
7Galina314500.002020-11-20 09:00:00
8HugoNULL13000.002024-05-05 09:00:00
9Irina37NULL2024-08-12 09:00:00
10Egor243500.002025-01-13 09:00:00
WITH RECURSIVE tree AS (
  SELECT id, name, manager_id, 0 AS lvl
  FROM iv_employees
  WHERE manager_id IS NULL
  UNION ALL
  SELECT e.id, e.name, e.manager_id, t.lvl + 1
  FROM iv_employees e
  JOIN tree t ON e.id = t.manager_id
)
SELECT name, lvl
FROM tree
ORDER BY lvl, name