Or, if your query is not about traversing all employee to get "top boss" and you only want "nearest" manager, try thisDECLARE @Sample TABLE ( ID INT, Employee CHAR(1), ManagerID INT )INSERT @SampleSELECT 1, 'A', 4 UNION ALLSELECT 2, 'B', 2 UNION ALLSELECT 3, 'C', 1 UNION ALLSELECT 4, 'D', 2 UNION ALLSELECT 5, 'E', 4SELECT m.ID AS ManagerID, m.Employee AS ManagerName, e.ID, e.EmployeeFROM @Sample AS eINNER JOIN @Sample AS m ON m.ID = e.ManagerIDORDER BY m.ID, e.ID
E 12°55'05.63"N 56°04'39.26"