SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
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
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems More SQL Database Design -- More SQL1.
Microsoft Access 2010 Chapter 7 Using SQL.
Midterm 1 Concepts Relational Algebra (DB4) SQL Querying and updating (DB5) Constraints and Triggers (DB11) Unified Modeling Language (DB9) Relational.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
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.
Using Special Operators (LIKE and IN)
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
9 Advanced Query Formulation with SQL (Chapter 9).
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Chapter 12 Subqueries and Merge Statements
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SQL: Sets UNION INTERSECT MINUS D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sets 1.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
MySQL Subquery Source: Dev.MySql.com
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Using Subqueries to Solve Queries
Writing Correlated Subqueries
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Expressions.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Database Systems: Design, Implementation, and Management Tenth Edition
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
MySQL SQL for MySQL (I) Salim Mail :
Presentation transcript:

SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sub queries 1

General notes (1) Sub-query Queries used to produce values for processing within other statements (basically in filter-conditions WHERE or HAVING, and in FROM to join with other tables or as views for sequential query processing); Result of a sub-query are: – Tables may be used in FROM clause or with EXISTS / NOT EXISTS predicates; – Multivalued - the table has a single column; – Single row – may be used in row value constructor (with INSERT statement); – Scalar or single-valued - may be used in comparisons D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 2

General notes (2) -> General Rules The entire sub-query must always be enclosed in parentheses; The ORDER BY clause may not be used in a sub-query; Sub-queries may contain nested sub-queries: SQL evaluates them from inside out; When in doubt - qualify! Explicit qualification of column names is necessary only when referring to a table listed in FROM at a higher level query, but it is always permissible to qualify columns names to clarify the meaning; VISIBILITY: A sub-query may refer only to column names in tables listed in its FROM clause and the FROM clauses in outer (parent) queries. A sub-query may not access tables used only by a child query; D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 3

General notes (3) -> General rules (cont.) When a sub-query is one of the two operands involved in a comparison, the sub-query must be the second one -- it must follow the relational operator. The result of a sub-query can not be directly compared with the result of another sub-query; Sub-queries may not be used as operands in expressions; If you hesitate between using sub-query and join: – when the final result draws information from more than one table, use join; – when the final result involves information from only one of the tables -- use a sub-query (in general it is cheaper than join); – when necessary - combine. D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 4

Fancy Fruits Database D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 5

Single-valued sub-query (1) It may be used with relational operators just as if it is a constant. Example: Search test for equality on the unique attribute (Fancy Fruits DB/MySQL 5.6) SELECT * FROM Orders WHERE Item_id = (SELECT Item_id FROM Stock WHERE descript = ‘KIWI’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 6

Single-valued sub-query (2) Example: Use of aggregate functions (Fancy Fruits DB) SELECT * FROM Vendors WHERE cost < (SELECT avg(cost) FROM Vendors WHERE Item_id = ‘I02’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 7 Oracle 11g MySQL 5.6

Multi-valued sub-query (single column) Use with linking (quantified) predicates ANY, SOME, ALL – SOME and ANY are equivalent, they link a simple relational operator with the sub-query. The relational operator is applied to each row of the result of the sub- query and the logical expression is true if and only if one or more (at least one) rows in the sub-query result satisfy the comparison. – ALL is true if and only if the relevant comparison is true for each and every value in the sub-query result. Be careful when use ALL with relational operator equal! D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 8

Multi-valued sub-query (single column) Multi-valued sub-query (continue) – Use with list/set of values: IN and NOT IN x IN (a,b,c)  x = SOME (a, b, c) x NOT IN (a,b,c)  x <> ALL (a, b, c) – Use with EXISTS and NOT EXISTS  EXISTS is true if and only if there exists at least one row in the sub-query result and the result rows may have more than one column. Tables are used only with EXISTS and NOT EXISTS and are useful only as correlated sub-queries Multiple queries – Multiple sub-queries on the same level: used with compound logical expressions – Nested sub-queries D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 9

Examples (Fancy Fruits DB/MySQL 5.6) Multi-valued (single-column) queries: ANY SELECT DISTINCT Cust_id FROM Orders WHERE quantity > ANY (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); SOME SELECT DISTINCT Cust_id FROM Orders WHERE quantity > SOME (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 10

Examples (Fancy Fruits DB) Multi-valued (single-column) queries: ALL SELECT DISTINCT Cust_id FROM Orders WHERE quantity > ALL (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 11 Oracle 11g MySQL 5.6

Examples (Fancy Fruits DB) Multiple sub-queries -> On the same level SELECT * FROMOrders WHERE Cust_id = (SELECTCust_id FROMCustomers WHEREcust_name = ‘Alice’ ) AND Item_id = (SELECT Item_id FROMStock WHEREdescript = ‘Oranges’ ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 12 Oracle 11g MySQL 5.6

Examples (Fancy Fruits DB) Multiple sub-queries -> Nested SELECT Vendor_id FROM Vendors WHERE Item_id in ( SELECT Item_id FROM Orders WHERE Cust_id = ( SELECT Cust_id FROM Customers WHERE Cust_name = ‘Alice’ ) ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 13 Oracle 11g MySQL 5.6

Correlated sub-queries Some sub-queries are independent of their outer (parent) query: they return the same result any time they are evaluated. The result of other sub-queries, correlated, depends on the row being processed by the outer query: – the sub-query result varies with the row being processed by the outer query; – the sub-query must be re-evaluated for each row of the outer query; – processing time of correlated sub-queries is theoretically equal of joins, but practically much higher (when join combines tables according to fields, which are indexed). D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 14

Examples (Fancy Fruits DB/MySQL 5.6) Correlated sub-queries (1) SELECT * FROM Orders C1 WHERE C1.quantity > (SELECT AVG(quantity) FROM Orders C2 WHERE C2.Cust_id = C1.Cust_id ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 15 C1.Cust-id == ‘AAA’ Subquery: SELECT AVG(Quantity)produces:( )/ 4.0 = 42.5 FROM Orders C210 > 42.5  FALSE WHERE C2.Cust_id = ‘AAA’the first row is not included into result C1.Cust-id == ‘BBB’ Subquery: SELECT AVG(Quantity)produces:( ) / 2.0 = 35 FROM Orders C220 > 35  FALSE WHERE C2.Cust_id = ‘BBB’the second row is not included into result

Examples (Fancy Fruits DB/MySQL 5.6) Correlated sub-queries (2) SELECT * FROM Orders C1 WHERE C1.quantity < (SELECT AVG(quantity) FROM Orders C2 WHERE C2.Cust_id = C1.Cust_id); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 16

Examples (Fancy Fruits DB) Correlated sub-queries (3) SELECT cust_name FROM Customers WHERE 100 < (SELECT SUM(quantity) FROM Orders WHERE Orders.Cust_id = Customers.Cust_id ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 17

Examples (Fancy Fruits DB) Correlated sub-queries (4) SELECT Item_id FROM Orders GROUP BY Item_id HAVING sum(quantity) > (SELECT 0.1*on_hand FROM Stock WHERE Orders.Item_id = Stock.Item_id); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 18