TS70363
New User
Joined: 07 Apr 2010 Posts: 94 Location: Bangalore, India
|
|
|
|
Hi,,
Can some one provide me with an example of self join and explain it...
I tried so much but could not find some relevant material on it... |
|
guptae
Moderator
Joined: 14 Oct 2005 Posts: 1208 Location: Bangalore,India
|
|
|
|
Hello There,
Self join is just like any other join except that two instances of the same table will be joined together.
Lets take an example of EMPLOYEE Table which contain rows for each employee in the organisation . Employee can be Manager as well.
Table contain follwoing columns
EMPLOYEE Table
Empid Int
Mgrid Int
Dept Char(25)
Salary Double
Empname Char(35)
So in order to find out the manager of all the Employees u need a self join
Code: |
SELECT t1.empname [Employee], t2.empname [Manager]
FROM emp t1, emp t2
WHERE t1.mgrid = t2.empid
|
|
|