SQL JOINS.docx
Short Description
Download SQL JOINS.docx...
Description
join in SQL SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. SQL Join is used for combining column from two or more tables b y using values common to both tables. JoinKeyword is used in SQL queries for joining two or more tables. inimum required condition for joining table, is(n-1) where n, is number of tables. ! table can also join to itself "nown as, Self Join.
Types of Join #he following are the types of J$%& that we can use in SQL. •
%nner
•
$uter
•
Left
•
'ight
Cross JOIN or Cartesian Product #his type of J$%& returns the cartesian product of rows of from the tables in Join. %t will return a table which consists of records which combines each row from the first table with each row of the second table. (ross J$%& Synta) is, SELECT column-name-list from table-name1 CROSS JOIN
table-name2;; table-name2
Example of Cross JOIN #he class table,
ID
NAME
*
abhi
+
adam
ale)
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
Cross J$%& query will be, SELECT * from class, cross JOIN class_info;
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
*
-L/%
ale)
*
-L/%
ID
NAME
*
abhi
+
adam
ale)
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
Cross J$%& query will be, SELECT * from class, cross JOIN class_info;
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
*
-L/%
ale)
*
-L/%
*
abhi
+
01!%
+
adam
+
01!%
ale)
+
01!%
*
abhi
2
(/&&!%
+
adam
2
(/&&!%
ale)
2
(/&&!%
INNER Join or EQI Join #his is a simple J$%& in which the result is based on matched data as per the equality condition specified in the query. que ry. %nner Join Synta) is, SELECT column-name-list from table-name1 INNER JOIN
table-name2 WHERE tale-name!"column-name # tale-name$"column-name;
Example of Inner JOIN #he class table, ID
NAME
*
abhi
+
adam
2
ale)
anu
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
Inner J$%& query will be, SELECT * from class, class_info %&ere class"i' # class_info"i';
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
+
01!%
2
ale)
2
(/&&!%
Natural JOIN &atural Join is a type of %nner join which is based on column having same name and same datatype present in both the tables to be joined.
&atural Join Synta) is, SELECT * from table-name1 NATURAL JOIN
table-name2;
Example of Natural JOIN #he class table, ID
NAME
*
abhi
+
adam
2
ale)
anu
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
Natural join query ill !e" SELECT * from class N(T)R(L JOIN class_info;
#he result table will loo" li"e, ID
NAME
Address
*
abhi
-L/%
+
adam
01!%
2
ale)
(/&&!%
%n the above e)ample, both the tables being joined have %- column3same name and same datatype4, hence the records for which value of %- matches in both the tables will be the result of &atural Join of these two tables.
Outer JOIN $uter Join is based on both matched and unmatched data. $uter Joins subdivide further into, •
Left $uter Join
•
'ight $uter Join
•
5ull $uter Join
Left Outer Join #he left outer join returns a result table with the #atc$ed data of two tables then remaining rows of the lefttable and null for the ri%$t table6s column. Left $uter Join synta) is, SELECT column-name-list from table-name1 LEFT OUTER JOIN
table-name2
on tale-name!"column-name # tale-name$"column-name;
Left outer Join Synta) for &racle is, select column-name-list from table-name1, table-name2 on tale-name!"column-name # tale-name$"column-name ++;
Example of Left Outer Join #he class table, ID
NAME
*
abhi
+
adam
2
ale)
anu
7
ashish
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
8
&$%-!
9
:!&%:!#
'eft &uter Join query will be, SELECT * RO class LET O)TER JOIN class_info ON class"i'#class_info"i'+;
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
+
01!%
2
ale)
2
(/&&!%
anu
null
null
7
ashish
null
null
Ri!"t Outer Join #he right outer join returns a result table with the #atc$ed data of two tables then remaining rows of the ri%$t ta!le and null for the left table6s columns. 'ight $uter Join Synta) is, select column-name-list from table-name1 RIGHT OUTER JOIN
table-name2 on tale-name!"column-name # tale-name$"column-name;
'ight outer Join Synta) for &racle is, select column-name-list from table-name1, table-name2 on tale-name!"column-name ++ # tale-name$"column-name;
Example of Ri!"t Outer Join #he class table, ID
NAME
*
abhi
+
adam
2
ale)
anu
7
ashish
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
8
&$%-!
9
:!&%:!#
i%$t &uter Join query will be, SELECT * RO class RI.HT O)TER JOIN class_info on class"i'#class_info"i'+;
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
+
01!%
2
ale)
2
(/&&!%
null
null
8
&$%-!
null
null
9
:!&%:!#
#ull Outer Join #he full outer join returns a result table with the #atc$ed data of two table then remaining rows of both lefttable and then the ri%$t table. 5ull $uter Join Synta) is, select column-name-list from table-name1 FULL OUTER JOIN
table-name2 on tale-name!"column-name # tale-name$"column-name;
Example of #ull outer join is$ #he class table, ID
NAME
*
abhi
+
adam
2
ale)
anu
7
ashish
#he class_info table, ID
Address
*
-L/%
+
01!%
2
(/&&!%
8
&$%-!
9
:!&%:!#
ull &uter Join query will be li"e,
SELECT * RO class )LL O)TER JOIN class_info on class"i'#class_info"i'+;
#he result table will loo" li"e, ID
NAME
ID
Address
*
abhi
*
-L/%
+
adam
+
01!%
2
ale)
2
(/&&!%
anu
null
null
7
ashish
null
null
null
null
8
&$%-!
null
null
9
:!&%:!#
T/0es of Join in S1L Ser2er
Nirav Prabtani, $3 Jan $3!4 C5OL
!67"!8
6!
4"9! $6 2otes+
Rate t&is:
Vote!
T/0es of oin in S1L Ser2er for fetc&in< recor's from multi0le tales"
Intro'uction In t&is ti0, I am
View more...
Comments