Hierarchy Query in SQL Server - Get hierarchy of particular manager details

hi

i thought about what you said ..

the same issue for managerid 6 ...6 cannot be his own mananger

in your data .. the Top Most Manager will not have any manager above him ..
so i made it null

image

;WITH cte_org AS (
    SELECT       
        * 
    FROM       
        data
	WHERE 
	   ManagerID is null 
    UNION ALL
    SELECT 
       e.*
    FROM 
        data e
          INNER JOIN 
		cte_org o 
            ON o.EmpId = e.ManagerID 
     WHERE
	     e.ManagerID is not null 
)
SELECT * FROM cte_org;

image