Multiple Table Queries 1 Objectives Retrieve data from more than one table by joining tables Using IN and EXISTS to query multiple tables Nested Subqueries Using aliases Joining a table to itself UNION, INTERSECT and MINUS ALL and ANY operators 2 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 3 Create a Join Indicate in the SELECT clause all columns to display List in the FROM clause all tables involved in the query Give condition(s) in the WHERE clause to restrict the data to be retrieved to only those rows that have common values in matching columns 4 Qualify Columns SLSREP_NUMBER is in both the SALES-REP table and the CUSTOMER table, which causes ambiguity among column names Qualify columns by separating the table name and the column name with a period. 5 Examples List the customer number, last name, and first name for every customer, together with the sales rep number, last name, and fist name for the sales rep who represents each customer List the customer number, last name, and first name of every customer whose credit limit is $1,000 together with the sales rep number, last name, and first name of the sales rep who represents each customer 6 JOIN, IN, and EXISTS Include the where condition to ensure that matching columns contain equal values Similar results are obtained using IN or EXISTS operator within a subquery 7 Nested Subquery When a subquery is within another subquery, it is called a nested subquery Find the order number and order date for every order that includes a part located in warehouse number 3 Rewrite above query using 2 table joins 8 Nested Subquery Evaluation Innermost subquery is evaluated first, producing a temporary table of part numbers located in warehouse 3 Intermediate subquery is evaluated, producing a second temporary table with a list of order numbers Outer query is evaluated last, producing the desired list of order numbers and order dates using only those orders whose numbers are in the temporary table produced in step 2 9 Using an Alias Tables listed in the FROM clause can be given an alternative name List the sales rep number, last name, and first name for every sales rep together with the customer number, last name, and first name for each customer the sales rep represents 10 Complex Joins Joining a table to itself Find every pair of customers who have the same first and last names For each pair of tables joined, include a condition indicating how the columns are related For every part on order, list the part number, number ordered, order number, order date, customer number, last name, and first name of the customer who placed the order, and the last name and first name of the sales rep who represents each customer 11 Set Operations Union: a table containing every row that is in either the first table or the second table, or both (must be union-compatible: same number of columns and corresponding columns have identical data types and lengths) Intersection (intersect): a table containing every row that is in both tables Difference (minus): set of every row that is in the first table but not in the second table 12 Set Operations 13 Set Operations 14 UNION/INTERSECT/MINUS List the customer number, last name, and first name for every customer who is either represented by sales rep number 12 or who currently has orders on file, or both List the customer number, last name, and first name for every customer who is represented by sales rep number 12 and who currently has orders on file List the customer number, last name, and first name for every customer who is either represented by sales rep number 12 or who does not have orders currently on file 15 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 16 ALL and ANY Find the customer number, last name, first name, current balance, and sales rep number for every customer whose balance is greater than the individual balances of every customer of sales rep 12 Find the customer number, last name, first name, current balance, and sales rep number of every customer whose balance is larger than the balances of at least one customer of sales rep number 12 17 Summary To join tables together, indicate in the SELECT clause all columns to display, list in the FROM clause all tables to join, and then include in the WHERE clause any conditions requiring values in matching columns to be equal When referring to matching columns in different tables, you must qualify the column names to avoid confusion You qualify column names using the following format: table name.column name Use the IN operator or the EXISTS command with an appropriate subquery as an alternate way of performing a join 18 Summary A subquery can contain another subquery. The innermost subquery is executed first The name of a table in a FROM clause can be followed by an alias, which is an alternate name for the table The alias can be used in place of the table name throughout the SQL command By using two different aliases for the same table in a single SQL command, you can join a table to itself 19 Summary The UNION command creates a union of two tables; that is, the collection of rows that are in either or both tables The INTERSECT command creates the intersection of two tables; that is, the collection of rows that are in both tables The MINUS command creates the difference of two tables; that is, the collection of rows that are in the first table but not in the second table To perform any of these operation, the tables must be unioncompatible 20 Summary Two tables are union-compatible if they have the same number of columns, and if their corresponding columns have identical data types and lengths If a subquery is preceded by the ALL command, the condition is true only if it is satisfied by all values produced by the subquery If a subquery is preceded by the ANY command, the condition is true if it is satisfied by any value (one or more) produced by the subquery 21