9 Advanced Query Formulation with SQL (Chapter 9).

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

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.
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.
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.
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 3 The Relational Data Model.
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.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
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.
Chapter 12 Subqueries and Merge Statements
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 Five Multiple-Table Queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
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
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
Query Formulation with SQL
SQL Structured Query Language 11/9/2018 Introduction to Databases.
More SQL Nested and Union queries, and more
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Chapter Name SQL: Data Manipulation
JOINS (Joinining multiple tables)
CSC 453 Database Systems Lecture
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
JOINS (Joinining multiple tables)
Presentation transcript:

9 Advanced Query Formulation with SQL (Chapter 9)

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

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 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 Effect of Null Value on Compound Conditions

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 Column1Column Effect of Null Value on Aggregate Functions

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 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 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 Full Outer Join Example

11 University Database (Relationship Window)

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 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 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 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 Mixing Inner and One-Sided Outer Join Example 5 Combine columns from the Faculty, Offering, and Course tables for Information Systems courses offered in 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 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 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 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 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 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 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 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 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 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 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 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 Table Example for Division Problem StdNoClubNo S1C1 S1C2 S1C3 S1C4 S2C1 S2C4 S3C3 ClubNoCNameCPurposeCBudgetCActual C1DELTASOCIAL$1,000.00$1, C2BITSACADEMIC$500.00$ C3HELPSSERVICE$300.00$ C4SIGMASOCIAL$ Table 9-12: StdClub Table Listing Table 9-11: Club Table Listing

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