HOW TO USE SELF JOIN IN SQL SERVER AND WHAT'S IT'S IMPORTANT.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 1040  View(s)
Ratings:
Rate this:
Hi All!

How to use self join in sql server and what's it's important.
Please give any example.

Thanks in advance! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to use self join in sql server and what's it's important.

    Self Join

    Self join correlates rows of a table with the other rows of the same table. It is used when a table has to be joined to itself to produce results.

    Example

    First you have to create a table EmpTable and enter the details like this:

    EmpID EmpName EmpMgrID

    1 Smith 2

    2 Sai 3

    3 Suresh 1

    4 Bill 2

    5 Steve 4

    SELECT E2.EMPNAME,E1.EMPNAME as 'MANAGER'

    FROM EmpTable E1

    INNER JOIN EmpTable E2

    ON E1.EMPID=E2.EMPMGRID

    Output

    EmpName Manager

    Suresh Smith
    Smith Sai
    Bill Sai
    Sai Suresh
    Steve Bill





Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!