30
1 A Guide to SQL, Sixth Edition Chapter 4 Multiple-Table Queries

A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

Embed Size (px)

Citation preview

Page 1: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

1

A Guide to SQL, Sixth Edition

Chapter 4Multiple-Table Queries

Page 2: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

2

Objectives

Use joins to retrieve data from more than one table

Use the IN and EXISTS operators to query multiple tables

Use a subquery within a subquery

Use an alias

Join a table to itself

Page 3: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

3

Objectives

Perform set operations (union, intersection, and difference)

Use the ALL and ANY operators in a query

Perform special operations (inner join, outer join, and product)

Page 4: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

4

Querying Multiple Tables

To retrieve data from two or more tables:Join the tablesFormulate a query using the same

commands as for single tables

Page 5: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

5

Joining Two Tables

To retrieve data from more than one table, join the tables together

Tables can be joined by finding rows in the two tables that have identical values in matching columns

Page 6: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

6

Joining Two Tables

To join (relate) two or more tables, the SQL command is constructed as follows: SELECT clause: list all columns you want to

display FROM clause: list all tables involved in the query WHERE clause: list the condition that restricts the

data to be retrieved to only those rows from the two tables that match

Page 7: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

7

Joining Two Tables

Qualifying column names is especially important when joining tables

Precede column name with the table name, followed by a period

Page 8: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

8

Comparing of JOIN, IN, and EXISTS

Join tables by including a condition in the WHERE clause to ensure that matching columns contain equal values Similar results can be obtained by using either the IN operator or the EXISTS operator with a subquery Either approach obtains the same results

Page 9: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

9

Comparing of JOIN, IN, and EXISTS

Problem:Find the description of each part included

in order number 21610

Solution:SELECT DESCRIPTIONFROM ORDER_LINE, PARTWHERE ORDER_LINE.PART_NUM = PART.PART_NUMAND ORDER_NUM = '21610';

Page 10: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

10

Using IN Instead of a Join to Query Two Tables

Page 11: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

11

Using EXISTS

Problem: Find the order number and order date for each

order that contains part number DR93

Page 12: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

12

Using a Subquery Within a Subquery

When a subquery is within another subquery, it is called a nested subquerySQL evaluates queries from the innermost to the outermost: Innermost subquery is evaluated first Next (intermediate) subquery is evaluated Outer query is evaluated last

Page 13: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

13

A Comprehensive Example

The example given in this section involves several of the features already discussed

Problem:List the customer number, order number,

order date, and order total for each order with a total that exceeds $1,000. Rename the order total as ORDER_TOTAL

Page 14: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

14

A Comprehensive Example

Page 15: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

15

Using an Alias

Tables listed in the FROM clause can be given an alternative nameAn alias is created by:Typing the name of the tablePressing the space barTyping the name of the alias

One reason for using an alias is simplicity

Page 16: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

16

Joining a Table to Itself

A second reason for using an alias is that it is needed when joining a table to itself, called a self-join

Problem:For each pair of customers located in the

same city, display the customer number, customer name, and city

Page 17: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

17

Joining a Table to Itself

Page 18: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

18

Joining Several Tables

It is possible to join several tables

For each pair of tables, you must include a condition indicating how the columns are related

The procedure for joining more than two tables is essentially the same as the one for joining two tables

Page 19: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

19

Joining Several TablesIn such a query:The condition in the WHERE clause will be

a compound conditionAll the desired columns would be listed in

the SELECT clauseAny columns that appear in more than one

table would be qualified In the FROM clause, the tables that are

involved in the query would be listed

Page 20: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

20

Set Operations

The union of two tables is a table containing every row that is in either the first table, the second table, or both tables

The intersection (intersect) of two tables is a table containing all rows that are in both tables

The difference (minus) of two tables is the set of all rows that are in the first table but that are not in the second table

Page 21: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

21

Set Operations

The two tables in the union must be “union compatible”

Two tables are union compatible if:They have the same number of columnsTheir corresponding columns have

identical data types and lengths

Page 22: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

22

ALL and ANY

ALL and ANY operators are used with subqueries to produce a single column of numbersALL: condition is true only if it satisfies all

values produced by the subqueryANY: condition is true if it satisfies any

value (one or more) produced by the subquery

Page 23: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

23

Special Operations

The special operations within SQL are:Self-join Inner joinOuter joinProduct

Page 24: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

24

Inner Join

A join that compares the tables in the FROM clause and lists only those rows that satisfy the condition in the WHERE clause is called an inner join

The joins mentioned in this text so far have been inner joins

Page 25: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

25

Outer Join

Sometimes, there is a need to list all the rows from one of the tables in a join, regardless of whether they match any rows in the other table

This type of join is called an outer join

Page 26: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

26

Outer Join

There are three types of outer joins:Left outer join - all rows from the table on

the left will be included Right outer join - all rows from the table on

the right will be included Full outer join - all rows from both tables

will be included

Page 27: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

27

Product

The product is formally called the Cartesian Product

The product of two tables is the combination of all rows in the first table and all rows in the second table

Page 28: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

28

SummaryTo retrieve data from more than one table, you must join the tables together

The IN or EXISTS operators can be used as an alternate way of performing a join

A subquery can contain another subquery

The UNION command creates a union of two tables (the collection of rows that are in either or both tables)

The INTERSECT command creates the intersection of two tables (the collection of rows that are in both tables)

Page 29: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

29

Summary

The MINUS command creates the difference of two tables

In an inner join, only matching rows from both tables are included

In a left outer join, all rows from the table on the left will be included

In a right outer join, all rows from the table on the right will be included

The product of two tables is the combination of all rows in the first table and all rows in the second table

Page 30: A Guide to SQL, Sixth Edition 1 Chapter 4 Multiple-Table Queries

30

SQL Project Four Completed

Good Luck

H. Zamanzadeh