Sql query

i have employee table as follows

Empid Name Mgrid Designation Salary

1 John Null Developer 15000
2 John 1 Programmer 15000
3 Joe 1 Developer 25000
4 Peter 3 Developer 35000

I want the ouput as follows

To get the below output how to write the query in sql server using self join

EmpName Manage rname

John Null
John John
Joe John
Peter Joe

https://www.sqlservertutorial.net/sql-server-basics/sql-server-recursive-cte/

1 Like

SELECT
Emp.Name AS EmpName,
Mgr.Name AS ManagerName
FROM
employee Emp LEFT JOIN employee Mgr on
Emp.Mgrid = Mgr.Empid