WITH (Common Table Expressions) To produce the organizational chart with the management chain for each employee (that is, the path from CEO to employee), use a recursive CTE: The CTE produces this output: WITH RECURSIVE employee_paths (id, name, path) AS ( SELECT id, name, CAST(id AS CHAR(200)) FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.name, CONCAT(ep.path, ’,’, e.id) FROM employee_paths AS ep JOIN employees AS e ON ep.id = e.manager_id ) SELECT * FROM employee_paths ORDER BY path; +------+---------+-----------------+ | id | name | path | +------+---------+-----------------+ | 333 | Yasmina | 333 | | 198 | John | 333,198 | | 29 | Pedro | 333,198,29 | | 4610 | Sarah | 333,198,29,4610 | | 72 | Pierre | 333,198,29,72 | | 692 | Tarek | 333,692 | | 123 | Adil | 333,692,123 | +------+---------+-----------------+ mysql>  WITH RECURSIVE ... ... SELECT * FROM employees_extended WHERE id IN (692, 4610) ORDER BY path; +------+-------+-----------------+ | id | name | path | +------+-------+-----------------+ | 4610 | Sarah | 333,198,29,4610 | | 692 | Tarek | 333,692 | +------+-------+-----------------+ How the CTE works: The  path  column is widened to  CHAR(200)  to ensure that there is room for the longer  path  values produced by the recursive  SELECT . •The nonrecursive  SELECT  produces the row for the CEO (the row with a  NULL  manager ID). •Each row produced by the recursive  SELECT  finds all employees who report directly to an employee produced by a previous row. For each such employee, the row includes the employee ID and name, and the employee management chain. The chain is the manager’s chain, with the employee ID added to the end. •Recursion ends when employees have no others who report to them. To find the path for a specific employee or employees, add a  WHERE  clause to the top-level  SELECT . For example, to display the results for Tarek and Sarah, modify that  SELECT  like this: Common table expressions (CTEs) are similar to derived tables in some ways: •Both constructs are named. Common Table Expressions Compared to Similar Constructs 2719