You are on page 1of 5

12/24/2016 SQL Server Interview Questions: Self Join with an example

0   More    Next Blog» Create Blog   Sign In

SQL Server Interview Questions
C#
Boo MV ASP. C SQL W Written HR Subsc SQL Buy
Program 0
ks C NET # Server CF Test Round ribe Tutorial DVD
s

If you like this website, please share


this site using g+1 button Self Join with an example
Recommend this on Google
{
Take: [“the”],
Android:
“course”
}
Best software training institute in bangalore for SAP ABAP,
SAP BI, .Net, Informatica, Software Testing, Siebel CRM
I am Venkat and this is my website. I did training in Pragim
Technologies and got job in Dell in less than a week. PRAGIM is the
best S/W training institute in Bangalore.
Click here for Hyderabad Center Website
For further details please call 09900113931.

There are 3 different types of joins available in sql server, and they are
1. Cross Join 
2. Inner Join or Join 
3. Outer Join

Outer Join is again divided into 3 types as shown below.
1. Left Outer Join or Left Join 
2. Right Outer Join or Right Join 
3. Full Outer Join or Full Join 

I strongly recomend to learn about the basics and types of joins, before reading
this article. Read the articles below, before proceeding with self join.
1. Basics of Joins
2. Inner Join
3. Left Outer Join
4. Right Outer Join
5. Full Outer Join
SAP BI Training in Bangalore
Best SAP BI Training institute in Bangalore,
Marathahalli. Real time project based training
provided by working software professionals
having more than 10 years of experience.
Informatica Training in Bangalore
Informatica training in bangalore delivered by
a real time software expert having 10 years
of experience. 
Siebel CRM Training in Bangalore
Best software training institute for Siebel CRM
training in Marathahalli, Bangalore. 
Software testing training institute in
bangalore

http://venkatsqlinterview.blogspot.in/2011/05/self­join­with­example.html 1/5
12/24/2016 SQL Server Interview Questions: Self Join with an example

Software testing training on real time projects
and placements. 
MSBI Training Institute in Bangalore
Best MSBI Training in Bangalore by an
expert. MSBI training on real time projects
and placements. 
Basic SQL Server Interview Questions

SQL Server Interview Questions on


Temporary Tables

SQL Server Interview Questions on


Indexes ‐ Part 1
Airtel Postpaid Plans
Get MyPlan with Great Benefits and
SQL Server Interview Questions on Packs. Browse for Details.
Indexes ‐ Part 2 www.airtel.in/myplan

What is the difference between


Having and Where clause Self join is not a different type of join. Self join means joining a table with itself.
We can have an inner self join or outer self join. Let us try to understand with
What is the difference between a an example.
Temporary Table and a Table Variable
To set up the data for the example, use the script below to create Employee
What is the use of COALESCE in SQL Table and populate it with some sample data. We will be using Employee Table
Server to understand Self Join.

SQL Server Interview Questions on CREATE TABLE EMPLOYEE


triggers (
[EMPLOYEEID] INT PRIMARY KEY,
Difference between User Defined [NAME] NVARCHAR(50),
Function and Stored Procedure [MANAGERID] INT
)
SQL Server Interview Questions on GO
Views ‐ Part 1
INSERT INTO EMPLOYEE VALUES(101,'Mary',102)
SQL Server Interview Questions on INSERT INTO EMPLOYEE VALUES(102,'Ravi',NULL)
Views ‐ Part 2 INSERT INTO EMPLOYEE VALUES(103,'Raj',102)
INSERT INTO EMPLOYEE VALUES(104,'Pete',103)
Basic SQL Server Interview Questions INSERT INTO EMPLOYEE VALUES(105,'Prasad',103)
on Joins INSERT INTO EMPLOYEE VALUES(106,'Ben',103)
GO
Explain Inner Join with an example
We use Self Join, if we have a table that references itself. For example, In the
Explain Left Outer Join with an Employee Table below MANAGERID column references EMPLOYEEID column.
example So the table is said to referencing itself. This is the right scenario where we can
use Self Join. Now I want to write a query that will give me the list of all
Explain Right Outer Join with an Employee Names and their respective Manager Names. In order to achieve this I
example can use Self Join. In the Table below,Raj is the manager for Pete,Prasad and
Ben. Ravi is the manager for Raj and Mary. Ravi does not have a manager as he
Explain Full Outer Join with an is the president of the Company.
example

Explain Self Join with an example

What is the difference between Index


Scan and Index Seek

Write a SQL Query to delete from a


table that is involved in a SQL join

