SQL Server Join Types

Microsoft SQL Server Programming FAQ Index ( good collection of topics on SQL Server).
http://www.tek-tips.com/faq.cfm?pid=183

JOIN Types the following is a summary of http://www.tek-tips.com/faqs.cfm?fid=4785

**********************************************************
– INNER JOIN
**********************************************************

Ex.
SELECT Team.Name, Matches.id
FROM Team INNER JOIN
Matches ON Team.id = Matches.HomeTeam

Note:
Returns all rows in which a column on the left table matches one on the right.

**********************************************************
– OUTER JOIN
**********************************************************
1) LEFT OUTER JOIN:
Ex.
SELECT Team.Name, Matches.id
FROM Team LEFT OUTER JOIN
Matches ON Team.id = Matches.HomeTeam

Note:
Similar to the INNER JOIN except that for all records in which a column does not exist for the left hand side table a NULL is returned

2) RIGHT OUTER JOIN
Ex.
SELECT Team.Name, Matches.id
FROM Team RIGHT OUTER JOIN
Matches ON Team.id = Matches.HomeTeam

Note:
RIGHT OUTER JOIN – This JOIN is just like the LEFT OUTER JOIN except the Right_Table is the table that will have every one of its records in the result set and if no record is found in the
Left_Table then its columns in the result set will be NULL

3) FULL OUTER JOIN
Ex.
SELECT Team.Name, Matches.id
FROM Team FULL OUTER JOIN
Matches ON Team.id = Matches.HomeTeam

Note:
FULL OUTER JOIN – This JOIN is a combination of both. All records from both Left_Table and Right_Table are in the result set and matched when they can be on the Join_Condition when no record is found in the opposit table NULL values are used for the columns.
So a query of

**********************************************************
CROSS JOIN
**********************************************************
Ex.

Note:

Leave a Reply

Your email address will not be published. Required fields are marked *