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

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Query Formulation with SQL
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Advanced Query Formulation with SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
7-1 Normalization - Outline  Modification anomalies  Functional dependencies  Major normal forms  Practical concerns.
Database Systems More SQL Database Design -- More SQL1.
Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 3 The Relational Data Model.
Advanced Query Formulation with SQL Week 10 Quiz Jaymond Huynh Tim Nguyen.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Chapter 7 Normalization. Outline Modification anomalies Functional dependencies Major normal forms Relationship independence Practical concerns.
Chapter 7 Normalization. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Modification anomalies Functional dependencies.
Copyright © 2011 by Michael V. Mannino All rights reserved. Database Design, Application Development, and Administration, 5 th Edition Chapter 3 The Relational.
Chapter 2 The Relational Data Model. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Relational model basics Integrity.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Chapter 10 Application Development with Views. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Creating.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 3 Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Getting started.
Chapter 4 Multiple-Table Queries
Advanced Query Formulation with SQL. 9-2 Outline  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Nested.
Chapter 9 Advanced Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Outer join problems.
9 Advanced Query Formulation with SQL (Chapter 9).
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Query Formulation with SQL.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
Advanced Relational Algebra & SQL (Part1 )
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 10 Application Development with Views.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 3 Query Formulation with SQL. Outline Background Getting started Joining tables Summarizing tables Reasoning tools Advanced problems Data manipulation.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 2 The Relational Data Model. Outline Relational model basics Integrity rules Rules about referenced rows Relational Algebra.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Application Development with Views
The Relational Data Model
Database Systems: Design, Implementation, and Management Tenth Edition
Using Subqueries to Solve Queries
Query Formulation with SQL
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter Name SQL: Data Manipulation
CSC 453 Database Systems Lecture
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Manipulating Data Lesson 3.
Presentation transcript:

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

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

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

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

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

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

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%'

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

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

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

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

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

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

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

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

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

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' )

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

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 )

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

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

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

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 )

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

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

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

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

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 )

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' )

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

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%' )

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

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

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

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)

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

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