More SQL Nested and Union queries, and more

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

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.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Structured Query Language SA0951a: Introduction to SQL Structured Query Language Lots of SQL books in library and ebrary e.g. Head First SQL, SQL for Mere.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Structured Query Language (SQL)
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
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.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
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.
9 Advanced Query Formulation with SQL (Chapter 9).
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
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.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Web Programming MySql JDBC Web Programming.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 11 SQL: Data Manipulation
Using Subqueries to Solve Queries
More SQL: Complex Queries,
Slides are reused by the approval of Jeffrey Ullman’s
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Using the Set Operators
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Using Subqueries to Solve Queries
Basic SQL Lecture 6 Fall
Writing Correlated Subqueries
SQL Structured Query Language 11/9/2018 Introduction to Databases.
David M. Kroenke and David J
Using the Set Operators
Chapter Name SQL: Data Manipulation
CS 3630 Database Design and Implementation
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
SQL: Structured Query Language
Using Subqueries to Solve Queries
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 4 Complex Queries in SQL
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
Presentation transcript:

More SQL Nested and Union queries, and more iSQLplus: http://uadisq01.uad.ac.uk:5560/isqlplus/ More SQL Nested and Union queries, and more For most queries, we use the same sample data as before: Personnel, Branch, Transport tables. Reading: For example: Connolly/Begg (4th Ed) : 5.3.5 / 5.3.6 / 5.3.8 / 5.3.9 Any SQL book!!! November 18

XY XY XY YX X Y X Y X Y X Y SQL2 November 18 Match up the set operations, diagrams and SQL words XY XY XY YX a. b. c. d. i. UNION ii. MINUS iii. INTERSECT iv. EXCEPT Y X X Y X Y Note: EXCEPT and MINUS do the same thing. EXCEPT is standard SQL, MINUS is Oracle dialect. Access has only UNION implemented. X Y

Set operations in SQL Set operations are union (), intersect (), set difference () Oracle SQL has corresponding operators UNION, INTERSECT, MINUS (standard SQL: EXCEPT) use these to “glue together” two SELECT statements with union-compatible results Can combine several using brackets like in maths by default deletes duplicates - use UNION ALL to keep them in November 18

Example Reader Staff Student Library readers {optional, and} Staff Student Library readers each reader can be staff, student, both, or neither Stored in three relations: Reader(readerNo, name, address) contains all readers. staff also have a record in Staff, students in Student Staff(readerNo*, department, email) Student(readerNo*,matricNo, course, email) November 18

Example: INTERSECT Find all readers who are both staff and student Reader(readerNo, name, address) Staff(readerNo, department, email) Student(readerNo,matricNo, course, email) Example: INTERSECT Find all readers who are both staff and student SQL: No semi-colon at end of first SELECT. Why? SELECT ReaderNo FROM Staff INTERSECT SELECT ReaderNo FROM Student; November 18

Example: MINUS SELECT ReaderNo FROM Reader SQL2 November 18 Reader(readerNo, name, address) Staff(readerNo, department, email) Student(readerNo,matricNo, course, email) Example: MINUS Find all readers who are not students SQL: SELECT ReaderNo FROM Reader MINUS SELECT ReaderNo FROM Student; To get names is difficult if not impossible with this query structure. You might try SELECT ReaderNo, name FROM Reader MINUS SELECT ReaderNo FROM Student; ----- but this would not work because not the same columns, so not union-compatible. But student does not have a name field, so could not add it there. Would be much better to use NOT IN instead. How could you change this to get the names as well? November 18

Reminder: Basic SQL SELECT <attributes> FROM <Tables> May include aliases and aggregate functions FROM <Tables> List Tables and may use Join operations WHERE <condition> Use logical expressions and may Join tables GROUP BY <aggregate statistics> HAVING <aggregate condition> ORDER BY <sorting attributes> November 18

Using IS NULL To find missing/empty values use IS NULL To exclude them use IS NOT NULL Useful in LEFT / RIGHT / FULL JOINS to find values that don't have a match SELECT * FROM personnel WHERE bonus IS NULL; SELECT * FROM personnel WHERE bonus IS NOT NULL; Query 2-1.sql Query 2-2.sql Note: NULL is different from 0!!!!! November 18

Reminder Example “Find staff outside London who don't have a company car” SELECT surname, city FROM personnel p LEFT JOIN transport t ON p.snum=t.snum, branch b WHERE city <> 'LONDON' AND t.snum IS NULL AND p.div = b.div; Query 2-3.sql Hints: How many tables are required by the query? Remember to join the tables November 18

Nested Queries Queries can be used within other queries usually in WHERE clause can also be in HAVING clause or used in calculated columns in SELECT expression in WHERE compares value with a query result uses math operators =, >, <, >=, <=, <> and keywords ALL, ANY, IN, NOT IN, EXISTS, NOT EXISTS November 18

Example 1 SELECT Regno, make FROM transport WHERE mileage < SQL2 November 18 Example 1 Find cars whose mileage is less than the Jaguar's. Two steps: Find mileage of the Jaguar Compare other mileages with that one Nested query (subquery) does all of this. Query 2-4.sql SELECT Regno, make FROM transport WHERE mileage < (SELECT mileage FROM transport WHERE make='JAGUAR'); Danger! This only works if the subquery returns a single value! November 18

SQL2 November 18 Example 2 Find staff with lower than average salaries, and show how much lower they are SELECT snum, surname, salary-(SELECT AVG(salary)FROM personnel) AS difference FROM Personnel p WHERE salary < (SELECT AVG(salary) FROM personnel); Query 2-5.sql Note: need the subqueries because aggregate functions are not allowed in WHERE and cannot be mixed with non-aggregates in SELECT November 18

Nested Queries : Operators If sub-query can return more than one result use an operator in the comparison (IN, NOT IN, ANY, ALL, EXISTS, NOT EXISTS) IN compares a value with a set and evaluates to True if the value is in the set and False otherwise set can be a query result or a simple set, e.g. WHERE town IN ('Dundee', 'Perth', 'Aberdeen') NOT IN November 18

Nested Queries: more operators ANY at least one result of the subquery must fulfil the condition ALL all results of the subquery must fulfil the comparison EXISTS returns True if there is at least one tuple in the subquery and False otherwise NOT EXISTS checks for empty sub-query November 18

Nested Examples: IN, ANY Find all staff working in London SELECT snum, surname FROM personnel WHERE div IN (SELECT div FROM branch WHERE city='LONDON'); or WHERE div = ANY Query 2-6.sql Query 2-7.sql November 18

Find staff earning more than those in branch 30 SQL2 November 18 Find staff earning more than those in branch 30 A SELECT * FROM personnel WHERE div <> 30 and salary > (SELECT salary FROM personnel WHERE div=30); B SELECT * FROM personnel WHERE div != 30 and salary > ANY (SELECT salary FROM personnel WHERE div=30); C SELECT * FROM personnel WHERE div <> 30 and salary > ALL (SELECT salary FROM personnel WHERE div=30); A will not run, because the subquery returns more than one value, so > is not a valid comparison operator. B will run, and returns staff not in branch 30 who earn more than someone (anyone) in branch 30; i.e. they earn more than AT LEAST ONE PERSON FROM BRANCH 30. C will run, and returns staff not in branch 30 who earn more than EVERYONE in branch 30; i.e. they earn more than EACH AND EVERY PERSON FROM BRANCH 30. So, I would regard C as correct (and this is script 2-8.sql). However, the question is actually not phrased very precisely, so B could also be regarded as correct. D Both A and C are correct Query 2-8.sql

Nested Examples: ALL SELECT * FROM branch WHERE div <> ALL branches which don’t have a director SELECT * FROM branch WHERE div <> ALL (SELECT div FROM personnel WHERE jobtitle = 'DIRECTOR'); Query 2-9.sql Note: no space between < and > November 18

How did this work? Subquery produces the result Outer Query takes this result and produces a list of branches which are none of these i.e. 20 10 30 40 50 November 18

Remember to specify join criterion EXISTS / NOT EXISTS branches which have no staff SELECT * FROM branch WHERE NOT EXISTS (SELECT * FROM personnel p WHERE p.div = branch.div); Query 2-10.sql Remember to specify join criterion November 18

How did it work? DBMS evaluates sub-query separately for each value of p.div If sub-query result is empty, NOT EXISTS is true, so this branch is in the result of the main query If p.div=branch.div is omitted, sub-query is evaluated for all branches at once, sub-result not empty, so main query returns nothing November 18

Which of these queries also find branches with no staff? SQL2 November 18 Which of these queries also find branches with no staff? A SELECT * FROM branch MINUS SELECT div FROM personnel; Query 2-11a.sql B SELECT b.* FROM personnel p RIGHT JOIN branch b ON p.div=b.div WHERE b.div IS NULL; Query 2-11b.sql C SELECT * FROM branch WHERE div NOT IN (SELECT div FROM personnel); The ideas used for all queries are all fine and are all equivalent, but: A gives error as columns not union-compatible. Would be ok if * is replaced by div. B returns no results as b.div is the wrong column to apply NULL criterion to. Replace b.div IS NULL with p.div IS NULL. C is correct. Query 2-11c.sql D Both B and C are correct but A is not E All queries are correct

Back to the start SELECT ReaderNo FROM Reader SQL2 November 18 Reader(readerNo, name, address) Staff(readerNo, department, email) Student(readerNo,matricNo, course, email) Back to the start Find all readers who are not students SQL: SELECT ReaderNo FROM Reader MINUS SELECT ReaderNo FROM Student; To get names is difficult if not impossible with this query structure. You might try SELECT ReaderNo, name FROM Reader MINUS SELECT ReaderNo FROM Student; ----- but this would not work because not the same columns, so not union-compatible. But student does not have a name field, so could not add it there. Would be much better to use NOT IN instead. So now, write another query that will find the readerno and name of all readers who are not students. November 18

Summary Basic SQL SELECT Advanced SQL SELECT Basic query facilities SELECT FROM WHERE GROUP BY HAVING ORDER BY IS NULL / IS NOT NULL Advanced SQL SELECT Nested Queries ALL, ANY, EXISTS, NOT EXISTS, IN, NOT IN Set operators UNION, INTERSECT, MINUS November 18