Beruflich Dokumente
Kultur Dokumente
Teradata SQL
PREV NEXT
Chapter 7 - Aggregation Function Chapter 9 - Date Functions
AfricanProverb
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 1/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AtwotablejoinusingNonANSISyntax
SELECT Customer_Table.Customer_Number,
Customer_Name,
Order_Number,Order_Total
FROM Customer_Table,
Order_Table
WHERE Customer_Table.Customer_Number=
Order_Table.Customer_Number
AJoincombinescolumnsonthereportfrommorethanonetable.The
exampleabovejoinstheCustomer_TableandtheOrder_Tabletogether.
ThemostcomplicatedpartofanyjoinistheJOINCONDITION.The
JOINCONDITIONmeanswhatColumnfromeachtableisamatch.In
thiscase,Customer_Numberisamatchthatestablishestherelationship,
sothisjoinwillhappenonmatchingCustomer_Numbercolumns.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 2/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AtwotablejoinusingNonANSISyntaxwithTableAlias
SELECT Cust.Customer_Number,
Customer_Name,
Order_Number,Order_Total
FROM Customer_TableasCust,
Order_TableasORD
WHERE Cust.Customer_Number=
Ord.Customer_Number
AJoincombinescolumnsonthereportfrommorethanonetable.The
exampleabovejoinstheCustomer_TableandtheOrder_Tabletogether.
ThemostcomplicatedpartofanyjoinistheJOINCONDITION.The
JOINCONDITIONmeanswhatColumnfromeachtableisamatch.In
thiscase,Customer_Numberisamatchthatestablishestherelationship.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 3/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AliasesandFullyQualifyingColumns
Customer_NumberisacolumninboththeCustomerandOrderTables.
CUST.Customer_Numberfullyqualifiesthecolumntospecificallystate
wewanttheCustomer_NumberfromtheCustomer_Table.Thatiswhy
weALIASEDthetablenames,sowecouldfullyqualifyanycolumnsin
bothtables,orelsewereceiveanerror!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 4/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AtwotablejoinusingANSISyntax
ThisisthesamejoinasthepreviousslideexceptitisusingANSIsyntax.
Bothwillreturnthesamerowswiththesameperformance.Rowsare
joinedwhentheCustomer_Numbermatchesonbothtables,butnon
matcheswon'treturn.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 5/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
BothQuerieshavethesameResultsandPerformance
/*TraditionalSyntax*/
SELECT Cust.Customer_Number,
Customer_Name,
Order_Number,
Order_Total
FROM Customer_TableasCust,
Order_TableasORD
WHERE Cust.Customer_Number=
Ord.Customer_Number
/*ANSISyntax*/
SELECT Cust.Customer_Number,
Customer_Name,
Order_Number,
Order_Total
FROM Customer_TableasCust
INNERJOIN
Order_TableasORD
ON Cust.Customer_Number=
Ord.Customer_Number
Bothofthesesyntaxtechniquesbringbackthesameresultsetandhave
thesameperformance.TheINNERJOINisconsideredANSI.Whichone
doesOuterJoins?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 6/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizCanYouFinishtheJoinSyntax?
SELECTFirst_Name,Last_Name,
Department_Name
FROMEmployee_TableasE
INNERJOIN
Department_TableasD
ON
FinishthisjoinbyplacingthemissingSQLintheproperplace!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 7/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswertoQuizCanYouFinishtheJoinSyntax?
Thisqueryisreadytorun.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 8/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizCanYouFindtheError?
SELECTFirst_Name,Last_Name,Dept_No
Department_Name
FROMEmployee_TableasE
INNERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
Thisqueryhasanerror!Canyoufindit?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 9/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswertoQuizCanYouFindtheError?
IfacolumnintheSELECTlistisinbothtables,youmustfullyqualifyit.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 10/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWhichrowsfrombothtablesWon'tReturn?
SELECTFirst_Name,Last_Name,
Department_Name
FROMEmployee_TableasE
INNERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
AnInnerJoinreturnsmatchingrows,butdidyouknowanOuterJoin
returnsbothmatchingrowsandnonmatchingrows?Youwillunderstand
soon!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 11/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswertoQuizWhichrowsfrombothtablesWon'tReturn?
SquiggyJoneshasaNULLDept_No
RichardSmythehasaninvalid
Dept_No10
NoEmployeesworkinDepartment
500
Thebottomlineisthatthethreerowsexcludeddidnothaveamatching
Dept_No.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 12/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
LEFTOUTERJOIN
ThisisaLEFTOUTERJOIN.ThatmeansthatallrowsfromtheLEFT
Tablewillappearinthereportregardlessifitfindsamatchontheright
table.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 13/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
LEFTOUTERJOINBringsBackAllRowsintheLeftTable
LeftOUTERJOIN
SELECTFirst_Name,Department_Name
FROMEmployee_TableasE
LEFTOUTERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
First_Name Department_Name
Mandee Marketing
Herbert CustomerSupport
William CustomerSupport
Loraine Sales
Squiggy ?
Richard ?
Cletus CustomerSupport
Billy ResearchandDev
John ResearchandDev
ALEFTOuterJoinReturnsallrowsfromtheLEFTTable,includingall
Matches.IfaLEFTrowcan'tfindamatch,aNULLisplacedonright
columnsnotfound!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 14/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
RIGHTOUTERJOIN
ThisisaRIGHTOUTERJOIN.Thatmeansthatallrowsfromthe
RIGHTTablewillappearinthereportregardlessifitfindsamatchwith
theLEFTTable.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 15/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
RIGHTOUTERJOINBringsBackAllRowsintheRIGHTTable
RIGHTOUTERJOIN
SELECTFirst_Name,Department_Name
FROMEmployee_TableasE
RIGHTOUTERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
First_Name Department_Name
Mandee Marketing
Herbert CustomerSupport
William CustomerSupport
Loraine Sales
Cletus CustomerSupport
Billy ResearchandDev
John ResearchandDev
? HumanResources
AllrowsfromtheRightTablewerereturnedwithmatchesandDept_No
500didn'thaveamatch,sothesystemputaNULLValueforLeftColumn
values.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 16/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
FULLOUTERJOIN
ThisisaFULLOUTERJOIN.Thatmeansthatallrowsfromboththe
RIGHTandLEFTTablewillappearinthereportregardlessifitfindsa
match.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 17/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
FULLOUTERJOINBringsBackAllRowsinAllTables
FULLOUTERJOIN
SELECTFirst_Name,Department_Name
FROMEmployee_TableasE
FULLOUTERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
First_Name Department_Name
Mandee Marketing
Herbert CustomerSupport
William CustomerSupport
Loraine Sales
Squiggy
?
Richard ?
Cletus CustomerSupport
Billy ResearchandDev
John ResearchandDev
HumanResources
?
TheFULLOuterJoinReturnsallrowsfrombothTables.NULLsshow
theflaws!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 18/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
WhichTablesaretheLeftandwhicharetheRight?
SELECTCla.Claim_Id,
Cla.Claim_Date,
SUB.Last_Name,
SUB.First_Name,
ADD.Phone,
SER.Service_Pay,
PRO.Provider_Code,
PRO.Provider_Name,
FROMCLAIMSCla
LEFTOUTERJOINPROVIDERSPRO
ONCla.Provider_No=PRO.Provider_Code
LEFTOUTERJOINSERVICESSER
ONCla.Claim_Service=SER.Service_Code
LEFTOUTERJOINSUBSCRIBERSSUB
ONCla.Subscriber_No=SUB.Subscriber_No
ANDCla.Member_No=SUB.Member_No
LEFTOUTERJOINADDRESSESADD
ONSUB.Subscriber_No=ADD.Subscriber_No
YourmissionistoshowwhichtablesareLeftTablesandwhichonesare
RightTables.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 19/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWhichTablesaretheLeftandwhicharetheRight?
SELECTCla.Claim_Id,
Cla.Claim_Date,
SUB.Last_Name,
SUB.First_Name,
ADD.Phone,
SER.Service_Pay,
PRO.Provider_Code,
PRO.Provider_Name,
FROMCLAIMSCla
LEFTOUTERJOINPROVIDERSPRO
ONCla.Provider_No=PRO.Provider_Code
LEFTOUTERJOINSERVICESSER
ONCla.Claim_Service=SER.Service_Code
LEFTOUTERJOINSUBSCRIBERSSUB
ONCla.Subscriber_No=SUB.Subscriber_No
ANDCla.Member_No=SUB.Member_No
LEFTOUTERJOINADDRESSESADD
ONSUB.Subscriber_No=ADD.Subscriber_No
AlltablesafterthefirsttableareeachRightTables.
TheSpoolfromeachjoinresultremainstheLeftTable
Thefirsttableisalwaysthelefttableandtherestaretherighttables.Itis
thespoolorintermediateresultsfromeachjointhatremainthelefttable.
InthiscaseallrowswillreturnfromtheClaimstable.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 20/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
INNERJOINwithAdditionalANDClause
TheadditionalANDisperformedfirstinordertoeliminateunwanted
data,sothejoinislessintensivethanjoiningeverythingfirstandthen
eliminating.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 21/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
ANSIINNERJOINwithAdditionalANDClause
TheadditionalANDisperformedfirstinordertoeliminateunwanted
data,sothejoinislessintensivethanjoiningeverythingfirstandthen
eliminatingafter.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 22/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
ANSIINNERJOINwithAdditionalWHEREClause
TheadditionalANDisperformedfirstinordertoeliminateunwanted
data,sothejoinislessintensivethanjoiningeverythingfirstandthen
eliminatingafter.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 23/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
OUTERJOINwithAdditionalWHEREClause
TheadditionalWHEREisalwaysperformedlastonOuterJoins.Allrows
willbejoinedfirst,andthentheadditionalWHEREclausefiltersafter
thejointakesplace.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 24/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
OUTERJOINwithAdditionalANDClause
TheadditionalANDisperformedinconjunctionwiththeONstatement
onOuterJoins.AllrowswillbeevaluatedwiththeONclauseandthe
ANDcombined.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 25/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
ResultsfromOUTERJOINwithAdditionalANDClause
OUTERJoinwithadditionalAND
SELECTFirst_Name,Department_Name
FROMEmployee_TableasE
LEFTOUTERJOIN
Department_TableasD
ONE.Dept_No=D.Dept_No
ANDE.Dept_No=100
First_Name Department_Name
Mandee Marketing
Herbert ?
William ?
Loraine ?
Squiggy ?
Richard ?
Cletus ?
Billy ?
John ?
TheadditionalANDisperformedinconjunctionwiththeONstatement
onOuterJoins.Thiscansurpriseyou.OnlyMandeeisinDept_No100!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 26/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWhyisthisconsideredanINNERJOIN?
ThisisconsideredanINNERJOINbecausewearedoingaLEFTOUTER
JOINontheEmployee_Table,andthenfilteringwiththeANDfora
columnintherighttable!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 27/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheDREADEDProductJoin
ThisquerybecomesaProductJoinbecauseitdoesnotpossessanyJOIN
Conditions(JoinKeys).Everyrowfromonetableiscomparedtoevery
rowoftheothertable,andquiteoftenthedataisnotwhatyouintendedto
getback.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 28/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
ResultSetoftheDREADEDProductJoin
HowcanBillyCoffingworkin3differentdepartments?
AProductJoinisoftenamistake!ThreeDepartmentrowshadanmin
theirname.Thesewerejoinedtoeveryemployeeandtheinformationis
worthless.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 29/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheHorrifyingCartesianProductJoin
Thisjoinseveryrowfromonetabletoeveryrowofanothertable.Nine
rowsmultipliedby5rows=45rowsofcompletenonsense!
ACartesianProductJoinisabigmistake.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 30/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheANSICartesianJoinwillERROR
Thiscausesanerror.ANSIwon'tletthisrununlessajoinconditionis
present.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 31/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizDotheseJoinsReturntheSameAnswerSet?
/*Query1*/
SELECTFirst_Name,
Department_Name
FROMEmployee_Table
INNERJOIN
Department_Table
/*Query2*/
SELECTFirst_Name,
Department_Name
FROMEmployee_Table,
Department_Table
Dothesetwoqueriesproducethesameresult?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 32/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerDotheseJoinsReturntheSameAnswerSet?
/*Query1*/
SELECTFirst_Name,
Department_Name
FROMEmployee_Table
INNERJOIN
Department_Table
/*Query2*/
SELECTFirst_Name,
Department_Name
FROMEmployee_Table,
Department_Table
Dothesetwoqueriesproducethesameresult?No,Query1Errorsdueto
ANSIsyntaxandnoONClause,butQuery2ProductJoinstobringback
junk!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 33/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheCROSSJOIN
ThisquerybecomesaProductJoinbecauseaCrossJoinisanANSI
ProductJoin.ItwillcompareeveryrowfromtheCustomer_Tableto
Order_Number123456intheOrder_Table.CheckouttheAnswerSeton
thenextpage.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 34/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheCROSSJOINAnswerSet
ThisCrossJoinproducesinformationthatquiteoftenjustisn'tworth
anything!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 35/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheSelfJoin
SELECTMgrs.Dept_No,Mgrs.Last_NameasMgrName,
Mgrs.SalaryasMgrSal,
Emps.Last_NameasEmpName,Emps.SalaryasEmpsal
FROMEmployee_Table2asEmps,
Employee_Table2asMgrs
WHEREEmps.Dept_No=Mgrs.Dept_No
ANDMgrs.Mgr=Y
ANDEmps.Salary>Mgrs.Salary
WhichWorkersmakeabiggerSalarythantheirManager?
ASelfJoingivesitself2differentAliases,whichisthenseenastwo
differenttables.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 36/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheSelfJoinwithANSISyntax
SELECTMgrs.Dept_No,Mgrs.Last_NameasMgrName,
Mgrs.SalaryasMgrSal,
Emps.Last_NameasEmpName,Emps.SalaryasEmpsal
FROMEmployee_Table2asEmps
INNERJOIN
Employee_Table2asMgrs
ONEmps.Dept_No=Mgrs.Dept_No
WHEREMgrs.Mgr=Y
ANDEmps.Salary>Mgrs.Salary
WhichWorkersmakeabiggerSalarythantheirManager?
ASelfJoingivesitself2differentAliases,whichisthenseenastwo
differenttables.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 37/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWillbothqueriesbringbackthesameAnswerSet?
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
INNERJOINOrder_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
WHERE
Customer_NamelikeBilly%
ORDERBY1
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
INNERJOINOrder_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
AND
Customer_NamelikeBilly%
ORDERBY1
Willbothqueriesbringbackthesameresultset?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 38/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWillbothqueriesbringbackthesameAnswerSet?
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
INNERJOINOrder_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
WHERE
Customer_NamelikeBilly%
ORDERBY1
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
INNERJOINOrder_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
AND
Customer_NamelikeBilly%
ORDERBY1
Willbothqueriesbringbackthesameresultset?Yes!Theyarebothinner
joins,sotheybringbackthesameresults.Hadtheybeenouterjoins,the
WHEREandtheANDwouldactdifferently.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 39/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWillbothqueriesbringbackthesameAnswerSet?
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
LEFTOUTERJOIN
Order_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
WHERE
Customer_NamelikeBilly%
ORDERBY1
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
LEFTOUTERJOIN
Order_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
AND
Customer_NamelikeBilly%
ORDERBY1
Willbothqueriesbringbackthesameresultset?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 40/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWillbothqueriesbringbackthesameAnswerSet?
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
LEFTOUTERJOIN
Order_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
WHERE
Customer_NamelikeBilly%
ORDERBY1
SELECTCustomer_Name,
Order_Number,
Order_Total
FROMCustomer_TableasCust
LEFTOUTERJOIN
Order_TableasORD
ONCust.Customer_Number
=Ord.Customer_Number
AND
Customer_NamelikeBilly%
ORDERBY1
Willbothqueriesbringbackthesameresultset?NO!TheWHEREis
performedlast.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 41/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
HowwouldyouJointhesetwotables?
Lookingatthecolumnsabove,whichwillallowthesetwotablestojoin?
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 42/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
HowwouldyouJointhesetwotables?YouCan'tYet!
GetreadyfortheAssociativeTable!
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 43/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnAssociativeTableisaBridgethatJoinsTwoTables
TheAssociativeTableisabridgebetweentheCourse_Tableand
Student_Table.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 44/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizCanyouWritethe3TableJoin?
SELECTALLColumnsfromtheCourse_TableandStudent_Tableand
Jointhem.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 45/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswertoQuizCanyouWritethe3TableJoin?
TheAssociativeTableisabridgebetweentheCourse_Tableand
Student_Table,anditssolepurposeistojointhesetwotablestogether.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 46/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizCanyouWritethe3TableJointoANSISyntax?
PleaserewritetheabovequeryusingANSISyntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 47/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerCanyouWritethe3TableJointoANSISyntax?
TraditionalSyntax
SELECTS.*,C.*
FROMStudent_TableasS,
Course_TableasC,
Student_Course_TableasSC
WhereS.Student_ID=SC.Student_ID
ANDC.Course_ID=SC.Course_ID
ANSISyntax
SelectS.*,C.*
FromStudent_TableasS
INNERJOIN
Student_Course_TableasSC
ONS.Student_ID=SC.Student_ID
INNERJOIN
Course_TableasC
ONC.Course_ID=SC.Course_ID
TheabovequeryhasbeenwrittenusingANSISyntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 48/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizCanyouPlacetheONClausesattheEnd?
SelectS.*,C.*
FromStudent_TableasS
INNERJOIN
Student_Course_TableasSC
ONS.Student_ID=SC.Student_ID
INNERJOIN
Course_TableasC
ONC.Course_ID=SC.Course_ID
PleaserewritetheabovequeryandplacebothONClausesattheend.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 49/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerCanyouPlacetheONClausesattheEnd?
Thisistricky.TheonlywayitworksistoplacetheONclausesbackwards.
ThefirstONClauserepresentsthelastINNERJOIN,andthenmoves
backwards.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 50/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
The5TableJoinLogicalInsuranceModel
AboveisthelogicalmodelfortheinsurancetablesshowingthePrimary
KeyandForeignKeyrelationships(PK/FK).
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 51/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWriteaFiveTableJoinUsingANSISyntax
YourmissionistowriteafivetablejoinselectingallcolumnsusingANSI
syntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 52/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWriteaFiveTableJoinUsingANSISyntax
SELECT
cla1.*,sub1.*,add1.*,pro1.*,ser1.*
FROMCLAIMSAScla1
INNERJOIN
SUBSCRIBERSASsub1
ONcla1.Subscriber_No=sub1.Subscriber_No
ANDcla1.Member_No=sub1.Member_No
INNERJOIN
ADDRESSESASadd1
ONsub1.Subscriber_No=add1.Subscriber_No
INNERJOIN
PROVIDERSASpro1
ONcla1.Provider_No=pro1.Provider_Code
INNERJOIN
SERVICESASser1
ONcla1.Claim_Service=ser1.Service_Code
AboveistheexamplewritingthisfivetablejoinusingANSIsyntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 53/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWriteaFiveTableJoinUsingANSISyntax
YourmissionistowriteafivetablejoinselectingallcolumnsusingANSI
syntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 54/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWriteaFiveTableJoinUsingANSISyntax
SELECT
cla1.*,sub1.*,add1.*,pro1.*,ser1.*
FROMCLAIMSAScla1
INNERJOIN
SUBSCRIBERSASsub1
ONcla1.Subscriber_No=sub1.Subscriber_No
ANDcla1.Member_No=sub1.Member_No
INNERJOIN
ADDRESSESASadd1
ONsub1.Subscriber_No=add1.Subscriber_No
INNERJOIN
PROVIDERSASpro1
ONcla1.Provider_No=pro1.Provider_Code
INNERJOIN
SERVICESASser1
ONcla1.Claim_Service=ser1.Service_Code
AboveistheexamplewritingthisfivetablejoinusingANSIsyntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 55/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizWriteaFiveTableJoinUsingNonANSISyntax
YourmissionistowriteafivetablejoinselectingallcolumnsusingNon
ANSIsyntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 56/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerWriteaFiveTableJoinUsingNonANSISyntax
SELECT cla1.*,sub1.*,add1.*,pro1.*,ser1.*
FROM CLAIMSAScla1,
SUBSCRIBERSASsub1,
ADDRESSESASadd1,
PROVIDERSASpro1,
SERVICESASser1
WHERE cla1.Subscriber_No=sub1.Subscriber_No
AND cla1.Member_No=sub1.Member_No
AND sub1.Subscriber_No=add1.Subscriber_No
AND cla1.Provider_No=pro1.Provider_Code
AND cla1.Claim_Service=ser1.Service_Code
AboveisanexampleofwritingthisfivetablejoinusingNonANSI
syntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 57/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
QuizReWritethisputtingtheONclausesattheEND
SELECT
cla1.*,sub1.*,add1.*,pro1.*,ser1.*
FROMCLAIMSAScla1
INNERJOIN
SUBSCRIBERSASsub1
ONcla1.Subscriber_No=sub1.Subscriber_No
ANDcla1.Member_No=sub1.Member_No
INNERJOIN
ADDRESSESASadd1
ONsub1.Subscriber_No=add1.Subscriber_No
INNERJOIN
PROVIDERSASpro1
ONcla1.Provider_No=pro1.Provider_Code
INNERJOIN
SERVICESASser1
ONcla1.Claim_Service=ser1.Service_Code
AboveisanexampleofwritingthisfivetablejoinusingNonANSI
syntax.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 58/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
AnswerReWritethisputtingtheONclausesattheEND
SELECTcla1.*,sub1.*,add1.*,pro1.*,ser1.*
FROMPROVIDERSASpro1
INNERJOIN
ADDRESSESASadd1
INNERJOIN
SUBSCRIBERSASsub1
INNERJOIN
SERVICESASser1
INNERJOIN
CLAIMSascla1
ONcla1.Claim_Service=ser1.Service_Code
ONcla1.Subscriber_No=sub1.Subscriber_No
ANDcla1.Member_No=sub1.Member_No
ONsub1.Subscriber_No=add1.Subscriber_No
ONcla1.Provider_No=pro1.Provider_Code
AboveisanexampleofwritingthisfivetablejoinusingANSIsyntaxwith
theONclausesattheend.Alsotomakethishappen,wehadtomovethe
tablesaround.NoticethatthefirstONclauserepresentsthelasttwo
tablesbeingjoinedandthenitworksbackwards.
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 59/60
4/7/2017 Chapter8JoinFunctionsTeradataSQL
TheNexusQueryChameleonWritestheSQLforUsers.
LetNexusshowusersthetablerelationshipsandthenletNexusbuildthe
SQL.JustloadtheERwinlogicalmodelinsideNexus,andthenallusers
canpointandclick.
PREV NEXT
Recommended / Queue / History
Chapter 7 - Aggregation / Topics / Tutorials / Settings / Blog / Get the App / Sign Out
Function Chapter 9 - Date Functions
2017 Safari. Terms of Service / Privacy Policy
https://www.safaribooksonline.com/library/view/teradatasql/9781940540153/chapter08.xhtml 60/60