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

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
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,
Chapter 8 Normalization. © 2001 The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill/Irwin Outline Modification anomalies Functional dependencies.
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.
Concepts of Database Management Sixth Edition
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Supplemental Slides Type II Nested Query Demonstration.
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
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data 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 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.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
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.
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.
1 Welcome to the World of Databases! Database technology: crucial to the operation and management of modern organisations Major transformation in computing.
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.
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.
Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.
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
Developing Data Models – Conversion Rules
Query Formulation with SQL
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CSC 453 Database Systems Lecture
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
Presentation transcript:

Advanced Query Formulation with SQL

9-2 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

9-3 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-4 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

9-5 Full Outer Join Example

9-6 University Database

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

9-8 Full Outer Join Example II Example 4 (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

9-9 Mixing Inner and Outer Joins I Example 5 (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*'

9-10 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

9-11 Type I Nested Query Examples I Example 6 : 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*' )

9-12 Type I Nested Query Examples II Example 7 : 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 ) )

9-13 Type I Nested Query Examples III Example 7a : List students who are enrolled in offering 1234 SELECT StdLastName, StdFirstName FROM Student WHERE StdSSN IN ( SELECT StdSSN FROM Enrollment WHERE OfferNo = 1234 )

9-14 Type I Nested Query Examples IV Example 7b : List students who are enrolled in a class other than offering 1234 SELECT StdLastName, StdFirstName FROM Student WHERE StdSSN IN ( SELECT StdSSN FROM Enrollment WHERE OfferNo <> 1234 )

9-15 Type I Nested Query Examples V Example 7b : List students who are not enrolled in offering 1234 SELECT StdLastName, StdFirstName FROM Student WHERE StdSSN NOT IN ( SELECT StdSSN FROM Enrollment WHERE OfferNo = 1234 )

9-16 Type I Nested Query Examples VI Example 7c : List faculty member with highest salary SELECT facfirstname, faclastname from faculty where facsalary = (select max(facsalary) from faculty)

9-17 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.FacSSN IN ( SELECT FacSSN FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' )

9-18 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

9-19 Type II Nested Query Example for a Difference Problem Example 9: Retrieve MS faculty who are not teaching in winter SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'MS' AND NOT EXISTS ( SELECT * FROM Offering WHERE OffTerm = 'WINTER' AND OffYear = 2006 AND Faculty.FacSSN = Offering.FacSSN )

9-20 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

9-21 Type I Difference Formulation Example 10: Retrieve MS faculty who are not teaching in winter SELECT FacSSN, FacLastName, FacDept FROM Faculty WHERE FacDept = 'MS' AND FacSSN NOT IN ( SELECT FacSSN FROM Offering WHERE OffTerm = 'WINTER' AND OffYear = 2006 )

9-22 One-Sided Outer Join Difference Formulation Example 11: Retrieve courses that have never been taught. SELECT * FROM course left join offering ON course.courseno = offering.courseno WHERE offering.courseno is null

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

9-24 Nested FROM Query Example Example 13: 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

9-25 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

9-26 Division Example

9-27 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 )

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

9-29 Null Value Effects  Simple conditions  Compound conditions  Grouping and aggregate functions  SQL:2003 standard but implementation may vary

9-30 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

9-31 Compound Conditions

9-32 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)

9-33 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

9-34 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