Advanced Query Formulation dengan SQL Chapter 9 Advanced Query Formulation dengan SQL Welcome to Chapter 9 on advanced query formulation Chapter 9 covers advanced matching problems: - These problems are not common but important when they occur. - Solving these problems requires more specialized knowledge: higher class grade and job expertise Objectives: - 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
Outline Masalah Outer join Type I nested queries Type II nested queries dan perbedaan masalahnya Nested queries dalam FROM clausema Masalah Division Null value effects Outer 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 used Type I nested queries: - Understand evaluation - Use in SELECT and DELETE statements Difference problems: - Understand evaluation of Type II nested queries - Solve difference problems using Type II nested queries Nested queries in the FROM clause: - Motivation: nested aggregates and multiple aggregate computations - Example problems Division problems - Division operator: chapter 2 material - Animation supplement to reinforce the meaning of division - Use the Count method to solve division problems Null value effects: - Simple conditions - Compound conditions - Grouping and aggregate functions
Outer Join Overview Join meniadakan baris yang tidak sesuai Pemeliharan baris yang tidak sesuai adalah beberapa dari masalah yang penting Variasi Outer join Full outer join One-sided outer join Review of Chapter 2 material: cover for review if desired Can skip this material in initial coverage of chapter 2 and cover now Importance of preserving non matching rows: - Offerings without assigned faculty - Orders without sales associates Outer 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
Outer Join Operators Full outer join Left Outer Join Right Outer Join Review of Chapter 2 material: cover for review if desired Can skip this material in initial coverage of chapter 2 and cover now Outer 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 demonstration Unmatched rows of the left table Matched rows using the join condition Unmatched rows of the right table
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 table One-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 row See outer join animation for interactive example
University Database Knowledge 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)
LEFT JOIN dan RIGHT JOIN Keywords contoh 1 (Access) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Offering LEFT JOIN Faculty ON Offering.FacSSN = Faculty.FacSSN WHERE CourseNo LIKE 'IS*' contoh 2 (Access) FROM Faculty RIGHT JOIN Offering Example 1: - Preserve rows of the table on the left (Offering) - Result includes Offering rows that join with Faculty rows as well as non matching Offering rows Example 2: - Result is identical to Example 1 Outer join keywords: Access syntax as well as SQL:1999 syntax and Oracle 9i Oracle 9i examples are identical except for % wild card character
Notasi Oracle 8i untuk One-Sided Outer Joins contoh 3 (Oracle 8i) SELECT OfferNo, CourseNo, Offering.FacSSN, FacFirstName, FacLastName FROM Faculty, Offering WHERE Offering.FacSSN = Faculty.FacSSN (+) AND CourseNo LIKE 'IS%' contoh 4 (Oracle 8i) WHERE Faculty.FacSSN (+) = Offering.FacSSN Oracle 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 multiple outer 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 notation Example 3 and 4 produce the same results as examples 1 and 2
Contoh I Full Outer Join contoh 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 Example 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
Contoh II Full Outer Join contoh 5 (Access) SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty RIGHT JOIN Student ON Student.StdSSN = Faculty.FacSSN UNION FROM Faculty LEFT JOIN Student Notation: - Access: UNION of two one-sided outer joins; LEFT JOIN, RIGHT JOIN notation - Result is identical to Example 5 (FULL JOIN)
Contoh III Full Outer Join contoh 5 (Oracle 8i) SELECT FacSSN, FacFirstName, FacLastName, FacSalary, StdSSN, StdFirstName, StdLastName, StdGPA FROM Faculty, Student WHERE Student.StdSSN = Faculty.FacSSN (+) UNION WHERE Student.StdSSN (+) = Faculty.FacSSN Notation: - 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
Mixing Inner dan Outer Joins I contoh 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*' Mixing inner and outer joins: - Very common when using one-sided outer join - Access requires parentheses when using more than one join operation in 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 *
Mixing Inner dan Outer Joins II contoh 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%' 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
Type I Nested Queries Query di dalam query Menggunakan WHERE dan HAVING conditions Sama dengan prosedur nested Menjalankan satu kali Tidak ada reference ke outer query Juga dikenal sebagai non-correlated atau independent nested query Nested 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 clause Type I: - Primarily an alternative join style Nested procedure execution model: procedure executes and is replaced by a value - Executes one time and is replaced by a list of values Distinguishing feature: - No reference to outer query - Type I nested query is independent of outer query - Non-correlated or independent nested query
Contoh Type I Nested Query Contoh 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*' ) Contoh 8 (Oracle): List finance faculty who teach 4 unit IS courses. WHERE CourseNo LIKE 'IS%' AND CourseNo IN ( SELECT CourseNo FROM Course WHERE 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 table Example 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
Contoh DELETE Gunakan Type I nested queries untuk menguji kondisi pada tabel lain Gunakan juga UPDATE statements Contoh 9: Delete offerings taught by Leonard Vince. DELETE FROM Offering WHERE Offering.FacSSN IN ( SELECT FacSSN FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' ) Type I nested query good for complex deletions: - Test conditions on other tables - Portable across most DBMSs
Type II Nested Queries Sama dengan nested loops Menjalan sekali saja untuk setiap baris dari outer query Reference ke outer query Dikenal juga sebagai correlated or variably nested query Penggunaan masalah difference adalah bukan joins Type 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
Contoh Type II Nested Query Contoh 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 ) 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 otherwise Difference 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
Alternatif Difference Formulation Contoh 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 ) Example 11: - Nested query is Type I (no reference to outer query) - Type I formulation does not work for complex difference problems such as Example 9.20 which involves a comparison of multiple correlated columns in the Type II nested query
Nested Queries dalam FROM Clause Lebih banyak pengenak baru daripada nested query dalam klausa WHERE dan HAVING Konsisten dalam bahasa designnya Dimanapun tabel terlihat, ekspresi tabel dapat terlihat. Spesifikasinya: Nested aggregates Multiple independent aggregate calculations Nested queries in the WHERE and HAVING clauses were part of original SELECT statement design Nested queries in the FROM clause were introduced in SQL-92 Consistency in language design: consistency Wherever X constants are permitted, X expressions should be permitted FROM clause supports table constants and table expressions (SELECT statements) Specialized uses: not as important as nested queries in the FROM and HAVING clauses Nested aggregates: average of the sum of credit hours or average of the number of students Multiple aggregates: number of students enrolled and the average resources consumed in the course (grouping of different tables)
Contoh Nested FROM Query Contoh 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 Example 12: Needs a nested query in the FROM clause because nested aggregate is computed Nested aggregate: average number of students enrolled per offering Nested query retrieves one row per offering: course number, description, and number of students enrolled The outer query groups the nested query by course number and description so that number of offerings and the average enrollment per offering can be computed A useful query to compare introductory courses containing many sections Must use two queries to formulate if not using a nested query in the FROM clause
Operator Divide Pasangan subset values operator khusus Suppliers yang mensupply semua bagian Faculty yang mengajar setiap kursus IS operator khusus Secara tipikal diterapkan untuk associative tabel yang menyajikan M-N relationships Chapter 2 material: - Present as review if desired - Can skip material when covering chapter 2 and instead cover now Subset 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 difficult Table 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)
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 operator Table structure: - SuppPart: associative table between Part and Supp tables - List suppliers who supply every part Formulation: - 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
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 clause Contoh 13: List the students who belong to all clubs. SELECT StdNo FROM StdClub GROUP BY StdNo HAVING 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 subqueries Example 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
Masalah Typical Division Membandingkan subset yang menarik daripada entire table Menggunakan kondisi yang sama dalam outer dan nested query Contoh 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' ) Example 13: - Interesting subset: social clubs - Same condition in WHERE clause of both queries (outer and nested) - Ensures that sets are comparable
Masalah Advanced Division Jumlah perbedaan nilai daripada baris Faculty yang mengajar paling sedikit satu session dari tawaran kursus yang ada. Duplikasi penggunaan table Menggunakan COUNT(DISTINCT column) Menggunakan stored query atau nested FROM query dalam Access Use COUNT(DISTINCT …) to count unique column values See Section 9.3.3 for detailed explanation Access: - 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
Contoh masalah Advanced Division Contoh 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, HAVING COUNT(DISTINCT CourseNo) = ( SELECT COUNT(DISTINCT CourseNo) FROM Offering WHERE OffTerm = 'FALL' AND OffYear = 2002 AND 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.
Null Value Efek Kondisi yang sederhana Kondisi campuran Pengelompokan dan fungsi agregasi SQL:1999 standard tetapi implementasinya beragam The 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.
Kondisi yang sederhana Kondisi yang sederhana adalah null jika sisi left-hand maupun right-hand adalah null. Pembuangan evaluasi baris untuk false atau null Mempertahankan evaluasi baris yang benar Evaluasi 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.
Compound Conditions Compound 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.
Fungsi Agregat Nilai null tidak diperbolehkan Pengaruhnya dapat terlihat pada COUNT(*) 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).
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.
Ringkasan Masalah sesuai Advanced itu tidak biasa tetapi penting ketika dibutuhkan. Pemahaman outer join, difference, dan division operators Nested 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 problems Skills: - 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 problems Lots 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