Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.

Similar presentations


Presentation on theme: "Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki."— Presentation transcript:

1 Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

2 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-2 OBJECTIVES  Define terms  Write single and multiple table SQL queries  Define and use three types of joins  Write noncorrelated and correlated subqueries  Understand and use SQL in procedural languages (e.g. PHP, PL/SQL)  Understand triggers and stored procedures

3 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-3 PROCESSING MULTIPLE TABLES  Join – a relational operation that causes two or more tables with a common domain to be combined into a single table or view  Equi-join – a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table  Natural join – an equi-join in which one of the duplicate columns is eliminated in the result table The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships

4 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-4 4 PROCESSING MULTIPLE TABLES– JOINS  Outer join – a join in which  rows that do not have matching values in common columns are nonetheless  included in the result table  (as opposed to inner join, in which rows must have matching values in order to appear in the result table)  Union join – includes all columns from each table in the join, and an instance for each row of each table  Illustration using “abstract examples”

5 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-5 Figure 7-2 Visualization of different join types with results returned in shaded area Right Outer Join

6 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-6 THE FOLLOWING SLIDES INVOLVE QUERIES OPERATING ON TABLES FROM THIS ENTERPRISE DATA MODEL (from Chapter 1, Figure 1-3)

7 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-7 Customer_T and Order_T are 1:M These tables are used in queries that follow Figure 7-1 Pine Valley Furniture Company Customer_T and Order_T tables with pointers from customers to their orders

8 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-8 SPECIFYING JOINS IN SQL  Join conditions may be specified in  WHERE clause:  FROM clause:  In either case, each contains a column that shares a common domain with the other (PK-FK)  There should be one PK-FK equality specification for each pair of tables being joined Has unintended outcome if not done carefully More straightforward

9 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-9 SPECIFYING JOINS IN SQL (CONT)  Join conditions may be specified in  WHERE clause:  WHERE PK1=FK2 and PK2=FK3 and PK3=FK4  Simply state the N-1 join conditions for the N- 1 pairs of PK-FK for the N tables involved  FROM clause:  FROM Table1 JOIN Table2 ON PK1=FK2  More complicated; often needs to use parentheses

10 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-10 EQUI-JOIN EXAMPLE  For each customer who placed an order, what is the customer’s name and order number? Customer ID appears twice in the result Fields that appear in only one table does not need table identification

11 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-11 EQUI-JOIN EXAMPLE – ALTERNATIVE SYNTAX INNER JOIN clause is an alternative to WHERE clause, and is used to match primary and foreign keys. An INNER join will only return rows from each table that have matching rows in the other. This query produces same results as previous equi-join example.

12 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-12 EQUI-JOIN (THREE METHODS)  SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;  SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T FROM CUSTOMER_T INNER JOIN ORDER_T ON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;  SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T FROM CUSTOMER_T INNER JOIN ORDER_T USING CUSTOMER_ID; Recommended for clarity

13 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-13 NATURAL JOIN EXAMPLE  For each customer who placed an order, what is the customer’s name and order number? Join involves multiple tables in FROM clause ON clause performs the equality check for common columns of the two tables Note: From Fig. 7-1, you see that only 10 Customers have links with orders.  Only 10 rows will be returned from this INNER join

14 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-14 OUTER JOIN EXAMPLE  List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order. LEFT OUTER JOIN clause causes customer data to appear even if there is no corresponding order data Unlike INNER join, this will include customer rows with no matching order rows Why Customer data? Why not Order data? (--Fig 7-2) All records from table A, & matching records from table B

15 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-15 15 Results Unlike INNER join, this will include customer rows with no matching order rows EVERY row in CUST table [even w/o order], PLUS Those matching rows in ORDER

16 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-16 FIGURE 7-2 (PORTION) - LEFT OUTER JOIN: WATCH FOR THE SEQUENCE OF TABLE CUSTOMER_T is the outer table – ”all records returned from it” even there is no matching: those who did not place an order … Had we reversed the order the tables were listed, … Customer_T Order_T

17 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-17 FIGURE “7-2” (EXTENDED) - RIGHT OUTER JOIN: WATCH FOR THE SEQUENCE OF TABLE Customer_T Order_T RIGHT OUTER JOIN Also see P. 295, first half Logically, A LEFT join B = B RIGHT join A: All rows in A, plus matching rows in B

