Presentation on theme: "Multiple Table Queries"— Presentation transcript:
1Multiple Table Queries A Guide to SQL – Chapter 5
2Instructional Objectives Use joins to retrieve data from one than one tableUse IN and EXISTS operators to query multiple tablesUse a subquery within a subqueryALLANY
3Joining tablesUsed when dealing with multiple tables (rather than writing lengthy subqueries)Finds rows in two tables that have identical values in matching columnsExample: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.
7ExampleFor every part on order, list the order number, part number, part description, number of units ordered, quoted price, and unit priceSolution:SELECT Order_Num, Order_Line.Part_Num, Description, Num_Ordered, Quoted_Price, PriceFROM Order_Line, PartWHERE Order_Line.Part_Num = Part.Part_Num;
8ExampleFind the description of each part included in order number 21610Solution:SELECT DescriptionFROM Order_Line, PartWHERE Order_Line.Part_Num = Part.Part_NumAND Order_Num = ‘21610’;
12Alternate solution… subquery within a subquery Find the order number and order date for each order that includes a part located in warehouse 3Solution:SELECT Order_Num, Order_DateFROM OrdersWHERE Order_Num IN(SELECTOrder_NumFROM Order_LineWHERE Part_Num IN(SELECT Part_NumFROM PartWHERE Warehouse = ‘3’));
13Exists Can also be used to get data from multiple tables Checks for existence of rows that satisfy some criterionExample:Find the order number and order date for each order that contains part number DR93
15Your turnFor 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.
17Comprehensive 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
18Solution…SELECT Customer_Num, Orders.Order_Num, SUM(Num_Ordered * Quoted_Price) AS ORDER_TOTALFROM Orders, Order_LineWHERE Orders.Order_Num = Order_Line.Order_NumGROUP BY Orders.Order_Num, Customer_NumHAVING Sum (Num_Ordered * Quoted_Price) > 1000Order By Orders.Order_Num;
19Joining 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
21Set Operations Union Intersect (intersection) Minus (difference) Of two tables is a temp table containing every row that is in either the first table, the second table or bothIntersect (intersection)Of two tables is a temp table containing all rows that are in both tablesTables must be union compatible – same number of columns with identical data types and lengthsMinus (difference)Of two tables is (a temp table) the set of all rows that are in the first table but not in the second tableAccess does not support Intersect or Minus
22Union exampleList the number and name of each customer that either is represented by sales rep 65 or that currently has orders on file, or bothSolution:
23Intersect exampleList the number and name of each customer that is represented by sales rep 65 and that currently has orders on fileSolution:Since Access does not support Intersect, use subquery
24Minus ExampleList the number and name of each customer that is represented by sales rep 65 and that does not currently have orders on fileSolution:Since Access does not support Intersect, use subquery
25ALLFind 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:
26ANYFind 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:
27Special Operations Inner join Outer join Join that compares the tables in the FROM clause and lists only those rows that satisfy the conditionAll the examples we have looked at so far have used inner joinsOuter joinUsed to list all rows from one of the tables in a join, regardless of whether they match any rows in the other tableTypes of outer joinLeft outer joinRight outer joinFull outer join
28Outer joins Left outer join Right outer join Full outer join All rows from the table on the left will be included regardless of whether they match rows from the table on the rightRight outer joinAll rows from the table on the right will be included regardless of whether they match rows from the table on the leftFull outer joinAll rows from both tables will be included regardless of whether they match rows from the other table
29Outer join exampleDisplay 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.
31ProductCombination of all rows in the first table and all rows in the second tableExample:Form the product of the Customer and Orders table. Display the customer number, name, order number and order dateSolution: