chapter 9 advanced query formulation with sql. outline outer join problems type i nested queries...

33
Chapter 9 Chapter 9 Advanced Query Formulation with SQL

Post on 22-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Chapter 9Chapter 9Advanced Query Formulation with SQL

Page 2: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Outline Outline

Outer join problemsType I nested queriesType II nested queries and difference

problemsNested queries in the FROM clauseDivision problemsNull value effects

Page 3: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Outer Join OverviewOuter Join Overview

Join excludes non matching rowsPreserving non matching rows is important

in some problems Outer join variations

– Full outer join– One-sided outer join

Page 4: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Right Outer Join

Outer Join OperatorsOuter 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

Page 5: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Full Outer Join ExampleFull Outer Join Example

FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara

Offerno FacSSN

1111 111-11-1111 2222 222-22-2222 3333 111-11-1111 4444

FacSSN FacName OfferNo 111-11-1111 joe 1111

222-22-2222 sue 2222

111-11-1111 joe 3333

333-33-3333 sara

4444

Faculty

Offering

Outer Join of Offering and Faculty

Page 6: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

University DatabaseUniversity Database

Page 7: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

LEFT JOIN and RIGHT JOIN LEFT JOIN and RIGHT JOIN KeywordsKeywords

Example 1 (Access) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Offering LEFT JOIN Faculty ON Offering.FacSSN = Faculty.FacSSN WHERE CourseNo LIKE 'IS*'

Example 2 (Access) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Faculty RIGHT JOIN Offering ON Offering.FacSSN = Faculty.FacSSN WHERE CourseNo LIKE 'IS*'

Page 8: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Oracle 8i Notation for One-Oracle 8i Notation for One-Sided Outer JoinsSided Outer Joins

Example 3 (Oracle 8i) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Faculty, Offering WHERE Offering.FacSSN = Faculty.FacSSN (+) AND CourseNo LIKE 'IS%'

Example 4 (Oracle 8i) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Faculty, Offering WHERE Faculty.FacSSN (+) = Offering.FacSSN AND CourseNo LIKE 'IS%'

Page 9: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Full Outer Join Example IFull Outer Join Example I

Example 5 (SQL:1999 and Oracle 9i) SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty FULL JOIN Student ON Student.StdSSN = Faculty.FacSSN

Page 10: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Full Outer Join Example IIFull Outer Join Example II

Example 5 (Access) SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty RIGHT JOIN Student ON Student.StdSSN = Faculty.FacSSN

UNION SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty LEFT JOIN Student ON Student.StdSSN = Faculty.FacSSN

Page 11: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Full Outer Join Example IIIFull Outer Join Example III

Example 5 (Oracle 8i) SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty, Student WHERE Student.StdSSN = Faculty.FacSSN (+)

UNION SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty, Student WHERE Student.StdSSN (+) = Faculty.FacSSN

Page 12: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Mixing Inner and Outer Joins IMixing Inner and Outer Joins I

Example 6 (Access)

SELECT OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacSSN, FacLastName FROM ( Faculty RIGHT JOIN Offering ON Offering.FacSSN = Faculty.FacSSN ) INNER JOIN Course ON Course.CourseNo = Offering.CourseNo WHERE Course.CourseNo LIKE 'IS*'

Page 13: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Mixing Inner and Outer Joins IIMixing Inner and Outer Joins II

Example 6 (Oracle 8i)

SELECT OfferNo, Offering.CourseNo, OffTerm, CrsDesc, Faculty.FacSSN, FacLastName FROM Faculty, Course, Offering WHERE Offering.FacSSN = Faculty.FacSSN (+) AND Course.CourseNo = Offering.CourseNo AND Course.CourseNo LIKE 'IS%'

Page 14: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Type I Nested QueriesType I Nested Queries

Query inside a queryUse in WHERE and HAVING conditionsSimilar to a nested procedureExecutes one timeNo reference to outer queryAlso known as non-correlated or

independent nested query

Page 15: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Type I Nested Query ExamplesType I Nested Query ExamplesExample 7 (Access): List finance faculty who teach IS courses. SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'FIN' AND FacSSN IN ( SELECT FacSSN FROM Offering WHERE CourseNo LIKE 'IS*' )

Example 8 (Oracle): List finance faculty who teach 4 unit IS courses.SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'FIN' AND FacSSN IN ( SELECT FacSSN FROM Offering WHERE CourseNo LIKE 'IS%' AND CourseNo IN ( SELECT CourseNo FROM Course WHERE CrsUnits = 4 ) )

