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


Related Articles

More articles: SQL Join SQL query SQL 2008 features

Comments

Author: Christopher F02 Jun 2010 Member Level: Gold   Points : 0

Good Article

Keep Posting

Thanks

Author: Narayana03 Jun 2010 Member Level: Gold   Points : 2

Hi ananta,

Your article is so good,but a small modification is

Left Outer Join:This join returns all the rows from the Left
---------------
table in conjuction with the matching rows from the right table.If there are no columns matching in the right table, it returns NULL values.
example: select * from Table1 t1 Leftouter JOIN Table2 t2 ON
-------
t1.col1= t2.col1.



Right Outer Join: This join returns all the rows from the Right
----------------
table in conjction with the matching rows from the Left table .If there are no columns matching in the Left table,it returns NULL values.

example: select * from Table1 t1 Righttouter JOIN Table2 t2 ON
-------
t1.col1= t2.col1.

keep posting byee...



Author: Jivani25 Apr 2013 Member Level: Gold   Points : 0

you can best example show to the join

to blow link open

http://bharat1990.wordpress.com/2013/04/25/types-of-join-in-sql-server/



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: