Presentation is loading. Please wait.

Presentation is loading. Please wait.

9 Advanced Query Formulation with SQL (Chapter 9).

Similar presentations


Presentation on theme: "9 Advanced Query Formulation with SQL (Chapter 9)."— Presentation transcript:

1 9 Advanced Query Formulation with SQL (Chapter 9)

2 2 Outline  Null value effects  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Division problems

3 3 Null Value Effects Null values affect  Simple conditions involving comparison operators  Compound conditions involving logical operators  Aggregate calculation  Grouping SQL:2003 standard but implementation may vary

4 4 Effect of Null Value on Simple Conditions  A simple condition results in null if either left-hand or right-hand side of its comparison operator is null.  A comparison operator typically involves a column (or column expression) and a constant (or column, or column expression)  Example SELECT * FROM Club WHERE CBudget > 200  SQL result retains rows evaluating to true, hence SQL result discards rows evaluating to false or null  Rows evaluating to null will not appear in the result of the simple condition or its negation (evaluating to false) A simple condition. Its result could be true, false, or null.

5 5 Effect of Null Value on Compound Conditions

6 6  Null values are ignored  Effects can be subtle  COUNT(*) may differ from Count(Column )  COUNT(*) returns the number of rows  Count(Column ) returns the number of non-null values in the column  SUM(Column1) + SUM(Column2) may differ from SUM(Column1 + Column2)  An aggregate Functions ignore null values SUM(Column1 ) + SUM(Column2 ) = 1000  Sum({500, null}) = 500  Sum({350, 150}) = 500  A math equation involving a null value yields a null value SUM(Column1 + Column2 ) = 850  Column1 + Column2 of record 1 = 850  Column1 + Column2 of record 2 = null  Therefore, Sum({850, null}) = 850 Column1Column2 500350 150 Effect of Null Value on Aggregate Functions

7 7  All rows with null values are grouped together  Null group can be placed at the beginning or end of grouping result  Oracle default: End of the grouping result  ACCESS default : Beginning of the grouping result SELECT FacSupervisor, COUNT(*) FROM Faculty GROUP BY FacSupervisor ORDER BYFacSupervisor NULLS FIRST; Effect of Null Value on Grouping Operations

8 8 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

9 9 Outer Join Operators Right Outer JoinLeft Outer Join Join Matched rows using the join condition Unmatched rows of the left table are retained Unmatched rows of the right table are retained Full outer join

10 10 Full Outer Join Example

11 11 University Database (Relationship Window)

12 12 Example of Using LEFT JOIN Keyword Example 1 (Oracle) For Information Systems courses, retrieve the offer number, the course number, the faculty number, and the faculty name. Include an offering in the result even if the instructor is not assigned yet. SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Offering LEFT JOIN Faculty ON Offering.FacSSN = Faculty.FacSSN WHERE CourseNo LIKE 'IS%'; D/B Diagram

13 13 Example of Using RIGHT JOIN Keyword Example 2 (Oracle) For Information Systems courses, retrieve the offer number, the course number, the faculty number, and the faculty name. Include an offering in the result even if the instructor is not assigned yet. SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Faculty RIGHT JOIN Offering ON Offering.FacSSN = Faculty.FacSSN WHERE CourseNo LIKE 'IS%' ; The result is identical to Example 1 (using LEFT JOIN ) Diagram D/B

14 14 Full Outer Join Example Example 3 (Oracle, but not Microsoft Access) Combine the Faculty and Student tables using a full outer join. SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty FULL JOIN Student ON Student.StdSSN = Faculty.FacSSN; D/B Diagram

