What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8602 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > SQL Server Interview Questions > Explain Self Join with an example ... ...

Explain Self Join with an example

Interview question and answer by: CGN007 | Posted on: 7/20/2012 | Category: SQL Server Interview questions | Views: 514 | | Points: 40


Answer:

Self Join is just like a inner join, which joins the two instance of the same table.

A simple example is to find-out the manger of an employee from the employee table that contains both employee id an manger id.
Here the manager is also an employee having an employee id.

Let us see the code

1.Creating the Employee table

CREATE TABLE Employee

(employee_id int,

manger_id int,

employee_name varchar(20)

)



2.Inserting the value

INSERT INTO Employee VALUES(1,1,'Sachin'),(2,1,'Rahul'),(3,2,'Sourav'),(4,1,'Sunil')



3.The self join query

SELECT emp.employee_id,emp.employee_name [Employee], mgr.employee_name [Manager]


FROM Employee emp, Employee mgr

WHERE emp.manger_id = mgr.employee_id

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from CGN007

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 11:11:11 PM