SlideShare a Scribd company logo
1 of 23
Download to read offline
SQL Joins
Source material from Code Project article by C.L. Moffatt and
ā€œCoding Horrorā€ blog from 10/11/07
Types of Joins
ā€¢ Inner Join
ā€¢ Natural Join
ā€¢ Left (Outer) Join
ā€¢ Right (Outer) Join
ā€¢ (Full) Outer Join
ā€¢ Left (Outer) Join Excluding Inner Join
ā€¢ Right (Outer) Join Excluding Inner Join
ā€¢ (Full) Outer Join Excluding Inner Join
ā€¢ Cross Join
ā€¢ Equi-Join
Sample Tables
PK Value
1 FOX
2 COP
3 TAXI
6 WASHINGTON
7 DELL
5 ARIZONA
4 LINCOLN
10 LUCENT
TableA
PK Value
1 TROT
2 CAR
3 CAB
6 MONUMENT
7 PC
8 MICROSOFT
9 APPLE
11 SCOTCH
TableB
Inner Join
ā€¢ Inner join produces
only the set of
records that match in
both Table A and
Table B
ā€¢ Most commonly
used, best
understood join
Inner Join
SELECT * FROM TableA INNER JOIN TableB ON
TableA.PK = TableB.PK
ā€¢ This is the same as doing
SELECT * FROM TableA, TableB WHERE TableA.PK =
TableB.PK
TableA
Value PK
TableB
PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
Inner Join (continued)
ā€¢ Inner Joins do not have to use equality to
join the fields
ā€¢ Can use <, >, <>
Inner Join (continued)
SELECT * FROM
TableA INNER
JOIN TableB ON
TableA.PK >
TableB.PK
TableA
PK Value
TableB
PK Value
2 COP 1 TROT
3 TAXI 1 TROT
3 TAXI 2 CAR
4 LINCOLN 1 TROT
4 LINCOLN 2 CAR
4 LINCOLN 3 CAB
5 ARIZONA 1 TROT
5 ARIZONA 2 CAR
5 ARIZONA 3 CAB
ā€¦ Moreā€¦ Rowsā€¦
Inner Join/Natural Join
ā€¢ A NATURAL join is just an inner equi-join where the join is
implicitly created using any matching columns between the
two tables
ā€¢ Example:
ā€¢ SELECT * FROM TableA NATURAL JOIN TableB
ā€¢ Same results as inner equi-join?
ā€¢ Which columns match?
Left Outer Join
ā€¢ Left outer join
produces a complete
set of records from
Table A, with the
matching records
(where available) in
Table B. If there is no
match, the right side
will contain null.
Left Outer Join
ā€¢ SELECT * FROM TableA LEFT OUTER JOIN TableB
ON TableA.PK = TableB.PK
TableA
Value PK
TableB
PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
LUCENT 10 NULL NULL
Right Outer Join
ā€¢ Right outer join
produces a complete
set of records from
Table B, with the
matching records
(where available) in
Table A. If there is no
match, the left side
will contain null.
Right Outer Join
ā€¢ SELECT * FROM TableA RIGHT OUTER JOIN
TableB ON TableA.PK = TableB.PK
TableA
Value PK
TableB
PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
Full Outer Join
ā€¢ Full outer join
produces the set of
all records in Table A
and Table B, with
matching records
from both sides
where available. If
there is no match,
the missing side will
contain null.
Full Outer Join
ā€¢ SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK
= TableB.PK
TableA
Value PK
TableB
PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
LUCENT 10 NULL NULL
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
Full Outer Join in MySQL
ā€¢ MySQL doesnā€™t have FULL OUTER JOIN
ā€¢ Simulate using UNION, LEFT and RIGHT JOINs
ā€¢ SELECT * FROM TableA LEFT JOIN TableB
ON TableA.PK = TableB.PK
UNION
SELECT * FROM TableA RIGHT JOIN TableB
ON TableA.PK = TableB.PK
Left Join Excluding Inner Join
ā€¢ This query will return
all of the records in
the left table (table
A) that do not match
any records in the
right table (table B).
Left Join Excluding Inner Join
ā€¢ SELECT * FROM TableA LEFT JOIN TableB
ON TableA.PK = TableB.PK
WHERE TableB.PK IS NULL
ā€¢ Perform left outer join, then exclude the records
we don't want from the right side via a where
clause.
TableA
Value PK
TableB
PK Value
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
LUCENT 10 NULL NULL
Right Join Excluding Inner Join
ā€¢ This query will return
all of the records in
the right table (table
B) that do not match
any records in the
left table (table A).
Right Join Excluding Inner Join
ā€¢ SELECT * FROM TableA RIGHT JOIN TableB
ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL
ā€¢ Perform right outer join, then exclude the
records we don't want from the left side via a
where clause.
TableA
Value PK
TableB
PK Value
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
Full Outer Excluding Inner Join
ā€¢ This query will return
all of the records in
Table A and Table B
that do not have a
matching record in
the other table.
ā€¢ (If you find a useful
application, let me
know! ļŠ)
Full Outer Excluding Inner Join
ā€¢ SELECT * FROM TableA FULL OUTER JOIN TableB
ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL OR
TableB.PK IS NULL
TableA
Value PK
TableB
PK Value
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
LUCENT 10 NULL NULL
How Can We Do This in MySQL?
ā€¢ MySQL doesnā€™t have FULL OUTER JOIN
ā€¢ Simulate using UNION, LEFT and RIGHT JOINs
ā€¢ SELECT * FROM TableA LEFT JOIN TableB
ON TableA.PK = TableB.PK
WHERE TableB.PK IS NULL
UNION
SELECT * FROM TableA RIGHT JOIN TableB
ON TableA.PK = TableB.PK
WHERE TABLEA.PK IS NULL
Cross Join
ā€¢ A cross join is a Cartesian Product join ā€“ it is
every record in Table A combined with every
record in Table B.
ā€¢ It gives the same results as not using a WHERE
clause when querying two tables in MySQL
ā€¢ SELECT * from TableA CROSS JOIN TableB
ā€¢ SELECT * from TableA, TableB

More Related Content

What's hot

Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
Ā 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
Nargis Ehsan
Ā 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalization
daxesh chauhan
Ā 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
Naresh Kumar
Ā 

What's hot (20)

Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & Relationship
Ā 
Entity relationship diagram - Concept on normalization
Entity relationship diagram - Concept on normalizationEntity relationship diagram - Concept on normalization
Entity relationship diagram - Concept on normalization
Ā 
Selection sort
Selection sortSelection sort
Selection sort
Ā 
The Growing Importance of Data Cleaning
The Growing Importance of Data CleaningThe Growing Importance of Data Cleaning
The Growing Importance of Data Cleaning
Ā 
The Relational Database Model
The Relational Database ModelThe Relational Database Model
The Relational Database Model
Ā 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
Ā 
DBMS and its Models
DBMS and its ModelsDBMS and its Models
DBMS and its Models
Ā 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
Ā 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalization
Ā 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
Ā 
Sql joins
Sql joinsSql joins
Sql joins
Ā 
SQL
SQLSQL
SQL
Ā 
Components and Advantages of DBMS
Components and Advantages of DBMSComponents and Advantages of DBMS
Components and Advantages of DBMS
Ā 
Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)
Ā 
Database Normalization
Database NormalizationDatabase Normalization
Database Normalization
Ā 
Sql joins
Sql joinsSql joins
Sql joins
Ā 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Ā 
Query processing
Query processingQuery processing
Query processing
Ā 
All data models in dbms
All data models in dbmsAll data models in dbms
All data models in dbms
Ā 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Ā 

Similar to SQL Joins.pdf

Sap internal
Sap   internalSap   internal
Sap internal
kyashpal
Ā 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Joshi Vinay
Ā 

Similar to SQL Joins.pdf (15)

Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)
Ā 
Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer Join
Ā 
Join
JoinJoin
Join
Ā 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdf
Ā 
Join query
Join queryJoin query
Join query
Ā 
Sap internal
Sap   internalSap   internal
Sap internal
Ā 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
Ā 
Bootcamp sql fundamentals bootcamp_part4
Bootcamp   sql fundamentals  bootcamp_part4Bootcamp   sql fundamentals  bootcamp_part4
Bootcamp sql fundamentals bootcamp_part4
Ā 
Internal tables
Internal tables Internal tables
Internal tables
Ā 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
Ā 
MS SQLSERVER:Joining Databases
MS SQLSERVER:Joining DatabasesMS SQLSERVER:Joining Databases
MS SQLSERVER:Joining Databases
Ā 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
Ā 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
Ā 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Ā 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptx
Ā 

More from NiravPanchal50

SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdf
NiravPanchal50
Ā 
SQL and DBMS INTERVIEW QUESTIONS .pdf
SQL and DBMS INTERVIEW QUESTIONS .pdfSQL and DBMS INTERVIEW QUESTIONS .pdf
SQL and DBMS INTERVIEW QUESTIONS .pdf
NiravPanchal50
Ā 
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdfš™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
NiravPanchal50
Ā 
Javascript String Methods.pdf
Javascript String Methods.pdfJavascript String Methods.pdf
Javascript String Methods.pdf
NiravPanchal50
Ā 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
NiravPanchal50
Ā 

