Professional Documents
Culture Documents
SQL:Joins
WithouttheadvertisementsonthiswebsitewewouldnotbeabletoofferTechOnTheNet.com.
PleasesupportusbywhitelistingTechOnTheNet.cominyouradblockingsoftware.
Youcanfindoutmoreabouthowweuseadvertisementshere.
SQL:JOINS
ThisSQLtutorialexplainshowtouseSQLjoinswithsyntax,visualillustrations,and
examples.
DESCRIPTION
SQLJOINSareusedtoretrievedatafrommultipletables.ASQLJOINisperformedwhenever
twoormoretablesarejoinedinaSQLstatement.
Thereare4differenttypesofSQLjoins:
SQLINNERJOIN(orsometimescalledsimplejoin)
SQLLEFTOUTERJOIN(orsometimescalledLEFTJOIN)
SQLRIGHTOUTERJOIN(orsometimescalledRIGHTJOIN)
SQLFULLOUTERJOIN(orsometimescalledFULLJOIN)
Solet'sdiscussSQLJOINsyntax,lookatvisualillustrationsofSQLJOINS,andexploreSQL
JOINexamples.
SQLINNERJOIN(SIMPLEJOIN)
Chancesare,you'vealreadywrittenaSQLstatementthatusesanSQLINNERJOIN.Itisthe
mostcommontypeofSQLjoin.SQLINNERJOINSreturnallrowsfrommultipletableswhere
thejoinconditionismet.
Syntax
ThesyntaxfortheSQLINNERJOINis:
SELECTcolumns
FROMtable1
INNERJOINtable2
ONtable1.column=table2.column;
VisualIllustration
http://www.techonthenet.com/sql/joins.php
1/10
25/3/2016
SQL:Joins
VisualIllustration
Inthisvisualdiagram,theSQLINNERJOINreturnstheshadedarea:
TheSQLINNERJOINwouldreturntherecordswheretable1andtable2intersect.
Example
HereisanexampleofaSQLINNERJOIN:
SELECTs.supplier_id,s.supplier_name,od.order_date
FROMsuppliersASs
INNERJOINorder_detailsASod
ONs.supplier_id=od.supplier_id;
ThisSQLINNERJOINexamplewouldreturnallrowsfromthesuppliersandorderstables
wherethereisamatchingsupplier_idvalueinboththesuppliersandorderstables.
Let'slookatsomedatatoexplainhowtheINNERJOINSwork:
Wehaveatablecalledsupplierswithtwofields(supplier_idandsupplier_name).Itcontains
thefollowingdata:
supplier_id
supplier_name
10000
IBM
10001
HewlettPackard
10002
Microsoft
10003
NVIDIA
Wehaveanothertablecalledorderswiththreefields(order_id,supplier_id,andorder_date).It
containsthefollowingdata:
order_id
supplier_id
order_date
500125
10000
2003/05/12
http://www.techonthenet.com/sql/joins.php
2/10
25/3/2016
SQL:Joins
500126
10001
2003/05/13
500127
10004
2003/05/14
IfweruntheSQLstatement(thatcontainsanINNERJOIN)below:
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers
INNERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
Ourresultsetwouldlooklikethis:
supplier_id
name
order_date
10000
IBM
2003/05/12
10001
HewlettPackard
2003/05/13
TherowsforMicrosoftandNVIDIAfromthesuppliertablewouldbeomitted,sincethe
supplier_id's10002and10003donotexistinbothtables.Therowfor500127(order_id)from
theorderstablewouldbeomitted,sincethesupplier_id10004doesnotexistinthesuppliers
table.
OldSyntax
Asafinalnote,itisworthmentioningthattheSQLINNERJOINexampleabovecouldbe
rewrittenusingtheolderimplicitsyntaxasfollows(butwestillrecommendusingtheINNER
JOINkeywordsyntax):
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers,orders
WHEREsuppliers.supplier_id=orders.supplier_id;
SQLLEFTOUTERJOIN
AnothertypeofjoiniscalledaLEFTOUTERJOIN.Thistypeofjoinreturnsallrowsfromthe
LEFThandtablespecifiedintheONconditionandonlythoserowsfromtheothertablewhere
thejoinedfieldsareequal(joinconditionismet).
Syntax
ThesyntaxfortheSQLLEFTOUTERJOINis:
http://www.techonthenet.com/sql/joins.php
3/10
25/3/2016
SQL:Joins
SELECTcolumns
FROMtable1
LEFT[OUTER]JOINtable2
ONtable1.column=table2.column;
Insomedatabases,theLEFTOUTERJOINkeywordsarereplacedwithLEFTJOIN.
VisualIllustration
Inthisvisualdiagram,theSQLLEFTOUTERJOINreturnstheshadedarea:
TheSQLLEFTOUTERJOINwouldreturntheallrecordsfromtable1andonlythoserecords
fromtable2thatintersectwithtable1.
Example
HereisanexampleofaSQLLEFTOUTERJOIN:
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers
LEFTOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
ThisLEFTOUTERJOINexamplewouldreturnallrowsfromthesupplierstableandonlythose
rowsfromtheorderstablewherethejoinedfieldsareequal.
Ifasupplier_idvalueinthesupplierstabledoesnotexistintheorderstable,allfieldsinthe
orderstablewilldisplayas<null>intheresultset.
Let'slookatsomedatatoexplainhowLEFTOUTERJOINSwork:
Wehaveatablecalledsupplierswithtwofields(supplier_idandsupplier_name).Itcontains
thefollowingdata:
supplier_id
supplier_name
10000
IBM
http://www.techonthenet.com/sql/joins.php
4/10
25/3/2016
SQL:Joins
10001
HewlettPackard
10002
Microsoft
10003
NVIDIA
Wehaveasecondtablecalledorderswiththreefields(order_id,supplier_id,andorder_date).
Itcontainsthefollowingdata:
order_id
supplier_id
order_date
500125
10000
2003/05/12
500126
10001
2003/05/13
IfweruntheSQLstatement(thatcontainsaLEFTOUTERJOIN)below:
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers
LEFTOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
Ourresultsetwouldlooklikethis:
supplier_id
supplier_name
order_date
10000
IBM
2003/05/12
10001
HewlettPackard
2003/05/13
10002
Microsoft
<null>
10003
NVIDIA
<null>
TherowsforMicrosoftandNVIDIAwouldbeincludedbecauseaLEFTOUTERJOINwas
used.However,youwillnoticethattheorder_datefieldforthoserecordscontainsa<null>
value.
OldSyntax
Asafinalnote,itisworthmentioningthattheLEFTOUTERJOINexampleabovecouldbe
rewrittenusingtheolderimplicitsyntaxthatutilizestheouterjoinoperator(+)asfollows(but
westillrecommendusingtheLEFTOUTERJOINkeywordsyntax):
http://www.techonthenet.com/sql/joins.php
5/10
25/3/2016
SQL:Joins
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers,orders
WHEREsuppliers.supplier_id=orders.supplier_id(+);
SQLRIGHTOUTERJOIN
AnothertypeofjoiniscalledaSQLRIGHTOUTERJOIN.Thistypeofjoinreturnsallrows
fromtheRIGHThandtablespecifiedintheONconditionandonlythoserowsfromtheother
tablewherethejoinedfieldsareequal(joinconditionismet).
Syntax
ThesyntaxfortheSQLRIGHTOUTERJOINis:
SELECTcolumns
FROMtable1
RIGHT[OUTER]JOINtable2
ONtable1.column=table2.column;
Insomedatabases,theRIGHTOUTERJOINkeywordsarereplacedwithRIGHTJOIN.
VisualIllustration
Inthisvisualdiagram,theSQLRIGHTOUTERJOINreturnstheshadedarea:
TheSQLRIGHTOUTERJOINwouldreturntheallrecordsfromtable2andonlythoserecords
fromtable1thatintersectwithtable2.
Example
HereisanexampleofaSQLRIGHTOUTERJOIN:
http://www.techonthenet.com/sql/joins.php
6/10
25/3/2016
SQL:Joins
SELECTorders.order_id,orders.order_date,suppliers.supplier_name
FROMsuppliers
RIGHTOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
ThisRIGHTOUTERJOINexamplewouldreturnallrowsfromtheorderstableandonlythose
rowsfromthesupplierstablewherethejoinedfieldsareequal.
Ifasupplier_idvalueintheorderstabledoesnotexistinthesupplierstable,allfieldsinthe
supplierstablewilldisplayas<null>intheresultset.
Let'slookatsomedatatoexplainhowRIGHTOUTERJOINSwork:
Wehaveatablecalledsupplierswithtwofields(supplier_idandsupplier_name).Itcontains
thefollowingdata:
supplier_id
supplier_name
10000
Apple
10001
Wehaveasecondtablecalledorderswiththreefields(order_id,supplier_id,andorder_date).
Itcontainsthefollowingdata:
order_id
supplier_id
order_date
500125
10000
2013/08/12
500126
10001
2013/08/13
500127
10002
2013/08/14
IfweruntheSQLstatement(thatcontainsaRIGHTOUTERJOIN)below:
SELECTorders.order_id,orders.order_date,suppliers.supplier_name
FROMsuppliers
RIGHTOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
Ourresultsetwouldlooklikethis:
order_id
order_date
supplier_name
500125
2013/08/12
Apple
500126
2013/08/13
http://www.techonthenet.com/sql/joins.php
7/10
25/3/2016
500127
SQL:Joins
2013/08/14
<null>
Therowfor500127(order_id)wouldbeincludedbecauseaRIGHTOUTERJOINwasused.
However,youwillnoticethatthesupplier_namefieldforthatrecordcontainsa<null>value.
OldSyntax
Asafinalnote,itisworthmentioningthattheRIGHTOUTERJOINexampleabovecouldbe
rewrittenusingtheolderimplicitsyntaxthatutilizestheouterjoinoperator(+)asfollows(but
westillrecommendusingtheRIGHTOUTERJOINkeywordsyntax):
SELECTorders.order_id,orders.order_date,suppliers.supplier_name
FROMsuppliers,orders
WHEREsuppliers.supplier_id(+)=orders.supplier_id;
SQLFULLOUTERJOIN
AnothertypeofjoiniscalledaSQLFULLOUTERJOIN.Thistypeofjoinreturnsallrowsfrom
theLEFThandtableandRIGHThandtablewithnullsinplacewherethejoinconditionisnot
met.
Syntax
ThesyntaxfortheSQLFULLOUTERJOINis:
SELECTcolumns
FROMtable1
FULL[OUTER]JOINtable2
ONtable1.column=table2.column;
Insomedatabases,theFULLOUTERJOINkeywordsarereplacedwithFULLJOIN.
VisualIllustration
Inthisvisualdiagram,theSQLFULLOUTERJOINreturnstheshadedarea:
http://www.techonthenet.com/sql/joins.php
8/10
25/3/2016
SQL:Joins
TheSQLFULLOUTERJOINwouldreturntheallrecordsfrombothtable1andtable2.
Example
HereisanexampleofaSQLFULLOUTERJOIN:
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers
FULLOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
ThisFULLOUTERJOINexamplewouldreturnallrowsfromthesupplierstableandallrows
fromtheorderstableandwheneverthejoinconditionisnotmet,<nulls>wouldbeextendedto
thosefieldsintheresultset.
Ifasupplier_idvalueinthesupplierstabledoesnotexistintheorderstable,allfieldsinthe
orderstablewilldisplayas<null>intheresultset.Ifasupplier_idvalueintheorderstable
doesnotexistinthesupplierstable,allfieldsinthesupplierstablewilldisplayas<null>inthe
resultset.
Let'slookatsomedatatoexplainhowFULLOUTERJOINSwork:
Wehaveatablecalledsupplierswithtwofields(supplier_idandsupplier_name).Itcontains
thefollowingdata:
supplier_id
supplier_name
10000
IBM
10001
HewlettPackard
10002
Microsoft
10003
NVIDIA
Wehaveasecondtablecalledorderswiththreefields(order_id,supplier_id,andorder_date).
Itcontainsthefollowingdata:
order_id
supplier_id
http://www.techonthenet.com/sql/joins.php
order_date
9/10
25/3/2016
SQL:Joins
500125
10000
2013/08/12
500126
10001
2013/08/13
500127
10004
2013/08/14
IfweruntheSQLstatement(thatcontainsaFULLOUTERJOIN)below:
SELECTsuppliers.supplier_id,suppliers.supplier_name,orders.order_date
FROMsuppliers
FULLOUTERJOINorders
ONsuppliers.supplier_id=orders.supplier_id;
Ourresultsetwouldlooklikethis:
supplier_id
supplier_name
order_date
10000
IBM
2013/08/12
10001
HewlettPackard
2013/08/13
10002
Microsoft
<null>
10003
NVIDIA
<null>
<null>
<null>
2013/08/14
TherowsforMicrosoftandNVIDIAwouldbeincludedbecauseaFULLOUTERJOINwas
used.However,youwillnoticethattheorder_datefieldforthoserecordscontainsa<null>
value.
Therowforsupplier_id10004wouldbealsoincludedbecauseaFULLOUTERJOINwas
used.However,youwillnoticethatthesupplier_idandsupplier_namefieldforthoserecords
containa<null>value.
OldSyntax
Asafinalnote,itisworthmentioningthattheFULLOUTERJOINexampleabovecouldnot
havebeenwrittenintheoldsyntaxwithoutusingaUNIONquery.
CopyrightTechOnTheNet.com
http://www.techonthenet.com/sql/joins.php
10/10