SlideShare a Scribd company logo
3
Most read
7
Most read
16
Most read
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

PPTX
Insertion and merge sort
PPTX
PPTX
Circular link list.ppt
PPT
SQL Tutorial - Basic Commands
PPTX
Normalization
PPTX
Ppt bubble sort
PPTX
linked list in data structure
PPTX
Priority queue in DSA
Insertion and merge sort
Circular link list.ppt
SQL Tutorial - Basic Commands
Normalization
Ppt bubble sort
linked list in data structure
Priority queue in DSA

What's hot (20)

PDF
Linked list implementation of Stack
DOC
Chapter-13-solutions
PDF
Expression trees
PPTX
Array ADT(Abstract Data Type)|Data Structure
PPTX
Introduction to data structure
PPTX
PPTX
Stacks and Queue - Data Structures
PPT
Database Triggers
PPTX
Phone Book project in Data Structure C
PPT
Linked List
PPTX
Basic blocks - compiler design
PPTX
Graph in data structure
PPTX
SQL JOIN
PPTX
Normal forms
PPT
Sql join
PPT
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
PDF
Create table
PPTX
Tree - Data Structure
PPTX
Tower of Hanoi
PPTX
Dbms 4NF & 5NF
Linked list implementation of Stack
Chapter-13-solutions
Expression trees
Array ADT(Abstract Data Type)|Data Structure
Introduction to data structure
Stacks and Queue - Data Structures
Database Triggers
Phone Book project in Data Structure C
Linked List
Basic blocks - compiler design
Graph in data structure
SQL JOIN
Normal forms
Sql join
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
Create table
Tree - Data Structure
Tower of Hanoi
Dbms 4NF & 5NF
Ad

Similar to SQL Joins.pdf (20)

PDF
SQL joins for Database Testing easy .pdf
PDF
Tipos de Joins para consultas em banco de dados.pdf
PPTX
join_SQL000000000000000000000000000000000.pptx
PDF
Advance database system(part 8)
PPTX
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
ย 
PPTX
Join in SQL - Inner, Self, Outer Join
PPTX
PDF
Lesson 6 - Relational Algebra.pdf
PPSX
Join query
DOCX
Sap internal
PPTX
sqljoins-220527115331-828e9932 (dee1).pptx
PPTX
SQL Joins.pptx
PPTX
Bootcamp sql fundamentals bootcamp_part4
PPT
joins IN DATA BASE MANAGEMENT SYSTEMSppt
PPT
Internal tables
PPT
Sql joins
PPTX
SQL JOINS- Reena P V
PPTX
MS SQLSERVER:Joining Databases
PPTX
MS Sql Server: Joining Databases
PPTX
MS SQL SERVER: Joining Databases
SQL joins for Database Testing easy .pdf
Tipos de Joins para consultas em banco de dados.pdf
join_SQL000000000000000000000000000000000.pptx
Advance database system(part 8)
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
ย 
Join in SQL - Inner, Self, Outer Join
Lesson 6 - Relational Algebra.pdf
Join query
Sap internal
sqljoins-220527115331-828e9932 (dee1).pptx
SQL Joins.pptx
Bootcamp sql fundamentals bootcamp_part4
joins IN DATA BASE MANAGEMENT SYSTEMSppt
Internal tables
Sql joins
SQL JOINS- Reena P V
MS SQLSERVER:Joining Databases
MS Sql Server: Joining Databases
MS SQL SERVER: Joining Databases
Ad

More from NiravPanchal50 (8)

PDF
SQL cheat sheet.pdf
PDF
SQL Short Notes.pdf
PDF
SQL EXCLUSIVE NOTES .pdf
PDF
SQL and DBMS INTERVIEW QUESTIONS .pdf
PDF
๐™…๐™–๐™ซ๐™– ๐™„๐™ฃ๐™ฉ๐™š๐™ง๐™ซ๐™ž๐™š๐™ฌ ๐™Œ๐™ช๐™š๐™จ๐™ฉ๐™ž๐™ค๐™ฃ๐™จ ๐™‰๐™ค๐™ฉ๐™š๐™จ.pdf
PDF
Javascript String Methods.pdf
PDF
Java Interview.pdf
PDF
Java Algorithm Interview Questions & Answers .pdf
SQL cheat sheet.pdf
SQL Short Notes.pdf
SQL EXCLUSIVE NOTES .pdf
SQL and DBMS INTERVIEW QUESTIONS .pdf
๐™…๐™–๐™ซ๐™– ๐™„๐™ฃ๐™ฉ๐™š๐™ง๐™ซ๐™ž๐™š๐™ฌ ๐™Œ๐™ช๐™š๐™จ๐™ฉ๐™ž๐™ค๐™ฃ๐™จ ๐™‰๐™ค๐™ฉ๐™š๐™จ.pdf
Javascript String Methods.pdf
Java Interview.pdf
Java Algorithm Interview Questions & Answers .pdf

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
The CXO Playbook 2025 โ€“ Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPTX
additive manufacturing of ss316l using mig welding
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPT
Project quality management in manufacturing
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
introduction to datamining and warehousing
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
ย 
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Embodied AI: Ushering in the Next Era of Intelligent Systems
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Construction Project Organization Group 2.pptx
The CXO Playbook 2025 โ€“ Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
additive manufacturing of ss316l using mig welding
III.4.1.2_The_Space_Environment.p pdffdf
Project quality management in manufacturing
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
introduction to datamining and warehousing
UNIT 4 Total Quality Management .pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
ย 
Fundamentals of safety and accident prevention -final (1).pptx
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Automation-in-Manufacturing-Chapter-Introduction.pdf

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