Download presentation
Presentation is loading. Please wait.
Published byFranklin Sloper Modified over 10 years ago
1
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
2
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-Join12.3 C. Simulating MINUS by Outer-Join 12.3 D. Joining to Oneself 12.3 E. Bus Routes Using Joining to Oneself12.3 E. Bus Routes Using Joining to Oneself
3
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.
4
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 TableName1 INNER JOIN TableName2 ON JoinCondition WHERE FilterCondition
5
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
6
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
7
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
8
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
9
Copyright 2005 Radian Publishing Co.9/27 12.2 A. Union (2/2)
10
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 TableA INTERSECT SELECT … FROM TableB
11
Copyright 2005 Radian Publishing Co.11/27 12.2 B. Intersect (2/2)
12
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 TableA MINUS SELECT … FROM TableB
13
Copyright 2005 Radian Publishing Co.13/27 12.3 Other Types of JOIN (1/2) Fig.12.19 All possible table combination
14
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.
15
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
16
Copyright 2005 Radian Publishing Co.16/27 12.3 A. Cross-join (2/3)
17
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.
18
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.
19
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
20
Copyright 2005 Radian Publishing Co.20/27 12.3 B. Outer-join (3/6)
21
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
22
Copyright 2005 Radian Publishing Co.22/27 12.3 B. Outer-join (5/6)
23
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
24
Copyright 2005 Radian Publishing Co.24/27 12.3 C. Simulating MINUS by Outer-Join Refer to textbook P.286
25
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.
26
Copyright 2005 Radian Publishing Co.26/27 12.3 D. Joining to Oneself (2/2)
27
Copyright 2005 Radian Publishing Co.27/27 12.3 E. Bus Routes Using Joining to Oneself Refer to textbook P.289
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.