Home > DeveloperSection > Forums > How to use self join in sql server and what's it's important.
Ankit Singh

Total Post:341

Points:2389
Posted on    May-16-2013 6:55 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 814  View(s)
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! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-16-2013 8:39 AM

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






Don't want to miss updates? Please click the below button!

Follow MindStick