SQL Server Interview Questions - Self Join With An Example
SQL Server Interview Questions - Self Join With An Example
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
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/selfjoinwithexample.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
http://venkatsqlinterview.blogspot.in/2011/05/selfjoinwithexample.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
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
http://venkatsqlinterview.blogspot.in/2011/05/selfjoinwithexample.html 3/5
12/24/2016 SQL Server Interview Questions: Self Join with an example
5 comments:
Reply
Replies
select e2.EmpID,e2.EmpName,e1.EmpName as
Manager
from tblEmployee e1 right join tblEmployee e2
on e1.EmpID=e2.Manager
http://venkatsqlinterview.blogspot.in/2011/05/selfjoinwithexample.html 4/5
12/24/2016 SQL Server Interview Questions: Self Join with an example
Reply
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
Disclaimer Terms of use Contact Us
http://venkatsqlinterview.blogspot.in/2011/05/selfjoinwithexample.html 5/5