February 10, 2014

SQL Server - Joins

INNER JOIN

This join will return the row that matches in both tables.

Select * from Table1 as tbl1
Inner Join Table2 as tbl2 on tbl1.column_name = tbl2.column_name

RIGHT OUTER JOIN

This join will return all the rows from right table along with the rows that matches in both tables. All the columns from left table which does not find match returns null value.
Select * from Table1 as tbl1
Right Outer Join Table2 as tbl2 on tbl1.column_name = tbl2.column_name

LEFT OUTER JOIN

This join will return all the rows from left table along with the rows that matches in both tables. All the columns from right table which does not find match returns null value.
Select * from Table1 as tbl1
Left Outer Join Table2 as tbl2 on tbl1.column_name = tbl2.column_name

FULL OUTER JOIN

This join returns all rows from both tables, matching up the rows wherever a match can be made and placing NULLs in the places where no matching row exists.
.

No comments: