Sql join
SQL JOIN
Hi All
The SQL JOIN is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
A primary key is a column with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
Type of Join:
1. Inner Join
2. Outer Join
* Left Outer Join
* Right Outer Join
* Full Outer Join
3. Cross Join
INNER JOIN: This will only return rows when there is at least one row in both tables that match the join condition.
LEFT OUTER JOIN (or LEFT JOIN): This will return rows that have data in the left table (left of the JOIN keyword), even if there's no matching rows in the right table.
RIGHT OUTER JOIN (or RIGHT JOIN): This will return rows that have data in the right table (right of the JOIN keyword), even if there's no matching rows in the left table.
FULL OUTER JOIN (or FULL JOIN): This will return all rows, as long as there's matching data in one of the tables.
Join Syntax:
Example Inner Join
SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
Left Outer Join
Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table.
SELECT * FROM Individual AS Ind
LEFT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Right Outer Join
Use this when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table.
SELECT * FROM Individual AS Ind
RIGHT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Full Outer Join
Use this when you want to all rows, even if there's no matching rows in the right table.
SELECT * FROM Individual AS Ind
FULL JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Cross Join:
Cross join works as a Cartesian product of rows for both left and right table. It combined each row of left table with all the rows of right table.
SELECT * FROM GameScores
CROSS JOIN Departments
Reference: http://msdn.microsoft.com/en-us/library/aa196318%28SQL.80%29.aspx
Good Article
Keep Posting
Thanks