0% found this document useful (0 votes)
8 views14 pages

SQL - Joins (1) .pptx-1

Uploaded by

roohibashir454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

SQL - Joins (1) .pptx-1

Uploaded by

roohibashir454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL Joins

Types of
Joins
• Inner Join/Equi-Join
• Natural Join
• Left (Outer) Join
• Right (Outer) Join
• (Full) Outer Join
• Cross Join
Sample
Tables
TableA TableB
PK Value PK Value
1 FOX 1 TROT
2 COP 2 CAR
3 TAXI 3 CAB
6 WASHINGTON 6 MONUMENT
7 DELL 7 PC
5 ARIZONA 8 MICROSOFT
4 LINCOLN 9 APPLE
10 LUCENT 11 SCOTCH
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
TableA TableB
Value PK PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC

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
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
• Natural Join in SQL joins two tables based on the same
attribute name and datatypes.
• In Natural Join, The resulting table will contain all the
attributes of both the tables but keep only one copy of
each common column
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
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

• SELECT * FROM TableA LEFT OUTER JOIN


TableB
ON TableA.PK = TableB.PK
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
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

• SELECT * FROM TableA RIGHT OUTER JOIN


TableB ON TableA.PK = TableB.PK
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
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
• SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK
= TableB.PK
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
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

You might also like