0% found this document useful (0 votes)
130 views

SQL Server Joins

This document provides an introduction to different types of JOINs in SQL, including: INNER JOIN, which returns rows when there is a match in both tables. OUTER JOIN has three types: LEFT OUTER JOIN returns all rows from the left table plus matched rows from the right table, RIGHT OUTER JOIN returns all rows from the right table plus matched rows from the left table, and FULL OUTER JOIN combines LEFT and RIGHT OUTER JOINS. CROSS JOIN returns the cartesian product of the tables without a join condition. The document also provides examples of using OUTER JOIN to replace NOT IN queries.

Uploaded by

Gopi Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

SQL Server Joins

This document provides an introduction to different types of JOINs in SQL, including: INNER JOIN, which returns rows when there is a match in both tables. OUTER JOIN has three types: LEFT OUTER JOIN returns all rows from the left table plus matched rows from the right table, RIGHT OUTER JOIN returns all rows from the right table plus matched rows from the left table, and FULL OUTER JOIN combines LEFT and RIGHT OUTER JOINS. CROSS JOIN returns the cartesian product of the tables without a join condition. The document also provides examples of using OUTER JOIN to replace NOT IN queries.

Uploaded by

Gopi Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL SERVER – Introduction to JOINs – Basic of JOINs

INNER JOIN
This join returns rows when there is at least one match in both the tables.

OUTER JOIN
There are three different Outer Join methods.
LEFTOUTERJOIN
This join returns all the rows from the left table in conjunction with the matching rows from the right
table. If there are no columns matching in the right table, it returns NULL values.
RIGHT OUTER JOIN
This join returns all the rows from the right table in conjunction with the matching rows from the left
table. If there are no columns matching in the left table, it returns NULL values.
FULL OUTER JOIN
This join combines left outer join and right after join. It returns row from either table when the
conditions are met and returns null value when there is no match.

CROSS JOIN
This join is a Cartesian join that does not necessitate any condition to join. The resultset contains
records that are multiplication of record number from both the tables.
Additional Notes related to JOIN:
The following are three classic examples to display where Outer Join is useful. You will notice several
instances where developers write query as given below.
SELECT t1.*
FROM Table1 t1
WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2)
GO

The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join
is the best practice. The query that gives same result as above is displayed here using Outer Join and
WHERE clause in join.
/*LEFTJOIN-WHERE NULL*/
SELECT t1.*,t2.*
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
The above example can also be created using Right Outer Join.
NOT INNER JOIN
Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer
Join is used along with WHERE condition, as explained in the above two examples, it will give you
exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.

You might also like