Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.

Similar presentations


Presentation on theme: "Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael."— Presentation transcript:

1 Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael V. Mannino. All rights reserved. Chapter 9 Advanced Query Formulation with SQL

2 Slide 2Chapter 9: Advanced Query Formulation with SQL Outline  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Nested queries in the FROM clause  Division problems  Null value effects

3 Slide 3Chapter 9: Advanced Query Formulation with SQL Outer Join Overview  Join excludes non matching rows  Preserving non matching rows is important in some business situations  Outer join variations  Full outer join  One-sided outer join

4 Slide 4Chapter 9: Advanced Query Formulation with SQL Right Outer Join Outer Join Operators Left Outer Join Join Matched rows using the join condition Unmatched rows of the left table Unmatched rows of the right table Full outer join

5 Slide 5Chapter 9: Advanced Query Formulation with SQL Full Outer Join Example

6 Slide 6Chapter 9: Advanced Query Formulation with SQL University Database

7 Slide 7Chapter 9: Advanced Query Formulation with SQL LEFT JOIN and RIGHT JOIN Keywords Example 1 (Access) SELECT OfferNo, CourseNo, Offering.FacNo, Faculty.FacNo, FacFirstName, FacLastName FROM Offering LEFT JOIN Faculty ON Offering.FacNo = Faculty.FacNo WHERE CourseNo LIKE 'IS*' Example 2 (Oracle) SELECT OfferNo, CourseNo, Offering.FacNo, Faculty.FacNo, FacFirstName, FacLastName FROM Faculty RIGHT JOIN Offering ON Offering.FacNo = Faculty.FacNo WHERE CourseNo LIKE 'IS%'

8 Slide 8Chapter 9: Advanced Query Formulation with SQL Full Outer Join Example I Example 3 (SQL:2011 and Oracle 9i and beyond) SELECT FacNo, FacFirstName, FacLastName, FacSalary, StdNo, StdFirstName, StdLastName, StdGPA FROM Faculty FULL JOIN Student ON Student.StdNo = Faculty.FacNo

9 Slide 9Chapter 9: Advanced Query Formulation with SQL Full Outer Join Example II Example 4 (Access) SELECT FacNo, FacFirstName, FacLastName, FacSalary, StdNo, StdFirstName, StdLastName, StdGPA FROM Faculty RIGHT JOIN Student ON Student.StdNo = Faculty.FacNo UNION SELECT FacNo, FacFirstName, FacLastName, FacSalary, StdNo, StdFirstName, StdLastName, StdGPA FROM Faculty LEFT JOIN Student ON Student.StdNo = Faculty.FacNo

10 Slide 10Chapter 9: Advanced Query Formulation with SQL Mixing Inner and Outer Joins I Example 5 (Access) SELECT OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacNo, FacLastName FROM ( Faculty RIGHT JOIN Offering ON Offering.FacNo = Faculty.FacNo ) INNER JOIN Course ON Course.CourseNo = Offering.CourseNo WHERE Course.CourseNo LIKE 'IS*' AND OffYear = 2013

11 Slide 11Chapter 9: Advanced Query Formulation with SQL Ambiguous Queries  Certain orderings of inner and outer joins −Outer join not associative (order dependent) −Do not execute in Access −May return different results in Oracle  Ambiguity rule  Non preserved table (only matching rows)  No other operations (joins or outer joins) for non preserved tables

12 Slide 12Chapter 9: Advanced Query Formulation with SQL Ambiguous Query Example I Example 5a (Oracle) SELECT Offering.OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacNo, FacFirstName, FacLastName FROM ( Faculty LEFT JOIN Offering ON Offering.FacNo = Faculty.FacNo ) INNER JOIN Course ON Course.CourseNo = Offering.CourseNo Ambiguity: Non preserved table (Offering) joined with Course after the outer join