More from NiravPanchal50 (8)

SQL cheat sheet.pdf
SQL cheat sheet.pdfSQL cheat sheet.pdf
SQL cheat sheet.pdf
Ā 
SQL Short Notes.pdf
SQL Short Notes.pdfSQL Short Notes.pdf
SQL Short Notes.pdf
Ā 
SQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdfSQL EXCLUSIVE NOTES .pdf
SQL EXCLUSIVE NOTES .pdf
Ā 
SQL and DBMS INTERVIEW QUESTIONS .pdf
SQL and DBMS INTERVIEW QUESTIONS .pdfSQL and DBMS INTERVIEW QUESTIONS .pdf
SQL and DBMS INTERVIEW QUESTIONS .pdf
Ā 
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdfš™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
š™…š™–š™«š™– š™„š™£š™©š™šš™§š™«š™žš™šš™¬ š™Œš™Ŗš™šš™Øš™©š™žš™¤š™£š™Ø š™‰š™¤š™©š™šš™Ø.pdf
Ā 
Javascript String Methods.pdf
Javascript String Methods.pdfJavascript String Methods.pdf
Javascript String Methods.pdf
Ā 
Java Interview.pdf
Java Interview.pdfJava Interview.pdf
Java Interview.pdf
Ā 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
Ā 

Recently uploaded

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
Ā 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
Ā 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
Ā 

Recently uploaded (20)

Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
Ā 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
Ā 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
Ā 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
Ā 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
Ā 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
Ā 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
Ā 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
Ā 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
Ā 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
Ā 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Ā 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Ā 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Ā 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
Ā 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Ā 
BhubaneswaršŸŒ¹Call Girls Bhubaneswar ā¤Komal 9777949614 šŸ’Ÿ Full Trusted CALL GIRL...
BhubaneswaršŸŒ¹Call Girls Bhubaneswar ā¤Komal 9777949614 šŸ’Ÿ Full Trusted CALL GIRL...BhubaneswaršŸŒ¹Call Girls Bhubaneswar ā¤Komal 9777949614 šŸ’Ÿ Full Trusted CALL GIRL...
BhubaneswaršŸŒ¹Call Girls Bhubaneswar ā¤Komal 9777949614 šŸ’Ÿ Full Trusted CALL GIRL...
Ā 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
Ā 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
Ā 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
Ā 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
Ā 

