Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality...

23
Chapter 4 Joining Multiple Tables

Transcript of Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality...

Page 1: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Chapter 4Joining Multiple Tables

Chapter 4Joining Multiple Tables

Page 2: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Purpose of JoinsPurpose of Joins

Joins are used to link tables and reconstruct data in a relational database

Joins can be created through:– Conditions in a WHERE clause– Use of JOIN keywords in FROM clause

Joins are used to link tables and reconstruct data in a relational database

Joins can be created through:– Conditions in a WHERE clause– Use of JOIN keywords in FROM clause

Page 3: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Cartesian JoinCartesian Join

Created by omitting joining condition in the WHERE clause or through CROSS JOIN keywords in the FROM clause

Results in every possible row combination (m * n)

Created by omitting joining condition in the WHERE clause or through CROSS JOIN keywords in the FROM clause

Results in every possible row combination (m * n)

Page 4: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Cartesian Join Example:Omitted ConditionCartesian Join Example:Omitted Condition

Page 5: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Equality JoinEquality Join

Links rows through equivalent data that exists in both tables

Created by:– Creating equivalency condition in the

WHERE clause– Using NATURAL JOIN, JOIN…USING, or

JOIN…ON keywords in the FROM clause

Links rows through equivalent data that exists in both tables

Created by:– Creating equivalency condition in the

WHERE clause– Using NATURAL JOIN, JOIN…USING, or

JOIN…ON keywords in the FROM clause

Page 6: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

JOIN Keyword OverviewJOIN Keyword Overview

Use NATURAL JOIN when tables have one column in common

Use JOIN…USING when tables have more than one column in common

Use JOIN…ON when a condition is needed to specify a relationship other than equivalency

Using JOIN keyword frees the WHERE clause for exclusive use in restricting rows

Use NATURAL JOIN when tables have one column in common

Use JOIN…USING when tables have more than one column in common

Use JOIN…ON when a condition is needed to specify a relationship other than equivalency

Using JOIN keyword frees the WHERE clause for exclusive use in restricting rows

Page 7: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Equality Join: WHERE Clause ExampleEquality Join: WHERE Clause Example

Page 8: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Equality Join: NATURAL JOINEquality Join: NATURAL JOIN

Syntax: tablename NATURAL JOIN tablenameSyntax: tablename NATURAL JOIN tablename

Page 9: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Equality Join: JOIN…USINGEquality Join: JOIN…USING

Syntax: tablename JOIN tablename USING (columnname)Syntax: tablename JOIN tablename USING (columnname)

Page 10: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Equality Join: JOIN…ONEquality Join: JOIN…ON

Syntax: tablename JOIN tablename ON conditionSyntax: tablename JOIN tablename ON condition

Page 11: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Non-Equality JoinsNon-Equality Joins

In WHERE clause, use any comparison operator other than equal sign

In FROM clause, use JOIN…ON keywords with non-equivalent condition

In WHERE clause, use any comparison operator other than equal sign

In FROM clause, use JOIN…ON keywords with non-equivalent condition

Page 12: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Non-Equality Join: WHERE Clause ExampleNon-Equality Join: WHERE Clause Example

Page 13: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Non-Equality Join: JOIN…ON ExampleNon-Equality Join: JOIN…ON Example

Page 14: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Self-JoinsSelf-Joins

Used to link a table to itself Requires use of column qualifier

Used to link a table to itself Requires use of column qualifier

Page 15: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Self-Join: WHERE Clause ExampleSelf-Join: WHERE Clause Example

Page 16: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Self-Join: JOIN…ON ExampleSelf-Join: JOIN…ON Example

Page 17: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Outer JoinsOuter Joins

Use to include rows that do not have a match in the other table

In WHERE clause, include outer join operator (+) next to table with missing rows to add NULL rows

In FROM clause, use FULL, LEFT, or RIGHT with OUTER JOIN keywords

Use to include rows that do not have a match in the other table

In WHERE clause, include outer join operator (+) next to table with missing rows to add NULL rows

In FROM clause, use FULL, LEFT, or RIGHT with OUTER JOIN keywords

Page 18: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Outer Join: WHERE Clause ExampleOuter Join: WHERE Clause Example

Page 19: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Outer Join: OUTER JOIN Keyword ExampleOuter Join: OUTER JOIN Keyword Example

Page 20: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Set OperatorsSet Operators

Used to combine the results of two or more SELECT statementsUsed to combine the results of two or more SELECT statements

Page 21: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Set Operator ExampleSet Operator Example

Page 22: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Joining Three or More TablesJoining Three or More Tables

Same procedure as joining two tables Will always results in one less join than

the number of tables being joined

Same procedure as joining two tables Will always results in one less join than

the number of tables being joined

Page 23: Chapter 4 Joining Multiple Tables. Chapter Objectives Create a Cartesian join Create an equality join using the WHERE clause Create an equality join using.

Joining Three or More Tables: ExampleJoining Three or More Tables: Example