Page 16: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

DELETE ExampleDELETE Example

• Use Type I nested queries to test conditions on other tables

• Use for UPDATE statements also

Example 9: Delete offerings taught by Leonard Vince. DELETE FROM Offering WHERE Offering.FacSSN IN ( SELECT FacSSN FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' )

Page 17: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Type II Nested QueriesType II Nested Queries

Similar to nested loopsExecutes one time for each row of outer

queryReference to outer queryAlso known as correlated or variably

nested queryUse for difference problems not joins

Page 18: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Type II Nested Query ExampleType II Nested Query Example

Example 10: Retrieve MS faculty who are not teaching in winter 2003. SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'MS' AND NOT EXISTS ( SELECT * FROM Offering WHERE OffTerm = 'WINTER' AND OffYear = 2003 AND Faculty.FacSSN = Offering.FacSSN )

Page 19: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Alternative Difference Alternative Difference FormulationFormulation

Example 11: Retrieve MS faculty who are not teaching in winter 2003. SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'MS' AND FacSSN NOT IN ( SELECT FacSSN FROM Offering WHERE OffTerm = 'WINTER' AND OffYear = 2003 )

Page 20: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Nested Queries in the FROM Nested Queries in the FROM ClauseClauseMore recent introduction than nested queries

in the WHERE and HAVING clausesConsistency in language designWherever table appears, table expression can

appearSpecialized uses

– Nested aggregates– Multiple independent aggregate calculations

Page 21: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Nested FROM Query ExampleNested FROM Query ExampleExample 12: Retrieve the course number, course description, the number of offerings, and the average enrollment across offering. 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

Page 22: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Divide OperatorDivide Operator

Match on a subset of values– Suppliers who supply all parts– Faculty who teach every IS course

Specialized operatorTypically applied to associative tables

representing M-N relationships

Page 23: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Division ExampleDivision Example

SuppNo PartNo s3 p1 s3 p2 s3 p3 s0 p1 s1 p2

PartNo p1 p2 p3

SuppNo s3

SuppPart Part SuppPart DIVIDEBY Part

s3 {p1, p2, p3}contains {p1, p2, p3}

Page 24: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

COUNT Method for Division COUNT Method for Division ProblemsProblems

• 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 13: List the students who belong to all clubs. SELECT StdNo FROM StdClub GROUP BY StdNo HAVING COUNT(*) = ( SELECT COUNT(*) FROM Club )

Page 25: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Typical Division ProblemsTypical Division Problems

• Compare to an interesting subset rather than entire table

• Use similar conditions in outer and nested query

Example 13: 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' )

Page 26: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Advanced Division ProblemsAdvanced 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

Page 27: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Advanced Division Problem Advanced Division Problem ExampleExample

Example 14: List the SSN and the name of faculty who teach at least one section of all of the fall, 2002, IS courses.SELECT Faculty.FacSSN, FacFirstName, FacLastName FROM Faculty, Offering WHERE Faculty.FacSSN = Offering.FacSSN AND OffTerm = 'FALL' AND CourseNo LIKE 'IS%' AND OffYear = 2002 GROUP BY Faculty.FacSSN, FacFirstName, FacLastName HAVING COUNT(DISTINCT CourseNo) = ( SELECT COUNT(DISTINCT CourseNo) FROM Offering WHERE OffTerm = 'FALL' AND OffYear = 2002 AND CourseNo LIKE 'IS%' )

Page 28: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Null Value EffectsNull Value Effects

Simple conditionsCompound conditionsGrouping and aggregate functionsSQL:1999 standard but implementation

may vary

Page 29: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Simple ConditionsSimple Conditions

Simple condition is null if either left-hand or right-hand side is null.

Discard rows evaluating to false or nullRetain rows evaluating to trueRows evaluating to null will not appear in

the result of the simple condition or its negation

Page 30: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Compound ConditionsCompound ConditionsAND True False Null

True True False Null

False False False False

Null Null False Null

OR True False Null

True True True True

False True False Null

Null True Null Null

NOT True False Null

False True Null

Page 31: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Aggregate FunctionsAggregate Functions

Null values ignoredEffects can be subtle

– COUNT(*) may differ from Count(Column)– SUM(Column1) + SUM(Column2) may differ

from SUM(Column1 + Column2)

Page 32: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

Grouping EffectsGrouping Effects

Rows with null values are grouped together

Grouping column contains null valuesNull group can be placed at beginning or

end of the non-null groups

Page 33: Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested

SummarySummary

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