Presentation is loading. Please wait.

Presentation is loading. Please wait.

8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.

Similar presentations


Presentation on theme: "8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel."— Presentation transcript:

1 8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

2 8 2 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel In this chapter, you will learn: About the relational set operators UNION, UNION ALL, INTERSECT, and MINUS How to use the advanced SQL JOIN operator syntax About the different types of subqueries and correlated queries

3 8 3 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel

4 8 4 Relational Set Operators UNION INTERSECT MINUS Work properly if relations are union- compatible –Names of relation attributes must be the same and their data types must be identical

5 8 5 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel

6 8 6 UNION Example -1: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; –This example generates a combined listing of customers—one that excludes duplicate records Example -2 –SELECT column-list FROM T1 UNION SELECT column-list FROM T2 UNION SELECT column-list FROM T3;

7 8 7 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION (continued)

8 8 8 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION ALL UNION ALL query can be used to produce a relation that retains the duplicate rows UNION ALL statement can be used to unite more than just two queries Example query: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION ALL SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2;

9 8 9 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION ALL (continued)

10 8 10 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel INTERSECT The NTERSECT statement can be used to combine rows from two queries, returning only the rows that appear in both sets

11 8 11 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel MINUS The MINUS statement in SQL combines rows from two queries and returns only the rows that appear in the first set but not in the second

12 8 12 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Syntax Alternatives For example, the following query returns the customer codes for all customers who are located in area code 615 and who have made purchases. (If a customer has made a purchase, there must be an invoice record for that customer.) SELECT CUS_CODE FROM CUSTOMER WHERE CUS_AREACODE = '615' INTERSECT SELECT DISTINCT CUS_CODE FROM INVOICE;

13 8 13 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Syntax Alternatives (continued) For example, the following query returns the customer codes for all customers located in area code 615 minus the ones who have made purchases, leaving the customers in area code 615 who have not made purchases. SELECT CUS_CODE FROM CUSTOMER WHERE CUS_AREACODE = '615' MINUS SELECT DISTINCT CUS_CODE FROM INVOICE;

14 8 14 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Tue 18-6 SQL Join Operators

15 8 15 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Cross Join Syntax: –SELECT column-list FROM table1 CROSS JOIN table2 –Returns the Cartesian product of table1 and table2(old style). EXAMPLE : SELECT* FROM GameScores CROSS JOIN Departments PlayerNameDepartmentIdScores Jason13000 Irene11500 Jane21000 David22500 Paul32000 James32000 DepartmentI d DepartmentNam e 1IT 2Marketing 3HR PlayerNam e DepartmentI d Score s DepartmentI d DepartmentNam e Jason130001IT Irene115001IT Jane210001IT David225001IT Paul320001IT James320001IT Jason130002Marketing Irene115002Marketing Jane210002Marketing David225002Marketing Paul320002Marketing James330002Marketing Jason130003HR Irene115003HR Jane210003HR David225003HR Paul320003HR James330003HR

16 8 16 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Natural Join

17 8 17 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel JOIN USING Clause

18 8 18 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel JOIN ON Clause

19 8 19 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins Returns not only matching rows, but also rows with unmatched attribute values for one table or both tables to be joined Three types –Left –Right –Full

20 8 20 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

21 8 21 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

22 8 22 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

23 8 23 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Subqueries and Correlated Queries

24 8 24 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Correlated Subqueries (continued) Example (1): you want to know all customers who have placed an order lately Example (2): you want to know the vendor code and name of vendors for products having a quantity on hand that is less than double the minimum quantity


Download ppt "8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel."

Similar presentations


Ads by Google