15 15 Full Outer Join Example Example 4 (Microsoft 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;

16 16 Mixing Inner and One-Sided Outer Join Example 5 Combine columns from the Faculty, Offering, and Course tables for Information Systems courses offered in 2006. Include a row in the result even if there is not an assigned instructor. 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%‘ AND OffYear = 2006;

17 17 Nested Queries (Subqueries)  Query inside a query  Typically appears in WHERE and HAVING conditions  Can also be used in FROM clause  Type I and type II nested queries

18 18 Type I Nested Queries (Subqueries)  The whole query has at least two sub-queries  The outer sub-query  The inner sub-query (or nested query)  A Type I nested query is like a procedure in a programming language. It evaluates one time and produce a table  The nested (inner) query does not reference the outer query  Using the IN comparison operator to correlate the outer and inner sub-queries.

19 19 Example 7: List the Social Security number, name, and major of students who have a high grade (>= 3.5) in course offering. SELECT StdSSN, StdFirstName, StdFirstName, StdMajor FROM Student WHERE Student.StdSSN IN ( SELECT StdSSN FROM Enrollment WHERE EnrGrade >= 3.5 ); Type I Nested Query Examples I inner sub-query outer sub-query

20 20 DELETE Example Using Type I Nested Query  Use Type I nested queries to test conditions on other tables  Use for UPDATE statements also Example 10: Delete offerings taught by Leonard Vince. DELETE FROM Offering WHERE Offering.FacSSN IN ( SELECT FacSSN FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' );

21 21 Type II Nested Queries  Similar to nested loops  Executes one time for each row of outer query  Inner query reference one or more columns from the outer query  Also known as correlated subqueries  Use for difference problems not joins  Using the EXISTS or NOT EXISTS operator

22 22 Type II Nested Query Example for a Difference Problem Example 16: Retrieve the Social Security number, the name, and the department of faculty who are not student. SELECT FacSSN, FacFirstName, FacLastName, FacDept FROM Faculty WHERE NOT EXISTS ( SELECT * FROM Student WHERE Student.StdSSN = Faculty.FacSSN );

23 23 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

24 24 Type I Difference Formulation Example 12 Retrieve the Social Security number, the name, and the department of faculty who are not student. SELECT FacSSN, FacFirstName, FacLastName, FacDept FROM Faculty WHERE FacSSN NOT IN ( SELECT StdSSN FROM Student ) The result is identical to Example 16 (using Type II Nested Query )

25 25 One-Sided Outer Join Difference Formulation Example 13 Retrieve the Social Security number, the name, and the department of faculty who are not student. SELECT FacSSN, FacFirstName, FacLastName, FacDept FROM Faculty LEFT JOIN Student ON Faculty.FacSSN = Student.StdSSN WHERE Student.StdSSN IS NULL; The result is identical to Example 12, 16

26 26 MINUS Operator Difference Formulation Example 14: Retrieve faculty who are not students SELECT FacSSN AS SSN, FacFirstName AS FirstName, FacLastName AS LastName, FacCity AS City, FacState AS State FROM Faculty MINUS SELECT StdSSN, StdFirstName, StdLastName, StdCity, StdState FROM Student; The result is identical to Example 12, 13, 16

27 27 Divide Operator  Match on a subset of values  Suppliers who supply all parts  Faculty who teach every IS course  Specialized operator  Typically applied to associative tables representing M-N relationships

28 28 Table Example for Division Problem StdNoClubNo S1C1 S1C2 S1C3 S1C4 S2C1 S2C4 S3C3 ClubNoCNameCPurposeCBudgetCActual C1DELTASOCIAL$1,000.00$1,200.00 C2BITSACADEMIC$500.00$350.00 C3HELPSSERVICE$300.00$330.00 C4SIGMASOCIAL$150.00 Table 9-12: StdClub Table Listing Table 9-11: Club Table Listing

29 29 Simple Division Problems (Using COUNT Function)  The approach use COUNT function with a Type I nested query in the HAVING clause  Compare the number of rows associated with a group to the total number of another group of interest, like a division. Example 24 List the student number of students who belong to all clubs. SELECT StdNo FROM StdClub GROUP BY StdNo HAVING COUNT(*) = ( SELECT COUNT(*) FROM Club );

30 30 Typical Division Problems  Compare to an interesting subset rather than entire table  Use similar conditions in outer and nested queries Example 25: List the student number of students who belong to all social clubs. SELECT StdNo FROM StdClub, Club WHERE StdClub.ClubNo = Club.ClubNo AND CPurpose = 'SOCIAL' GROUP BY StdNo HAVING COUNT(*) = ( SELECT COUNT(*) FROM Club WHERE CPurpose = 'SOCIAL' );

31 31 Summary  Advanced matching problems are not common but important when necessary  It is important to understand outer join, difference, and division operators  Nested queries are important for advanced matching problems  Lots of practice to master query formulation and SQL


Download ppt "9 Advanced Query Formulation with SQL (Chapter 9)."

Similar presentations


Ads by Google