You are on page 1of 4

Joins

AVisualExplanationofSQLJoins
Assumewehavethefollowingtwotables.TableAisontheleft,andTableBisontheright. We'llpopulatethemwithfourrecordseach.

Let'sjointhesetablesbythenamefieldinafewdifferentwaysandseeifwecangeta conceptualmatchtothoseniftyVenndiagrams.

InnerjoinproducesonlythesetofrecordsthatmatchinbothTableAandTableB.

FullouterjoinproducesthesetofallrecordsinTableAandTableB,withmatchingrecords frombothsideswhereavailable.Ifthereisnomatch,themissingsidewillcontainnull.

LeftouterjoinproducesacompletesetofrecordsfromTableA,withthematchingrecords (whereavailable)inTableB.Ifthereisnomatch,therightsidewillcontainnull.

ToproducethesetofrecordsonlyinTableA,butnotinTableB,weperformthesameleftouter join,thenexcludetherecordswedon'twantfromtherightsideviaawhereclause.

ToproducethesetofrecordsuniquetoTableAandTableB,weperformthesamefullouter join,thenexcludetherecordswedon'twantfrombothsidesviaawhereclause.

There'salsoacartesianproductorcrossjoin,whichasfarasIcantell,can'tbeexpressedas aVenndiagram:

Thisjoins"everythingtoeverything",resultingin4x4=16rows,farmorethanwehadinthe originalsets.Ifyoudothemath,youcanseewhythisisaverydangerousjointorunagainst largetables.

You might also like