Wednesday, 23 October 2013

What is the Difference between SQL Inner Join and Outer Join

What is SQL JOIN Clause :
The SQL JOIN is a clause that enables a SELECT statement to access more than one table. The JOIN clause controls how tables are linked. It is a qualifier of the SQL FROM clause.

In SQL joins are used to get data from two or more tables based on relationship between some of the columns in tables. In most of the cases we will use primary key of first table and foreign key of secondary table to get data from tables by using this relationship we can reduce the duplication of data in every table.

Why and How to use JOIN Clause :
Use the SQL JOIN whenever multiple tables must be accessed through a SQL SELECT statement and no results should be returned if there is not a
match between the JOINed tables.

The ON clause describes the conditions of the JOIN.

SQL JOIN Syntax :

SELECT <column_name1>, <column_name2> <aggregate_function> FROM <table_name> JOIN <table_name> ON <join_conditions>

SQL JOIN Example : 
Let us suppose there are two tables called CUSTOMERS and ORDERS as follows

Customers Table 
CREATE TABLE CUSTOMERS(CID INT PRIMARY KEY IDENTITY(111,1) ,CNAME VARCHAR(15),LOCATION VARCHAR(15));

INSERT INTO CUSTOMERS VALUES ('JAN','HYDERABAD'),('FEB','BANGALORE'),('MAR','CHENNAI'),
('APR','PUNE'),('MAY','HYDERABAD'),('JUN','BANGALORE'),('JULY','BANGALORE');
  
CID
CNAME
LOCATION
111
JAN
HYDERABAD
112
FEB
BANGALORE
113
MAR
CHENNAI
114
APR
PUNE
115
MAY
HYDERABAD
116
JUN
BANGALORE
117
JULY
BANGALORE

 Orders Table
CREATE TABLE ORDERS(OID INT IDENTITY(1234,3),PRODUCT VARCHAR(15),CID INT);

INSERT INTO ORDERS VALUES('LAPTOP',113),('TV',115),('COOLER',117),
('FRIDGE',143),('PEN DRIVE',789);

OID
PRODUCT
CID
1234
LAPTOP
113
1237
TV
115
1240
COOLER
117
1243
FRIDGE
143
1246
PEN DRIVE
789

Now we perform the join on the above tables as follows 
SELECT C.CID,C.CNAME,C.LOCATION,O.OID,O.PRODUCT FROM CUSTOMERS C JOIN ORDERS O ON C.CID=O.CID ;

This join is also known as Normal Join or Inner Join.
The result of the Join is as follows :

CID
CNAME
LOCATION
OID
PRODUCT
113
MAR
CHENNAI
1234
LAPTOP
115
MAY
HYDERABAD
1237
TV
117
JULY
BANGALORE
1240
COOLER

Inner Join Vs Outer Join
The standard JOIN clause (also known as the INNER JOIN clause) differs from the OUTER JOIN in that rows are returned only when there are matches for the JOIN critieria on the second table.If there are no matches on the JOIN criteria then no rows will be returned. This is known an "INNER JOIN".

An INNER JOIN should be used when you only want to pull data that exists in both of the tables. Any entry found in the first table, that does not also have an entry in the second table, will not be included in the result set.


An OUTER JOIN should be used when you want to pull all of the matching data in the first table, regardless of whether or not it exists in the second table. Your result set will contain NULL for the variables that would have been found in the second table.


Use the "OUTER JOIN" in cases where rows should be returned when one side of the join is missing.

Notes :
A "Cartesian product" can result if there is no relation between the tables for the join. A row would be included for each combination between the two tables so if one table has 1,000 rows and the second table has 2,000 rows then 2,000,000 rows would be returned.

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts