Self Join In Sql Server 2000 2005

Updated on 07 Feb 2012,
Published on 28 Feb 2008

Self Join in SQL Server 2000/2005 helps in retrieving the records having some relation or similarity with other records in the same database table. A common example of employees table can do more clearly about the self join in sql. Self join in sql means joining the single table to itself. It creates the partial view of the single table and retrieves the related records. You can use aliases for the same table to set a self join between the single table and retrieve the records satisfying the condition in where clause.

You can see the example of Joins using table aliases in SQL Server 2005 here.

For self join in sql you can try the following example:

Create Table Employees
emp_idemp_nameemp_manager_id
1 John Null
2 Tom 1
3 Smith 1
4 Albert 2
5 David 2
6 Murphy 5
7 Petra 5

Now to get the names of managers from the above single table you can use sub queries or simply the self join. 

Self Join SQL Query

Following is an example of sql query to get the names of manager and employees.

SELECT		e1.emp_name as 'manager',
			e2.emp_name as 'employee'
FROM		employees e1 
JOIN		employees e2
ON			e1.emp_id = e2.emp_manager_id
Result
manageremployee
John Tom
John Smith
Tom Albert
Tom David
David Murphy
David Petra

Understanding the Self Join Example

In the above self join query, employees table is joined with itself using table aliases e1 and e2. This creates the two views of a single table. 

...
FROM		employees e1 
JOIN		employees e2
ON			e1.emp_id = e2.emp_manager_id

Here e.emp_manager_id passes the manager id from the 2nd view to the first aliased e1 table to get the names of managers.

Continue to next tutorial: SQL Inner Equi Join Examples to learn the implementation of equi join using SQL query.

18 Responses to "Self Join In Sql Server 2000 2005"
shailendra Agarwal
Excellent
Jitendra
Good example...thanks
Muthu
Nice!
Ragulani
Nice example.Thanks a lot:-)
test
this is test coment to test this site 'sp_help'
srinalini
nice example and good understanding example
harish
good
swetha
good example
Very good...... I like this This is Way to learn every thing and you should also explain all join which is included in sqlServer 2005 with all example.
veeru
nice example
Kushal
good example.
uday
thanks yaar
Vipin
Very Good Example. By This example every one learn easily the concept of joins.
jaiveer
i like this example.........thanks
sudha
Good one. All can easily understand this clearly. You rock by this example. keep up.
Siddu Jakkannavar
Please i have to make SP and wiht Join condtion , please tell meabout join condtion with all Parameters and Different Join commands ...

we have very big problem in Join condtion in SP
Lokesh
good example
Asha Bhatt
Hi, This is very very useful information. Like this. Regards, Asha Bhatt
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers