Types of joins in sql server
In this article i explain about joins in sql server. Join is used to combine two or more tables with common relationship between tables. There are various types of joins like inner join, outer join, cross join in sql server. If we want to select data from two different tables we can use these joins.
Join is used to combine two or more tables in sql with some relationship between tables. There are several types of joins in sql server. The most common join used in pratical are Inner Join, Outer Join, Cross Join.
Let us consider these two tables to know how to use joins in tables.Detail Table:
Id Name PhoneNo
1 Ramesh 9786756453
2 Siva 9283746565
3 Kamal 9192939495Address Table:
Sno Resident Office
1 Trichy Delhi
2 Madurai Chennai
4 Coimbatore BangloreInner Join:
Inner join return the rows when the first table have the exact match with the second table with atleast any one common value.Query:
select a.Id, b.Sno, a.Name, a.PhoneNo, b.ResidentAddress, b.OfficeAddress
from Detail a inner join Address b on a.Id = b.SnoOutput:
Id Sno Name PhoneNo Resident Office
1 1 Ramesh 9786756453 Trichy Delhi
2 2 Siva 9283746565 Madurai ChennaiLeft Outer Join:
Left outer join returns all the values in the first table and the matching values in the second table with Null values in the nonmatching column.Query:
select a.Id, b.Sno, a.Name, a.PhoneNo, b.ResidentAddress, b.OfficeAddress
from Detail a left outer join Address b on a.Id = b.SnoOutput:
Id Sno Name PhoneNo Resident Office
1 1 Ramesh 9786756453 Trichy Delhi
2 2 Siva 9283746565 Madurai Chennai
3 NULL Kamal 9192939495 NULL NULLRight Outer Join:
Right outer join returns all the values in the second table and the matching values in the first table with Null values in the nonmatching column.Query:
select a.Id, b.Sno, a.Name, a.PhoneNo, b.ResidentAddress, b.OfficeAddress
from Detail a right outer join Address b on a.Id = b.SnoOutput:
Id Sno Name PhoneNo Resident Office
1 1 Ramesh 9786756453 Trichy Delhi
2 2 Siva 9283746565 Madurai Chennai
NULL 4 NULL NULL Coimbatore BangloreFull Outer Join:
Full outer join returns all the value in both the table and NULL value in nonmatching column of both the tables.Query:
select a.Id, b.Sno, a.Name, a.PhoneNo, b.ResidentAddress, b.OfficeAddress
from Detail a full outer join Address b on a.Id = b.SnoOutput:
Id Sno Name PhoneNo Resident Office
1 1 Ramesh 9786756453 Trichy Delhi
2 2 Siva 9283746565 Madurai Chennai
3 NULL Kamal 9192939495 NULL NULL
NULL 4 NULL NULL Coimbatore BangloreCross Join:
Cross join gives the cartesian product of both the tables.Query:
select a.Id, b.Sno, a.Name, a.PhoneNo, b.ResidentAddress, b.OfficeAddress
from Detail a cross join Address b Output:
Id Sno Name PhoneNo Resident Office
1 1 Ramesh 9786756453 Trichy Delhi
2 1 Siva 9283746565 Trichy Delhi
3 1 Kamal 9192939495 Trichy Delhi
1 2 Ramesh 9786756453 Madurai Chennai
2 2 Siva 9283746565 Madurai Chennai
3 2 Kamal 9192939495 Madurai Chennai
1 4 Ramesh 9786756453 Coimbatore Banglore
2 4 Siva 9283746565 Coimbatore Banglore
3 4 Kamal 9192939495 Coimbatore BangloreSelf Join:
Self join is the process of joining the table with itself. To compare two columns of same table we can use self join with different alias name.Query:
select a.* from Detail a join Detail b on a.Id=b.IdOutput:
Id Name PhoneNo
1 Ramesh 9786756453
2 Siva 9283746565
3 Kamal 9192939495
Hi,
Thanks for this Nice post!
How to use multiple table in Inner Join ?
Thanks and Regards
Vinodh