31
Multiple Table Queries A Guide to SQL – Chapter 5

Multiple Table Queries A Guide to SQL – Chapter 5

Embed Size (px)

Citation preview

Page 1: Multiple Table Queries A Guide to SQL – Chapter 5

Multiple Table Queries

A Guide to SQL – Chapter 5

Page 2: Multiple Table Queries A Guide to SQL – Chapter 5

Instructional Objectives

Use joins to retrieve data from one than one table

Use IN and EXISTS operators to query multiple tables

Use a subquery within a subquery ALL ANY

Page 3: Multiple Table Queries A Guide to SQL – Chapter 5

Joining tables

Used when dealing with multiple tables (rather than writing lengthy subqueries)

Finds rows in two tables that have identical values in matching columns

Example: List the number and name of each customer

along with the number, last name, and first name of the sales rep who represents the customer. Sort the records by customer number.

Page 4: Multiple Table Queries A Guide to SQL – Chapter 5

Solution

SELECT CustomerNum, CustomerName, Rep.RepNum, LastName, FirstName

FROM Customer, Rep

WHERE Customer.RepNum = Rep.RepNum

ORDER BY CustomerNum;

Page 5: Multiple Table Queries A Guide to SQL – Chapter 5

Another example

Lists the number and name of each customer whose credit limit is $7,500 together with the number, last name, and first name of the sales rep who represents the customer

Page 6: Multiple Table Queries A Guide to SQL – Chapter 5

Solution

SELECT Customer_Num, Customer_Name, Rep.Rep_Num, Last_Name, First_Name

FROM Customer, Rep

WHERE Customer.Rep_Num = Rep.Rep_Num

AND Credit_Limit = 7500;

Page 7: Multiple Table Queries A Guide to SQL – Chapter 5

Example

For every part on order, list the order number, part number, part description, number of units ordered, quoted price, and unit price

Solution:SELECT Order_Num, Order_Line.Part_Num,

Description, Num_Ordered, Quoted_Price, Price

FROM Order_Line, Part

WHERE Order_Line.Part_Num = Part.Part_Num;

Page 8: Multiple Table Queries A Guide to SQL – Chapter 5

Example

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’;

Page 9: Multiple Table Queries A Guide to SQL – Chapter 5

Another way… IN Operator

SELECT Description

FROM Part

WHERE Part_Num IN

(SELECT Part_Num

FROM Order_Line

WHERE Order_Num = ‘21610’);

Page 10: Multiple Table Queries A Guide to SQL – Chapter 5

Your Turn

List all the number and dates of those orders that include a part located in warehouse 3

Page 11: Multiple Table Queries A Guide to SQL – Chapter 5

Solution

Page 12: Multiple Table Queries A Guide to SQL – Chapter 5

Alternate solution… subquery within a subquery Find the order number and order date for each order

that includes a part located in warehouse 3 Solution:

SELECT Order_Num, Order_DateFROM OrdersWHERE Order_Num IN (SELECTOrder_Num FROM Order_Line WHERE Part_Num IN (SELECT Part_Num FROM Part WHERE Warehouse = ‘3’));

Page 13: Multiple Table Queries A Guide to SQL – Chapter 5

Exists

Can also be used to get data from multiple tables

Checks for existence of rows that satisfy some criterion

Example: Find the order number and order date for each

order that contains part number DR93

Page 14: Multiple Table Queries A Guide to SQL – Chapter 5

Solution:

Page 15: Multiple Table Queries A Guide to SQL – Chapter 5

Your turn

For every order, list the order number, order date, customer number, and customer name. In addition, for each order line within the order, list the part number, description, number ordered, and quoted price. Sort the records by order number.

Page 16: Multiple Table Queries A Guide to SQL – Chapter 5

Solution

Page 17: Multiple Table Queries A Guide to SQL – Chapter 5

Comprehensive Example… Your Turn List the customer number, order number, and

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

Page 18: Multiple Table Queries A Guide to SQL – Chapter 5

Solution…

SELECT Customer_Num, Orders.Order_Num, SUM(Num_Ordered * Quoted_Price) AS ORDER_TOTAL

FROM Orders, Order_Line

WHERE Orders.Order_Num = Order_Line.Order_Num

GROUP BY Orders.Order_Num, Customer_Num

HAVING Sum (Num_Ordered * Quoted_Price) > 1000

Order By Orders.Order_Num;

Page 19: Multiple Table Queries A Guide to SQL – Chapter 5

Joining a table to itself (self-join) Have to use an alias (in FROM clause) to

treat a table as two tables. Example:

For each pair of customers located in the same city, display the customer number, customer name, and city

Page 20: Multiple Table Queries A Guide to SQL – Chapter 5

Solution

(See figures 5.12 & 5.13 on p. 147-148)

Page 21: Multiple Table Queries A Guide to SQL – Chapter 5

Set Operations

Union Of two tables is a temp table containing every row that is in

either the first table, the second table or both Intersect (intersection)

Of two tables is a temp table containing all rows that are in both tables

Tables must be union compatible – same number of columns with identical data types and lengths

Minus (difference) Of two tables is (a temp table) the set of all rows that are in

the first table but not in the second table Access does not support Intersect or Minus

Page 22: Multiple Table Queries A Guide to SQL – Chapter 5

Union example

List the number and name of each customer that either is represented by sales rep 65 or that currently has orders on file, or both

Solution:

Page 23: Multiple Table Queries A Guide to SQL – Chapter 5

Intersect example

List the number and name of each customer that is represented by sales rep 65 and that currently has orders on file

Solution:

Since Access does not support Intersect, use subquery

Page 24: Multiple Table Queries A Guide to SQL – Chapter 5

Minus Example

List the number and name of each customer that is represented by sales rep 65 and that does not currently have orders on file

Solution:

Since Access does not support Intersect, use subquery

Page 25: Multiple Table Queries A Guide to SQL – Chapter 5

ALL

Find the customer number, name, current balance, and rep number of each customer whose balance is greater than the individual balances of each customer of sales rep 65.

Solution:

Page 26: Multiple Table Queries A Guide to SQL – Chapter 5

ANY

Find the customer number, name, current balance, and rep number of each customer whose balance is greater than the balance of at least one customer of sales rep 65.

Solution:

Page 27: Multiple Table Queries A Guide to SQL – Chapter 5

Special Operations

Inner join Join that compares the tables in the FROM clause and lists

only those rows that satisfy the condition All the examples we have looked at so far have used inner

joins Outer join

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

Types of outer join Left outer join Right outer join Full outer join

Page 28: Multiple Table Queries A Guide to SQL – Chapter 5

Outer joins Left outer join

All rows from the table on the left will be included regardless of whether they match rows from the table on the right

Right outer join All rows from the table on the right will be included

regardless of whether they match rows from the table on the left

Full outer join All rows from both tables will be included regardless of

whether they match rows from the other table

Page 29: Multiple Table Queries A Guide to SQL – Chapter 5

Outer join example

Display the customer number, customer name, order number, and order date for all orders. Include all customers in the results. For customer that do not have orders, omit the order number and order date.

Page 30: Multiple Table Queries A Guide to SQL – Chapter 5

Outer join example solution

Page 31: Multiple Table Queries A Guide to SQL – Chapter 5

Product

Combination of all rows in the first table and all rows in the second table

Example: Form the product of the Customer and Orders

table. Display the customer number, name, order number and order date

Solution: