Presentasi berjudul: "Advanced Query Formulation dengan SQL"— Transcript presentasi:
1 Advanced Query Formulation dengan SQL Chapter 9Advanced Query Formulation dengan SQLWelcome to Chapter 9 on advanced query formulationChapter 9 covers advanced matching problems:- These problems are not common but important when they occur.- Solving these problems requires more specialized knowledge: higher classgrade and job expertiseObjectives:- Analyze problem statement- Identify the essential problem element- Apply template SQL solutions- SELECT statement extensions- Outer join operator- Nested queries- Recognize- Understand conceptual evaluation process- Interpret SQL statements that use the extensions- Use in advanced matching problems
2 Outline Masalah Outer join Type I nested queries Type II nested queries dan perbedaan masalahnyaNested queries dalam FROM clausemaMasalah DivisionNull value effectsOuter join problems:- One-sided and full outer join- Outer join animation supplements the lecture notes- SQL:1999 notation for outer joins: Access 2002 and Oracle 9i- Older Oracle 8i notation: Oracle 8i is still widely usedType I nested queries:- Understand evaluation- Use in SELECT and DELETE statementsDifference problems:- Understand evaluation of Type II nested queries- Solve difference problems using Type II nested queriesNested queries in the FROM clause:- Motivation: nested aggregates and multiple aggregate computations- Example problemsDivision problems- Division operator: chapter 2 material- Animation supplement to reinforce the meaning of division- Use the Count method to solve division problemsNull value effects:- Simple conditions- Compound conditions- Grouping and aggregate functions
3 Outer Join Overview Join meniadakan baris yang tidak sesuai Pemeliharan baris yang tidak sesuai adalah beberapa dari masalah yang pentingVariasi Outer joinFull outer joinOne-sided outer joinReview of Chapter 2 material: cover for review if desiredCan skip this material in initial coverage of chapter 2 and cover nowImportance of preserving non matching rows:- Offerings without assigned faculty- Orders without sales associatesOuter join variations:- Full: preserves non matching rows of both tables- One-sided: preserves non matching rows of the designated table- One-sided outer join is more common
4 Outer Join Operators Full outer join Left Outer Join Right Outer Join Review of Chapter 2 material: cover for review if desiredCan skip this material in initial coverage of chapter 2 and cover nowOuter join matching:- join columns, not all columns as in traditional set operators- One-sided outer join: preserving non matching rows of a designated table(left or right)- Full outer join: preserving non matching rows of both tables- See outer join animation for interactive demonstrationUnmatched rows of the left tableMatched rows using the join conditionUnmatched rows of the right table
5 Full Outer Join Example Outer join result:- Join part: rows 1 – 3- Outer join part: non matching rows (rows 4 and 5)- Null values in the non matching rows: columns from the other tableOne-sided outer join:- Preserve non matching rows of the designated table- Preserve the Faculty table in the result: first four rows- Preserve the Offering table: first three rows and fifth rowSee outer join animation for interactive example
6 University DatabaseKnowledge of optional relationships useful for one-sided outer joins- Optional relationship: FK accepts null values- Offering.FacSSN allows null values for Offerings without assigned faculty- OrderTbl.EmpNo (Order Entry database) allows null values for Internet orders(orders without a sales associate)
7 LEFT JOIN dan RIGHT JOIN Keywords contoh 1 (Access)SELECT OfferNo, CourseNo, Offering.FacSSN,FacFirstName, FacLastNameFROM Offering LEFT JOIN FacultyON Offering.FacSSN = Faculty.FacSSNWHERE CourseNo LIKE 'IS*'contoh 2 (Access)FROM Faculty RIGHT JOIN OfferingExample 1:- Preserve rows of the table on the left (Offering)- Result includes Offering rows that join with Faculty rows as well as non matchingOffering rowsExample 2:- Result is identical to Example 1Outer join keywords: Access syntax as well as SQL:1999 syntax and Oracle 9iOracle 9i examples are identical except for % wild card character
8 Notasi Oracle 8i untuk One-Sided Outer Joins contoh 3 (Oracle 8i)SELECT OfferNo, CourseNo, Offering.FacSSN,FacFirstName, FacLastNameFROM Faculty, OfferingWHERE Offering.FacSSN = Faculty.FacSSN (+)AND CourseNo LIKE 'IS%'contoh 4 (Oracle 8i)WHERE Faculty.FacSSN (+) = Offering.FacSSNOracle 8i does not support SQL:1999 syntax for outer joins- Oracle developed its own proprietary syntax that predates SQL-92 and SQL:1999- Oracle 9i supports the standard SQL notation for outer joins- Oracle notation inferior to SQL:1999 notation when multiple outer joins- For multiple outer join (not common), ordering must be specified for multipleouter joins)- Oracle 8i is still widely used so older notation is presented(+) notation:- Place by the null table (table with null values in the result)- Opposite of the SQL:1999 notationExample 3 and 4 produce the same results as examples 1 and 2
9 Contoh I Full Outer Join contoh 5 (SQL:1999 and Oracle 9i)SELECT FacSSN, FacFirstName, FacLastName,FacSalary, StdSSN, StdFirstName,StdLastName, StdGPAFROM Faculty FULL JOIN StudentON Student.StdSSN = Faculty.FacSSNExample 5:- Full outer join: retrieve all rows of two similar but not union compatible tables- Student and Faculty both are kinds of university people- Retrieves selected columns of student and faculty tables- Result contains every university person (student only, faculty only, TA)- Can use full outer join on tables that are not union compatible- Full outer join makes comparison on join column(s) not all columns- SQL:1999 (also SQL-92): FULL JOIN keyword
10 Contoh II Full Outer Join contoh 5 (Access)SELECT FacSSN, FacFirstName, FacLastName,FacSalary, StdSSN, StdFirstName,StdLastName, StdGPAFROM Faculty RIGHT JOIN StudentON Student.StdSSN = Faculty.FacSSNUNIONFROM Faculty LEFT JOIN StudentNotation:- Access: UNION of two one-sided outer joins; LEFT JOIN, RIGHT JOIN notation- Result is identical to Example 5 (FULL JOIN)
11 Contoh III Full Outer Join contoh 5 (Oracle 8i)SELECT FacSSN, FacFirstName, FacLastName,FacSalary, StdSSN, StdFirstName,StdLastName, StdGPAFROM Faculty, StudentWHERE Student.StdSSN = Faculty.FacSSN (+)UNIONWHERE Student.StdSSN (+) = Faculty.FacSSNNotation:- SQL:1999: FULL JOIN keyword- Access: UNION of two one-sided outer joins; LEFT JOIN, RIGHT JOIN notation- Oracle: UNION of two one-sided outer joins; (+) notation- Result is identical to Example 5 for Oracle 9i and Access
12 Mixing Inner dan Outer Joins I contoh 6 (Access)SELECT OfferNo, Offering.CourseNo, OffTerm,CrsDesc, Faculty.FacSSN, FacLastNameFROM ( Faculty RIGHT JOIN OfferingON Offering.FacSSN = Faculty.FacSSN )INNER JOIN CourseON Course.CourseNo = Offering.CourseNoWHERE Course.CourseNo LIKE 'IS*'Mixing inner and outer joins:- Very common when using one-sided outer join- Access requires parentheses when using more than one join operationin the FROM clause (should not be required for INNER JOINs)- Nested parentheses make the statement somewhat difficult to read- Access rule on combining inner and outer joins:- Cannot nest inner join inside outer joins- Perform outer joins first: make outer join most deeply nested- Oracle 9i solution: use % instead of *
13 Mixing Inner dan Outer Joins II contoh 6 (Oracle 8i)SELECT OfferNo, Offering.CourseNo, OffTerm,CrsDesc, Faculty.FacSSN, FacLastNameFROM Faculty, Course, OfferingWHERE Offering.FacSSN = Faculty.FacSSN (+)AND Course.CourseNo = Offering.CourseNoAND Course.CourseNo LIKE 'IS%'Mixing inner and outer joins:- Very common when using one-sided outer join- Oracle 8i solution is easier to read: no parentheses- Oracle 8i notation does not generalize to difficult problems involving multiple outer joins
14 Type I Nested Queries Query di dalam query Menggunakan WHERE dan HAVING conditionsSama dengan prosedur nestedMenjalankan satu kaliTidak ada reference ke outer queryJuga dikenal sebagai non-correlated atau independent nested queryNested query:- query inside a query (SELECT statement inside a SELECT statement)- Use in conditions in the WHERE and HAVING clauses- Also use nested queries in the FROM clauseType I:- Primarily an alternative join styleNested procedure execution model: procedure executes and is replaced by a value- Executes one time and is replaced by a list of valuesDistinguishing feature:- No reference to outer query- Type I nested query is independent of outer query- Non-correlated or independent nested query
15 Contoh Type I Nested Query Contoh 7 (Access): List finance faculty who teach IS courses.SELECT FacSSN, FacLastName, FacDeptFROM FacultyWHERE FacDept = 'FIN' AND FacSSN IN( SELECT FacSSN FROM OfferingWHERE CourseNo LIKE 'IS*' )Contoh 8 (Oracle): List finance faculty who teach 4 unit IS courses.WHERE CourseNo LIKE 'IS%' AND CourseNo IN( SELECT CourseNo FROM CourseWHERE CrsUnits = 4 ) )Example 7:- Type I nested query replaces a join to Offering table- IN keyword: "set element of" operator- Nested query executes one time and produces a list of FacSSN values- Oracle: use % instead of *Applicability of Type I nested queries:- Do not need columns from nested query in the final result- Cannot use Type I nested query if Offering columns needed in the result- Use Type I nested queries to test a condition from another tableExample 8:- Nested query inside a nested query- Second nested query tests a condition from the Course table- Access: use * instead of %- Cannot use type I nested queries if a column from the offering or course table is needed
16 Contoh DELETEGunakan Type I nested queries untuk menguji kondisi pada tabel lainGunakan juga UPDATE statementsContoh 9: Delete offerings taught by Leonard Vince.DELETE FROM OfferingWHERE Offering.FacSSN IN( SELECT FacSSN FROM FacultyWHERE FacFirstName = 'Leonard'AND FacLastName = 'Vince' )Type I nested query good for complex deletions:- Test conditions on other tables- Portable across most DBMSs
17 Type II Nested Queries Sama dengan nested loops Menjalan sekali saja untuk setiap baris dari outer queryReference ke outer queryDikenal juga sebagai correlated or variably nested queryPenggunaan masalah difference adalah bukan joinsType II:- More complex execution model- Nested loop execution model: inner loop executes one time for each outer loop iteration- Look for a column in nested query that refers to a table used in the outer query- Use for difference problems- Do not use for joins: performance penalty likely- See animation slides on Type II nested queries
18 Contoh Type II Nested Query Contoh 10: Retrieve MS faculty who are not teaching in winter 2003.SELECT FacSSN, FacLastName, FacDeptFROM FacultyWHERE FacDept = 'MS' AND NOT EXISTS( SELECT * FROM OfferingWHERE OffTerm = 'WINTER'AND OffYear = 2003AND Faculty.FacSSN = Offering.FacSSN )EXISTS:- Table comparison operator- True if nested query produces 1 or more rows; false otherwise- NOT EXISTS: true if nested query produces 0 rows; false otherwiseDifference problem:- Elements in one set but not another set- All MS faculty MINUS MS faculty teaching in winter 2003- Remaining set contains MS faculty not teaching in winter 2003- Look for "not" connecting different parts of a sentence (faculty not teaching)Example 10:- Type II nested query: Faculty.FacSSN references Faculty table in the outer query- See Nested Query animation for derivation of the result
19 Alternatif Difference Formulation Contoh 11: Retrieve MS faculty who are not teaching in winter 2003.SELECT FacSSN, FacLastName, FacDeptFROM FacultyWHERE FacDept = 'MS' AND FacSSN NOT IN( SELECT FacSSN FROM OfferingWHERE OffTerm = 'WINTER'AND OffYear = 2003 )Example 11:- Nested query is Type I (no reference to outer query)- Type I formulation does not work for complex difference problems such asExample 9.20 which involves a comparison of multiple correlated columnsin the Type II nested query
20 Nested Queries dalam FROM Clause Lebih banyak pengenak baru daripada nested query dalam klausa WHERE dan HAVINGKonsisten dalam bahasa designnyaDimanapun tabel terlihat, ekspresi tabel dapat terlihat.Spesifikasinya:Nested aggregatesMultiple independent aggregate calculationsNested queries in the WHERE and HAVING clauses were part of original SELECT statement designNested queries in the FROM clause were introduced in SQL-92Consistency in language design: consistencyWherever X constants are permitted, X expressions should be permittedFROM clause supports table constants and table expressions (SELECT statements)Specialized uses: not as important as nested queries in the FROM and HAVING clausesNested aggregates: average of the sum of credit hours or average of the number of studentsMultiple aggregates: number of students enrolled and the average resources consumed in the course (grouping of different tables)
21 Contoh Nested FROM Query Contoh 12: Retrieve the course number, course description, thenumber of offerings, and the average enrollment across offering.SELECT T.CourseNo, T.CrsDesc,COUNT(*) AS NumOfferings,Avg(T.EnrollCount) AS AvgEnrollFROM(SELECT Course.CourseNo, CrsDesc,Offering.OfferNo,COUNT(*) AS EnrollCountFROM Offering, Enrollment, CourseWHERE Offering.OfferNo = Enrollment.OfferNoAND Course.CourseNo = Offering.CourseNoGROUP BY Course.CourseNo, CrsDesc,Offering.OfferNo) TGROUP BY T.CourseNo, T.CrsDescExample 12:Needs a nested query in the FROM clause because nested aggregate is computedNested aggregate: average number of students enrolled per offeringNested query retrieves one row per offering: course number, description, and number of students enrolledThe outer query groups the nested query by course number and description so that number of offerings and the average enrollment per offering can be computedA useful query to compare introductory courses containing many sectionsMust use two queries to formulate if not using a nested query in the FROM clause
22 Operator Divide Pasangan subset values operator khusus Suppliers yang mensupply semua bagianFaculty yang mengajar setiap kursus ISoperator khususSecara tipikal diterapkan untuk associative tabel yang menyajikan M-N relationshipsChapter 2 material:- Present as review if desired- Can skip material when covering chapter 2 and instead cover nowSubset matching:- Use of every or all connecting different parts of a sentence- Use any or some: join problem- Specialized matching but important when necessary- Conceptually difficultTable structures:- Typically applied to associative tables such as Enrollment, Supp-Part, StdClub- Can also be applied to child (M) tables in a 1-M relationship (Offering table)
23 Contoh Division Chapter 2 material: - Present as review if desired - Can skip material when covering chapter 2 and instead cover now- See also animation slides for the division operatorTable structure:- SuppPart: associative table between Part and Supp tables- List suppliers who supply every partFormulation:- See Division animation for interactive presentation- Sort SuppPart table by SuppNo- Choose Suppliers that are associated with every part- Set of parts for a supplier contains the set of all parts- S3 associated with P1, P2, and P3- Must look at all rows with S3 to decide whether S3 is in the result
24 Metode COUNT untuk masalah Division Membandingkan jumlah dari baris yang digabungkan dengan group jumlah total dari subset interest.Type I nested query dalam the HAVING clauseContoh 13: List the students who belong to all clubs.SELECT StdNoFROM StdClubGROUP BY StdNoHAVING COUNT(*) =( SELECT COUNT(*) FROM Club )Count method:- Compare the number of rows in two sets rather than comparing the sets directly- Make sure that the sets are comparable- Use Type I nested query in the HAVING clause:- COUNT(*) to table with a single row and column (COUNT(*))- Much simpler than alternative formulations: doubly nested Type II subqueriesExample 13:- Left-hand COUNT(*): number of rows in a StdNo group- Nested query: total number of clubs- Nested query executes one time: no reference to the outer query
25 Masalah Typical Division Membandingkan subset yang menarik daripada entire tableMenggunakan kondisi yang sama dalam outer dan nested queryContoh 13: List the students who belong to all social clubs.SELECT Student1.StdNo, SNameFROM StdClub, Club, Student1WHERE StdClub.ClubNo = Club.ClubNoAND Student1.StdNo = StdClub.StdNoAND CPurpose = 'SOCIAL'GROUP BY Student1.StdNo, SNameHAVING COUNT(*) =( SELECT COUNT(*) FROM ClubWHERE CPurpose = 'SOCIAL' )Example 13:- Interesting subset: social clubs- Same condition in WHERE clause of both queries (outer and nested)- Ensures that sets are comparable
26 Masalah Advanced Division Jumlah perbedaan nilai daripada barisFaculty yang mengajar paling sedikit satu session dari tawaran kursus yang ada.Duplikasi penggunaan tableMenggunakan COUNT(DISTINCT column)Menggunakan stored query atau nested FROM query dalam AccessUse COUNT(DISTINCT …) to count unique column valuesSee Section for detailed explanationAccess:- Does not support COUNT(DISTINCT …)- Use SELECT DISTINCT in a stored query for the same effect- Use a nested query in the FROM clause for the same effect- See Appendix 9.A for Access details about stored queries
27 Contoh masalah Advanced Division Contoh 14: List the SSN and the name of faculty who teach at leastone section of all of the fall, 2002, IS courses.SELECT Faculty.FacSSN, FacFirstName,FacLastNameFROM Faculty, OfferingWHERE Faculty.FacSSN = Offering.FacSSNAND OffTerm = 'FALL' AND CourseNo LIKE 'IS%'AND OffYear = 2002GROUP BY Faculty.FacSSN, FacFirstName,HAVING COUNT(DISTINCT CourseNo) =( SELECT COUNT(DISTINCT CourseNo)FROM OfferingWHERE OffTerm = 'FALL' AND OffYear = 2002AND CourseNo LIKE 'IS%' )Example 9.29 is not particularly useful because it is unlikely that an instructor has taught every offering. Rather, it is more useful that an instructor has taught one offering of every course as demonstrated in Example 14 (9.30). Rather than counting the rows in each group, count the unique CourseNo values. This change is necessary because CourseNo is not unique in the Offering table. There can be multiple rows with the same CourseNo, corresponding to a situation where there are multiple offerings for the same course. The solution only executes in Oracle because Access does not support the DISTINCT keyword in aggregate functions.
28 Null Value Efek Kondisi yang sederhana Kondisi campuran Pengelompokan dan fungsi agregasiSQL:1999 standard tetapi implementasinya beragamThe last section of this chapter does not involve difficult matching problems or new parts of SQL. Rather, this section presents interpretation of query results when tables contain null values. These effects have largely been ignored until this section to simplify the presentation. Because most databases use null values, you need to understand the effects to attain a deeper understanding of query formulation.Null values affect simple conditions involving comparison operators, compound conditions involving Boolean operators, aggregate calculations, and grouping. As you will see, some of the null value effects are rather subtle. Because of these subtle effects, a good table design minimizes, although it usually does not eliminate, the use of null values. The null effects described in this section are specified in the SQL2 standard. Because specific DBMSs may provide different results, you may need to experiment with your DBMS.
29 Kondisi yang sederhana Kondisi yang sederhana adalah null jika sisi left-hand maupun right-hand adalah null.Pembuangan evaluasi baris untuk false atau nullMempertahankan evaluasi baris yang benarEvaluasi baris untuk null tidak akan nampak dalam hasil dari kondisi sederhana atau negasinya.Simple conditions involve a comparison operator, a column or column expression and a constant, column, or column expression. A simple condition results in a null value if either column (or column expression) in a comparison is null. A row qualifies in the result if the simple condition evaluates to true for the row. Rows evaluating to false and null are discarded.A more subtle result can occur when a simple condition involves two columns and at least one column contains null values. If neither column contains null values, every row will be in the result of either the simple condition or the opposite (negation) of the simple condition. For example, if < is the operator of a simple condition, the opposite condition contains as its operator assuming the columns remain in the same positions. If at least one column contains null values, some rows will not appear in the result of either the simple condition or its negation.
30 Compound ConditionsCompound conditions involve one or more simple conditions connected by the Boolean operators AND, OR, and NOT. Like simple conditions, compound conditions evaluate to true, false, or null. A row is selected if the entire compound condition in the WHERE clause evaluates to true.To evaluate the result of a compound condition, the SQL2 standard uses truth tables with three values. A truth table shows how combinations of values (true, false, and null) combine with the Boolean operators. Truth tables with three values define a three-valued logic. The above tables depict truth tables for the AND, OR, and NOT operators. The internal cells in these tables are the result values. For example, the first internal cell (True) in Table 13 results from the AND operator applied to two conditions with true values.
31 Fungsi Agregat Nilai null tidak diperbolehkan Pengaruhnya dapat terlihat padaCOUNT(*) may differ from Count(Column)SUM(Column1) + SUM(Column2) may differ from SUM(Column1 + Column2)Null values are ignored in aggregate calculations. Although this statement sounds simple, the results can be subtle. For the COUNT function, COUNT(*) returns a different value than COUNT(column) if the column contains null values. COUNT(*) always returns the number of rows. COUNT(column) returns the number of non-null values in the column.An even more subtle effect can occur if the SUM or AVG functions are applied to a column with null values. Without regard to null values, the following equation is true: SUM(Column1) + SUM(Column2) = SUM(Column1 + Column2). With null values in at least one of the columns, the equation may not be true because a calculation involving a null value yields a null value. If Column1 has a null value in one row, the plus operation in SUM(Column1 + Column2) produces a null value for that row. However, the value of Column2 in the same row is counted in SUM(Column2).
32 Pengaruh Pengelompkkan Baris dengan nilai null adalah kelompok bersama.Pengelompokan kolom berisi nilai null.Kelompok null bisa diganti dengan memulai atau mengakhiri dari kelompok bukan null.Null values also can affect grouping operations performed in the GROUP BY clause. The SQL2 standard stipulates that all rows with null values are grouped together. The grouping column shows null values in the result. In the university database, this kind of grouping operation is useful to find course offerings without assigned professors.
33 RingkasanMasalah sesuai Advanced itu tidak biasa tetapi penting ketika dibutuhkan.Pemahaman outer join, difference, dan division operatorsNested queries penting untuk masalah yang sesuai advanced.Banyak latihan buat master query formulasi dan SQL.Gain competitive advantage by understanding advanced matching problems:- Class: difference between A and B (this may not be true in your class)- Job: solving difficult but important problemsSkills:- Understand outer join, difference, and division operators- Recognize problem statements that involve these operators- Modify a template SQL solution to formulate the SELECT statement- Nested queries used in difference and division problemsLots of practice- Work many problems without seeing the solutions- 50 problems to develop understanding of query formulation and SQL- Do not rely on visual tools such as Query Design in Access; use SQL directly