13 Slide 13Chapter 9: Advanced Query Formulation with SQL Ambiguous Query Example II Example 5b (Oracle) SELECT Offering.OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacNo, FacFirstName, FacLastName FROM (Offering INNER JOIN Course ON Course.CourseNo = Offering.CourseNo ) RIGHT JOIN Faculty ON Offering.FacNo = Faculty.FacNo Ambiguity: Non preserved table (Offering) outer joined with Faculty after the join

14 Slide 14Chapter 9: Advanced Query Formulation with SQL Type I Nested Queries  Query inside a query  Use in WHERE and HAVING conditions  Similar to a nested procedure  Executes one time  No reference to outer query  Also known as non-correlated or independent nested query

15 Slide 15Chapter 9: Advanced Query Formulation with SQL Type I Nested Query Examples I

16 Slide 16Chapter 9: Advanced Query Formulation with SQL Type I Nested Query Examples II

17 Slide 17Chapter 9: Advanced Query Formulation with SQL DELETE Example Use Type I nested queries to test conditions on other tables Use for UPDATE statements also Example 8: Delete offerings taught by Leonard Vince. DELETE FROM Offering WHERE Offering.FacNo IN ( SELECT FacNo FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' )

18 Slide 18Chapter 9: Advanced Query Formulation with SQL Limited Formulations for Difference Problems  Type I nested query with NOT IN condition  One-sided outer join with IS NULL condition  Difference operation using MINUS (EXCEPT) operator

19 Slide 19Chapter 9: Advanced Query Formulation with SQL Type I Difference Formulation Example 9: Retrieve the faculty number, name (first and last), department, and salary of faculty who are not students. SELECT FacNo, FacFirstName, FacLastName, FacDept, FacSalary FROM Faculty WHERE FacNo NOT IN ( SELECT StdNo FROM Student )

20 Slide 20Chapter 9: Advanced Query Formulation with SQL Difference Formulation using a One-Sided Outer Join Example 10: Retrieve the faculty number, name, department, and salary of faculty who are not students. SELECT FacNo, FacFirstname, FacLastName, FacDept, FacSalary FROM Faculty LEFT JOIN Student ON Faculty.FacNo = Student.StdNo WHERE Student.StdNo IS NULL

21 Slide 21Chapter 9: Advanced Query Formulation with SQL Difference Formulation using a MINUS Operator Example 11 (Oracle): Retrieve faculty who are not students SELECT FacNo AS PerNo, FacFirstName AS FirstName, FacLastName AS LastName, FacCity AS City, FacState AS State FROM Faculty MINUS SELECT StdNo AS PerNo, StdFirstName AS FirstName, StdLastName AS LastName, StdCity AS City, StdState AS State FROM Student

22 Slide 22Chapter 9: Advanced Query Formulation with SQL Type II Nested Queries  Similar to nested loops  Executes one time for each row of outer query  Reference to outer query  Also known as correlated or variably nested query  Use for difference problems not joins

23 Slide 23Chapter 9: Advanced Query Formulation with SQL Type II Nested Query Example for a Difference Problem Example 12: Retrieve the faculty number, the name (first and last), the department, and the salary of faculty who are not students. SELECT FacNo, FacFirstName, FacLastName, FacDept FROM Faculty WHERE NOT EXISTS ( SELECT * FROM Student WHERE Student.StdNo = Faculty.FacNo )

24 Slide 24Chapter 9: Advanced Query Formulation with SQL Nested Queries in the FROM Clause  More recent introduction than nested queries in the WHERE and HAVING clauses  Consistency in language design  Wherever table appears, table expression can appear  Specialized uses  Nested aggregates  Multiple independent aggregate calculations

25 Slide 25Chapter 9: Advanced Query Formulation with SQL Nested FROM Query Example Example 13: List the course number, course description, the number of offerings, and the average enrollment across offerings. SELECT T.CourseNo, T.CrsDesc, COUNT(*) AS NumOfferings, Avg(T.EnrollCount) AS AvgEnroll FROM (SELECT Course.CourseNo, CrsDesc, Offering.OfferNo, COUNT(*) AS EnrollCount FROM Offering, Enrollment, Course WHERE Offering.OfferNo = Enrollment.OfferNo AND Course.CourseNo = Offering.CourseNo GROUP BY Course.CourseNo, CrsDesc, Offering.OfferNo) T GROUP BY T.CourseNo, T.CrsDesc

26 Slide 26Chapter 9: Advanced Query Formulation with SQL Divide Operator  Match on a subset of values  Suppliers who supply all parts  Faculty who teach every CS course  Specialized operator  Typically applied to associative tables representing M-N relationships

27 Slide 27Chapter 9: Advanced Query Formulation with SQL Division Example

28 Slide 28Chapter 9: Advanced Query Formulation with SQL COUNT Method for Division Problems Compare the number of rows associated with a group to the total number in the subset of interest Type I nested query in the HAVING clause Example 14: List the students who belong to all clubs. SELECT StdNo FROM StdClub GROUP BY StdNo HAVING COUNT(*) = ( SELECT COUNT(*) FROM Club )

29 Slide 29Chapter 9: Advanced Query Formulation with SQL Typical Division Problems Compare to an interesting subset rather than entire table Use similar conditions in outer and nested query Example 15: List the students who belong to all social clubs. SELECT Student1.StdNo, SName FROM StdClub, Club, Student1 WHERE StdClub.ClubNo = Club.ClubNo AND Student1.StdNo = StdClub.StdNo AND CPurpose = 'SOCIAL' GROUP BY Student1.StdNo, SName HAVING COUNT(*) = ( SELECT COUNT(*) FROM Club WHERE CPurpose = 'SOCIAL' )

30 Slide 30Chapter 9: Advanced Query Formulation with SQL Advanced Division Problems  Count distinct values rather than rows  Faculty who teach at least one section of selected course offerings  Offering table has duplicate CourseNo values  Use COUNT(DISTINCT column)  Use stored query or nested FROM query in Access

31 Slide 31Chapter 9: Advanced Query Formulation with SQL Advanced Division Problem Example Example 16: List the number and the name of faculty who teach at least one section of all of the fall 2012, information systems courses. SELECT Faculty.FacNo, FacFirstName, FacLastName FROM Faculty, Offering WHERE Faculty.FacNo = Offering.FacNo AND OffTerm = 'FALL' AND CourseNo LIKE 'IS%' AND OffYear = 2012 GROUP BY Faculty.FacNo, FacFirstName, FacLastName HAVING COUNT(DISTINCT CourseNo) = ( SELECT COUNT(DISTINCT CourseNo) FROM Offering WHERE OffTerm = 'FALL' AND OffYear = 2012 AND CourseNo LIKE 'IS%' )

32 Slide 32Chapter 9: Advanced Query Formulation with SQL Null Value Effects  Simple conditions  Compound conditions  Grouping and aggregate functions  SQL:2011 standard but implementation may vary

33 Slide 33Chapter 9: Advanced Query Formulation with SQL Simple Conditions  Simple condition is null if either left-hand or right-hand side is null.  Discard rows evaluating to false or null  Retain rows evaluating to true  Rows evaluating to null will not appear in the result of the simple condition or its negation

34 Slide 34Chapter 9: Advanced Query Formulation with SQL Compound Conditions

35 Slide 35Chapter 9: Advanced Query Formulation with SQL Aggregate Functions  Null values ignored  Effects can be subtle  COUNT(*) may differ from Count(Column)  SUM(Column1) + SUM(Column2) may differ from SUM(Column1 + Column2)

36 Slide 36Chapter 9: Advanced Query Formulation with SQL Grouping Effects  Rows with null values are grouped together  Grouping column contains null values  Null group can be placed at beginning or end of the non-null groups

37 Slide 37Chapter 9: Advanced Query Formulation with SQL Summary  Advanced matching problems not common but important when necessary  Understand outer join, difference, and division operators  Nested queries important for advanced matching problems  Lots of practice to master query formulation and SQL


Download ppt "Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael."

Similar presentations


Ads by Google