Download presentation
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
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)
4
Querying Multiple Tables
To retrieve data from two or more tables: Join the tables Formulate a query using the same commands as for single tables
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
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
7
Joining Two Tables Qualifying column names is especially important when joining tables Precede column name with the table name, followed by a period
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
9
Comparing of JOIN, IN, and EXISTS
Problem: Find the description of each part included in order number 21610 Solution: SELECT DESCRIPTION FROM ORDER_LINE, PART WHERE ORDER_LINE.PART_NUM = PART.PART_NUM AND ORDER_NUM = '21610';
10
Using IN Instead of a Join to Query Two Tables
11
Using EXISTS Problem: Find the order number and order date for each order that contains part number DR93
12
Using a Subquery Within a Subquery
When a subquery is within another subquery, it is called a nested subquery SQL evaluates queries from the innermost to the outermost: Innermost subquery is evaluated first Next (intermediate) subquery is evaluated Outer query is evaluated last
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
14
A Comprehensive Example
15
Using an Alias Tables listed in the FROM clause can be given an alternative name An alias is created by: Typing the name of the table Pressing the space bar Typing the name of the alias One reason for using an alias is simplicity
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
17
Joining a Table to Itself
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
19
Joining Several Tables
In such a query: The condition in the WHERE clause will be a compound condition All the desired columns would be listed in the SELECT clause Any 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
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
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 columns Their corresponding columns have identical data types and lengths
22
ALL and ANY ALL and ANY operators are used with subqueries to produce a single column of numbers ALL: condition is true only if it satisfies all values produced by the subquery ANY: condition is true if it satisfies any value (one or more) produced by the subquery
23
Special Operations The special operations within SQL are: Self-join
Inner join Outer join Product
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
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
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
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
28
Summary To 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)
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
30
SQL Project Four Completed
Good Luck H. Zamanzadeh
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.