What are the advantages of using  


stored procedures The query below is an example of Self Join. Both E1 and E2 refer to the same
Employee Table. In this query we are joining the Employee Table with itself. 
What are the different ways to
replace NULL values in SQL Server SELECT E1.[NAME],E2.[NAME] AS [MANAGER NAME]

http://venkatsqlinterview.blogspot.in/2011/05/self­join­with­example.html 2/5
12/24/2016 SQL Server Interview Questions: Self Join with an example

FROM EMPLOYEE E1 
SQL Server interview questions on INNER JOIN EMPLOYEE E2 
string manipulation functions ON E2.EMPLOYEEID =E1.MANAGERID

Write a Stored Procedure that takes If we run the above query we only get 5 rows out of the 6 rows as shown below.


column name as a parameter and
returns the result sorted by the Inner Self Join
column that is passed

What is deferred name resolution in


SQL Server?

 
This is because Ravi does not have a Manager. MANAGERID column for Ravi is
NULL. If we want to get all the rows then we can use LEFT OUTER JOIN as
shown below.

SELECT E1.[NAME],E2.[NAME] AS [MANAGER NAME]
FROM EMPLOYEE E1 
LEFT OUTER JOIN EMPLOYEE E2 
ON E2.EMPLOYEEID =E1.MANAGERID

If we execute the above query we get all the rows, including the row that has a
null value in the MANAGERID column. The results are shown below. The
MANAGERNAME for 2nd record is NULL as Ravi does not have a Manager.
Left Outer Self Join

 
Let us now slightly modify the above query using COALESCE as shown below.
Read COALESCE function in SQL Server to understand COALESCE in a greater
detail.

SELECT E1.[NAME],COALESCE(E2.[NAME],'No Manager') AS [MANAGER
NAME]
FROM EMPLOYEE E1 
LEFT JOIN EMPLOYEE E2 
ON E2.EMPLOYEEID =E1.MANAGERID

If we execute the above query the output will be as shown in the image below.
This is how COALESCE can be used.

Left Outer Self Join with COALESCE

Recommend this on Google

http://venkatsqlinterview.blogspot.in/2011/05/self­join­with­example.html 3/5
12/24/2016 SQL Server Interview Questions: Self Join with an example

5 comments:

Anonymous August 5, 2013 at 11:17 PM


what is the scope of triggers in database..?
Reply

jayprakash August 25, 2013 at 2:07 PM


This is for question table

EMPID EMPNAME MANAGER


1001 JP ‐
1002 SANDEEP 1001
1003 SUMIT 1002
1004 XYZ 1002
1005 QST 1005
1006 LMN 1004

This is for result table


OUTPUT
WRITE A QUERY

EMPID EMPNAME MANAGER


1001 JP ‐
1002 SANDEEP JP
1003 SUMIT SANDEEP
1004 XYZ SANDEEP
1005 QST QST
1006 LMN XYZ

Reply

Replies

kinsuk sadhukhan November 10, 2015 at 3:21 PM


HI Jayprakash,

Below is query for that output:

select e2.EmpID,e2.EmpName,e1.EmpName as
Manager
from tblEmployee e1 right join tblEmployee e2
on e1.EmpID=e2.Manager

Sudha Chitiprolu July 28, 2016 at 9:49 PM

http://venkatsqlinterview.blogspot.in/2011/05/self­join­with­example.html 4/5
12/24/2016 SQL Server Interview Questions: Self Join with an example

select EMPID,EMPNAME,(select empname from


empmanager a where a.EMPID=b.manager)mname
from empmanager b

Reply

sandeep 9908 December 20, 2015 at 10:19 PM


Hi Jayaprakash,

try this u'll get above output

select a.Employyeid,a.empname,COALESCE(aa.
[empname],'NoMAnager')as ManagerName
FRom
(
select a1.Employyeid,a1.empname,MAX(a2.Employyeid)as
nextID
from dbo.Employye a1
left join dbo.Employye a2 on a1.Employyeid>a2.Employyeid
group by a1.Employyeid,a1.empname)a
left join dbo.Employye aa on a.nextID=aa.Employyeid
order by a.Employyeid;
Reply

Enter your comment...

Comment as:  Select profile...

Publish
  Preview

If you are aware of any other sql server questions asked in an


interview, please post them below. If you find anything missing or
wrong, please feel free to correct by submitting the form below.

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

 
Disclaimer ­ Terms of use ­ Contact Us

http://venkatsqlinterview.blogspot.in/2011/05/self­join­with­example.html 5/5

You might also like