Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
-
Upload
franklin-sloper -
Category
Documents
-
view
228 -
download
2
Transcript of Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
![Page 1: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/1.jpg)
Chapter 12 Joining Tables
Part C. SQL
Copyright 2005 Radian Publishing Co.
![Page 2: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/2.jpg)
Copyright 2005 Radian Publishing Co.2/27
Contents
Chapter 12 Joining Tables
12.1 Business Applications
12.2 Set Operations
• 12.2 A. Union
• 12.2 B. Intersect
• 12.2 C. Minus
12.3 Other Types of JOIN
• 12.3 A. Cross-join
• 12.3 B. Outer-join
• 12.3 C. Simulating MINUS by Outer-Join
• 12.3 D. Joining to Oneself
• 12.3 E. Bus Routes Using Joining to Oneself
![Page 3: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/3.jpg)
Copyright 2005 Radian Publishing Co.3/27
Chapter 12 Joining Tables
Table joining gathers data from more than one table when retrieving
data from a database.
Join condition may be placed in JOIN clause. It can also be an
inequality.
![Page 4: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/4.jpg)
Copyright 2005 Radian Publishing Co.4/27
12.1 Business Applications
The basic syntax of inner join is:
SELECT …FROM TableName1, TableName2 WHERE JoinCondition [AND FilterCondition]
The filter condition must be written after the join condition.
An alternative syntax for inner-join is:
SELECT ...FROM TableName1INNER JOIN TableName2 ON JoinConditionWHERE FilterCondition
![Page 5: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/5.jpg)
Copyright 2005 Radian Publishing Co.5/27
12.2 Set Operations (1/3)
A join combines tables by columns.
Fig.12.7 Joining tables by columns
![Page 6: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/6.jpg)
Copyright 2005 Radian Publishing Co.6/27
12.2 Set Operations (2/3)
Combining tables by rows requires the original tables to have
compatible structures.
Fig.12.8 Joining tables by rows
![Page 7: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/7.jpg)
Copyright 2005 Radian Publishing Co.7/27
12.2 Set Operations (3/3)
We shall discuss three mathematical set operations:
• UNION• INTERSECT• MINUS
Fig.12.9 The three set operations
![Page 8: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/8.jpg)
Copyright 2005 Radian Publishing Co.8/27
12.2 A. Union (1/2)
UNION simply groups the records of two tables together. Duplicated
rows are removed. If duplicated rows are necessary, use UNION ALL.
Syntax for UNION:
SELECT … FROM TableA
UNION
SELECT … FROM TableB
![Page 9: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/9.jpg)
Copyright 2005 Radian Publishing Co.9/27
12.2 A. Union (2/2)
![Page 10: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/10.jpg)
Copyright 2005 Radian Publishing Co.10/27
12.2 B. Intersect (1/2)
INTERSECT selects records which are common to both tables.
Syntax for INTERSECT:
SELECT … FROM TableAINTERSECTSELECT … FROM TableB
![Page 11: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/11.jpg)
Copyright 2005 Radian Publishing Co.11/27
12.2 B. Intersect (2/2)
![Page 12: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/12.jpg)
Copyright 2005 Radian Publishing Co.12/27
12.2 C. Minus
MINUS selects records which are found in one set but not on the other.
Syntax for MINUS:
SELECT … FROM TableAMINUSSELECT … FROM TableB
![Page 13: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/13.jpg)
Copyright 2005 Radian Publishing Co.13/27
12.3 Other Types of JOIN (1/2)
Fig.12.19 All possibletable combination
![Page 14: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/14.jpg)
Copyright 2005 Radian Publishing Co.14/27
12.3 Other Types of JOIN (2/2)
A cross-join is a join without any join condition.
Joins with a join condition are divided into inner-joins and outer-joins.
An inner-join consists of records matching the join conditions. Inner-
joins are further classified into equi-joins and non-equi-joins.
An equi-join uses the equal operator (=) in the join condition.
A non-equi-join uses relational operators, >, <, >=, <=, <>.
A natural join is an equi-join without duplicating columns.
![Page 15: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/15.jpg)
Copyright 2005 Radian Publishing Co.15/27
12.3 A. Cross-join (1/3)
A cross-join is a join without any join condition. It will map every record
in one table with every record in the other table.
Fig.12.20 Cross-joining Table A and Table B
![Page 16: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/16.jpg)
Copyright 2005 Radian Publishing Co.16/27
12.3 A. Cross-join (2/3)
![Page 17: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/17.jpg)
Copyright 2005 Radian Publishing Co.17/27
12.3 A. Cross-join (3/3)
Qualifiers are used to remove ambiguity, where two tables have the
same column name.
Both join condition and filter condition can exist in the WHERE clause.
![Page 18: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/18.jpg)
Copyright 2005 Radian Publishing Co.18/27
12.3 B. Outer-join (1/6)
An outer-join selects both matched and non-matched records.
It is further classified into left-outer-joins, right-outer-joins and full-outer-
joins.
![Page 19: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/19.jpg)
Copyright 2005 Radian Publishing Co.19/27
12.3 B. Outer-join (2/6)
A left-outer-join selects all the rows from the first table including those
match and those do not match the rows in the second table.
The syntax for a left-outer-join is
SELECT ...
FROM TableName1
LEFT OUTER JOIN TableName2
ON JoinCondition
![Page 20: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/20.jpg)
Copyright 2005 Radian Publishing Co.20/27
12.3 B. Outer-join (3/6)
![Page 21: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/21.jpg)
Copyright 2005 Radian Publishing Co.21/27
12.3 B. Outer-join (4/6)
A right-outer-join selects all the rows from the second table including
those match and those do not match the rows in the first table.
Syntax for right-outer-join:
SELECT ...
FROM TableName1
RIGHT OUTER JOIN TableName2
ON JoinCondition
![Page 22: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/22.jpg)
Copyright 2005 Radian Publishing Co.22/27
12.3 B. Outer-join (5/6)
![Page 23: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/23.jpg)
Copyright 2005 Radian Publishing Co.23/27
12.3 B. Outer-join (6/6)
A full-outer-join reveals unmatched records on both sides as well as the matched records.
Syntax for full-outer-join:
SELECT ...
FROM TableName1
FULL OUTER JOIN TableName2
ON JoinCondition
![Page 24: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/24.jpg)
Copyright 2005 Radian Publishing Co.24/27
12.3 C. Simulating MINUS by Outer-Join
Refer to textbook P.286
![Page 25: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/25.jpg)
Copyright 2005 Radian Publishing Co.25/27
12.3 D. Joining to Oneself (1/2)
Joining a table to itself means writing the same table name twice in
FROM clause of the SQL statement.
![Page 26: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/26.jpg)
Copyright 2005 Radian Publishing Co.26/27
12.3 D. Joining to Oneself (2/2)
![Page 27: Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.](https://reader036.fdocuments.net/reader036/viewer/2022062312/5519b2a455034660578b4669/html5/thumbnails/27.jpg)
Copyright 2005 Radian Publishing Co.27/27
12.3 E. Bus Routes Using Joining to Oneself
Refer to textbook P.289