SQL Joins.pdf

  • 1. SQL Joins Source material from Code Project article by C.L. Moffatt and ā€œCoding Horrorā€ blog from 10/11/07
  • 2. Types of Joins ā€¢ Inner Join ā€¢ Natural Join ā€¢ Left (Outer) Join ā€¢ Right (Outer) Join ā€¢ (Full) Outer Join ā€¢ Left (Outer) Join Excluding Inner Join ā€¢ Right (Outer) Join Excluding Inner Join ā€¢ (Full) Outer Join Excluding Inner Join ā€¢ Cross Join ā€¢ Equi-Join
  • 3. Sample Tables PK Value 1 FOX 2 COP 3 TAXI 6 WASHINGTON 7 DELL 5 ARIZONA 4 LINCOLN 10 LUCENT TableA PK Value 1 TROT 2 CAR 3 CAB 6 MONUMENT 7 PC 8 MICROSOFT 9 APPLE 11 SCOTCH TableB
  • 4. Inner Join ā€¢ Inner join produces only the set of records that match in both Table A and Table B ā€¢ Most commonly used, best understood join
  • 5. Inner Join SELECT * FROM TableA INNER JOIN TableB ON TableA.PK = TableB.PK ā€¢ This is the same as doing SELECT * FROM TableA, TableB WHERE TableA.PK = TableB.PK TableA Value PK TableB PK Value FOX 1 1 TROT COP 2 2 CAR TAXI 3 3 CAB WASHINGTON 6 6 MONUMENT DELL 7 7 PC
  • 6. Inner Join (continued) ā€¢ Inner Joins do not have to use equality to join the fields ā€¢ Can use <, >, <>
  • 7. Inner Join (continued) SELECT * FROM TableA INNER JOIN TableB ON TableA.PK > TableB.PK TableA PK Value TableB PK Value 2 COP 1 TROT 3 TAXI 1 TROT 3 TAXI 2 CAR 4 LINCOLN 1 TROT 4 LINCOLN 2 CAR 4 LINCOLN 3 CAB 5 ARIZONA 1 TROT 5 ARIZONA 2 CAR 5 ARIZONA 3 CAB ā€¦ Moreā€¦ Rowsā€¦
  • 8. Inner Join/Natural Join ā€¢ A NATURAL join is just an inner equi-join where the join is implicitly created using any matching columns between the two tables ā€¢ Example: ā€¢ SELECT * FROM TableA NATURAL JOIN TableB ā€¢ Same results as inner equi-join? ā€¢ Which columns match?
  • 9. Left Outer Join ā€¢ Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.
  • 10. Left Outer Join ā€¢ SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.PK = TableB.PK TableA Value PK TableB PK Value FOX 1 1 TROT COP 2 2 CAR TAXI 3 3 CAB LINCOLN 4 NULL NULL ARIZONA 5 NULL NULL WASHINGTON 6 6 MONUMENT DELL 7 7 PC LUCENT 10 NULL NULL
  • 11. Right Outer Join ā€¢ Right outer join produces a complete set of records from Table B, with the matching records (where available) in Table A. If there is no match, the left side will contain null.
  • 12. Right Outer Join ā€¢ SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.PK = TableB.PK TableA Value PK TableB PK Value FOX 1 1 TROT COP 2 2 CAR TAXI 3 3 CAB WASHINGTON 6 6 MONUMENT DELL 7 7 PC NULL NULL 8 MICROSOFT NULL NULL 9 APPLE NULL NULL 11 SCOTCH
  • 13. Full Outer Join ā€¢ Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.
  • 14. Full Outer Join ā€¢ SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK = TableB.PK TableA Value PK TableB PK Value FOX 1 1 TROT COP 2 2 CAR TAXI 3 3 CAB LINCOLN 4 NULL NULL ARIZONA 5 NULL NULL WASHINGTON 6 6 MONUMENT DELL 7 7 PC LUCENT 10 NULL NULL NULL NULL 8 MICROSOFT NULL NULL 9 APPLE NULL NULL 11 SCOTCH
  • 15. Full Outer Join in MySQL ā€¢ MySQL doesnā€™t have FULL OUTER JOIN ā€¢ Simulate using UNION, LEFT and RIGHT JOINs ā€¢ SELECT * FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.PK UNION SELECT * FROM TableA RIGHT JOIN TableB ON TableA.PK = TableB.PK
  • 16. Left Join Excluding Inner Join ā€¢ This query will return all of the records in the left table (table A) that do not match any records in the right table (table B).
  • 17. Left Join Excluding Inner Join ā€¢ SELECT * FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.PK WHERE TableB.PK IS NULL ā€¢ Perform left outer join, then exclude the records we don't want from the right side via a where clause. TableA Value PK TableB PK Value LINCOLN 4 NULL NULL ARIZONA 5 NULL NULL LUCENT 10 NULL NULL
  • 18. Right Join Excluding Inner Join ā€¢ This query will return all of the records in the right table (table B) that do not match any records in the left table (table A).
  • 19. Right Join Excluding Inner Join ā€¢ SELECT * FROM TableA RIGHT JOIN TableB ON TableA.PK = TableB.PK WHERE TableA.PK IS NULL ā€¢ Perform right outer join, then exclude the records we don't want from the left side via a where clause. TableA Value PK TableB PK Value NULL NULL 8 MICROSOFT NULL NULL 9 APPLE NULL NULL 11 SCOTCH
  • 20. Full Outer Excluding Inner Join ā€¢ This query will return all of the records in Table A and Table B that do not have a matching record in the other table. ā€¢ (If you find a useful application, let me know! ļŠ)
  • 21. Full Outer Excluding Inner Join ā€¢ SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK = TableB.PK WHERE TableA.PK IS NULL OR TableB.PK IS NULL TableA Value PK TableB PK Value NULL NULL 8 MICROSOFT NULL NULL 9 APPLE NULL NULL 11 SCOTCH LINCOLN 4 NULL NULL ARIZONA 5 NULL NULL LUCENT 10 NULL NULL
  • 22. How Can We Do This in MySQL? ā€¢ MySQL doesnā€™t have FULL OUTER JOIN ā€¢ Simulate using UNION, LEFT and RIGHT JOINs ā€¢ SELECT * FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.PK WHERE TableB.PK IS NULL UNION SELECT * FROM TableA RIGHT JOIN TableB ON TableA.PK = TableB.PK WHERE TABLEA.PK IS NULL
  • 23. Cross Join ā€¢ A cross join is a Cartesian Product join ā€“ it is every record in Table A combined with every record in Table B. ā€¢ It gives the same results as not using a WHERE clause when querying two tables in MySQL ā€¢ SELECT * from TableA CROSS JOIN TableB ā€¢ SELECT * from TableA, TableB