18 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-18 MULTIPLE TABLE JOIN EXAMPLE  Assemble all information necessary to create an invoice for order number 1006 Four tables involved in this join Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys.

19 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-19 MULTIPLE TABLE JOIN EXAMPLE N tables, N-1 join conditions Join condi- tions from Slide #18 “Middle” table must be joined, even no filed listed

20 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-20 MULTIPLE TABLE JOIN - *NOTE*  “Bridge” table(s) must be joined, even there are no fields from these tables are selected to display.  SELECT A.f1, B.f2, B.f3, D.f1  FROM A, B, C, D  WHERE… ABCD “Bridge”: No field to display, but needed for joining

21 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-21 Figure 7-4 Results from a four-table join (edited for readability) From CUSTOMER_T table From ORDER_T table From PRODUCT_T table From ORDERLINE_T table Derived from ORDER_T and PRODUCT_T table: OrderQuantity * ProductStandartdPrice

22 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-22 SELF-JOIN EXAMPLE Self-joins are usually used on tables with unary relationships. Here the Employee table is viewed as E and M, … The same table is used on both sides of the join (remember unary? – and think about ERDs); distinguished using table aliases Note which one is which one!

23 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-23 Figure 7-5 Example of a self-join From Chapter 2 Unary 1:N

24 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-24 PROCESSING MULTIPLE TABLES USING SUBQUERIES  Subquery–placing an inner query (SELECT statement) inside an outer query  Options: 1. In a condition of the WHERE clause 2. As a “table” of the FROM clause 3. Within the HAVING clause 4. In the SELECT-clause: as a column to be displayed 5. (all the above because query results …)  Subqueries can be:  Noncorrelated–executed once for the entire outer query  Correlated–executed once for each row returned by the outer query (in Chap 7-Part 2) 0420 update

25 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-25 SUBQUERY EXAMPLE  Show all customers who have placed an order Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery

26 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-26  What are the name and address of the customer who placed order #1008?  SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS FROM CUSTOMER_T WHERE CUSTOMER_T.CUSTOMER_ID = (SELECT CUSTOMER_ID FROM ORDER_T WHERE ORDER_ID = 1008);  Note : the value for ORDER_ID does NOT appear in the query result; it is used as the selection criterion in the inner query  Data from a subquery cannot be included in the final results SUBQUERY EXAMPLE

27 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-27 JOIN VS. SUBQUERY  Some queries could be accomplished by either a join or a subquery Join version Subquery version

28 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-28 Figure 7-6 Graphical depiction of two ways to answer a query with different types of joins

29 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-29 Figure 7-6 Graphical depiction of two ways to answer a query with different types of joins

30 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-30  Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); SUBQUERY – “IN” Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery NOT/ANY/ALL may be used in front of IN; logical operators =, can be used

31 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-31 7-31 SUBQUERY – “ NOT IN”

32 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-32 ABOUT INNER QUERY AND OUTER QUERY  In the non-correlated subqueries, the inner query  only provides its output(s) as the Input(s) for the outer query;  The field names/alias are unrelated with those in the outer query;  The tables used can be (and often are) totally different from those in the outer query;  In simple words: they, are, in, two, different, worlds!

33 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-33 CORRELATED VS. NONCORRELATED SUBQUERIES  Noncorrelated subqueries:  Do not depend on data from the outer query  Execute once for the entire outer query  Correlated subqueries:  Make use of data from the outer query  Execute once for each row of the outer query  Can use the EXISTS operator

34 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-34 0420 UPDATES:  Subquery can be used in  SELECT (as a new column to display)  FROM (like a table to retrieve data from)  WHERE (condition provided by subquery)  HAVING (similar to that in WHERE)  Specifically: IN (subquery) – here the subquery does not output only one value but several values; so IN is used: “one of those values”

35 Chapter 7 Copyright © 2016 Pearson Education, Inc. 7-35 0420 UPDATES: EXAMPLE  SELECT (SELECT AVG(Sales) FROM Restaurants)  FROM (SELECT CustID FROM Cust WHERE St=“CA”)  WHERE Sale >= (SELECT AVG(Sales) FROM Restaurants)  HAVING AVG(Sales)>= (SELECT AVG(Sales) FROM Restaurants)  Specifically: IN (SELECT CustEmpID WHERE DOB<=1/1/1980)


Download ppt "Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki."

Similar presentations


Ads by Google