0% found this document useful (0 votes)
1 views

SQL Joins

Uploaded by

assistant0849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

SQL Joins

Uploaded by

assistant0849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SQL Joins

Types of Joins
1) Inner Join
• Natural Join
2) Left (Outer) Join
3) Right (Outer) Join
4) (Full) Outer Join
5) Left (Outer) Join Excluding Inner Join
6) Right (Outer) Join Excluding Inner Join
7) (Full) Outer Join Excluding Inner Join
8) Cross Join
9) Equi-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
• Inner Joins do not have
to use equality to join
the fields
• Can use <, >, <>
• Most commonly
used
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 Table A


INNER JOIN TableB ON
TableA.PK = TableB.PK

• This is the same as doing


SELECT * FROM TableA, TableB WHERE TableA.PK =
TableB.PK
Inner Join (continued)
SELECT * TableA TableB
PK Value PK Value
FROM Table A 2 COP 1 TROT
INNER JOIN TableB 3 TAXI 1 TROT
ON TableA.PK > 3 TAXI 2 CAR
TableB.PK 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
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 TableB
Value PK 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 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
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 TableB
Value PK 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
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
TableA TableB
Value PK PK Value
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
LUCENT 10 NULL NULL

• 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.
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
TableA TableB
Value PK PK Value
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH

• 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.
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.
Full Outer Excluding Inner Join
TableA TableB
Value PK 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

• SELECT * FROM TableA FULL OUTER JOIN TableB


ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL OR
TableB.PK IS 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

You might also like