Skip to content
Loading
  • Jignesh Kumar
    +1
    Hi Sonu,
     
    You can use left join.
    http://venkatsqlinterview.blogspot.com/2011/05/self-join-with-example.html 
    +1
  • Amit Gupta
    0
    Use sub query for that
    0
  • 0
    Perhaps a sub-select. Any reason why you can't/won't use a self-join?
    0
  • Sonu Gupta
    0
     Table Stracture - 
     
    EMPLID   EmpName   ManagerID
    7    Ak 19
    2    AT 7
    19    MS 3
     
     
    Result 
    EMPLID   EmpName   ManagerID    ManagerName  
    7      Ak 19    MS
    0
  • Sonu Gupta
    0
    Hi Priyanka,
     
    There are only one table tblEmployee and its contain three fields - EmpID,EmpName,ManagerID. 
    Note - Find employee manager without using self join 
     
     
    Result like this
    EMPLID EmpName ManagerID ManagerName
    7 Ak19 MS
    0
  • Priyanka Jain
    0
    Hi Sonu
    If there are only 3 fields and only one table than from where Manager Name is coming from ?. Please let me know
    0
  • Sonu Gupta
    0
    Hi Priyanka,
     
    manager also employee in tblEmployee 
     
    For Example - MS is a manager of AK
    EMPLID   EmpName   ManagerID
    7   Ak19 
    2   AT7 
    19   MS3 
    0
  • Amit Kumar
    0
    Hi, you can use recursive CTE but still you have to use a join Look at this example: WITH MyCTE AS ( SELECT EmpID, FirstName, LastName, ManagerID FROM Employee WHERE ManagerID IS NULL UNION ALL SELECT EmpID, FirstName, LastName, ManagerID FROM Employee INNER JOIN MyCTE ON Employee.ManagerID = MyCTE.EmpID WHERE Employee.ManagerID IS NOT NULL ) SELECT * FROM MyCTE please refer to this link for detail of CTE. http://blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/
    0
  • Harsh Shah
    0
    You Can try this query if you don't want to use self join as shown in below image
     
     
     
     Output:
     
     
    0
  • Tushar Dikshit
    0
    HI,
     
    You can use CTE.
    1. ;with empCTE as  
    2. (  
    3. select e.empid, e.empname, e.managerid,  
    4. CAST('' as varchar(50)) as Mname from employee e  
    5. where managerid = 0  
    6. union all  
    7. select e1.empid, e1.empname, e1.managerid,  
    8. CAST(c.empname as varchar(50)) as Mname from employee e1  
    9. inner join empCTE as C on e1.managerid=c.empid  
    10. where e1.managerid>0  
    11. ) select * from empCTE  
    My post for such queries (Most asked SQL queries)-
     
    https://www.c-sharpcorner.com/article/most-asked-sql-queries-in-interview-questions/
     
    Thanks,
    Tushar
    0
  • Jaimin Shethiya
    0
    Hello Sonu,
     
    can you please try the below script for
    1. CREATE TABLE #tmpEmployee  
    2. (  
    3.    EmpId INT,  
    4.    EmpName VARCHAR(30),  
    5.    ManagerId INT  
    6. )  
    7. INSERT INTO  
    8.    #tmpEmployee  
    9. VALUES  
    10.    (1,'Emp-01', 1),  
    11.    (2,'Emp-02', 1),  
    12.    (3,'Emp-03', 2),  
    13.    (4,'Emp-04', 2)  
    14. SELECT  
    15.    DISTINCT  
    16.       e.EmpId,  
    17.       e.EmpName,  
    18.       m.EmpId ManagerId,  
    19.       m.EmpName ManagerName  
    20. FROM  
    21.    #tmpEmployee e,  
    22.    #tmpEmployee m  
    23. WHERE  
    24.    e.ManagerId = m.empId  
    25. GROUP BY  
    26.    e.EmpId,  
    27.    e.EmpName,  
    28.    m.EmpId,  
    29.    m.EmpName  
    30. DROP TABLE #tmpEmployee  
    let me know if you have any other help needed.
     
    Thanks
    0