Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 12 Subqueries and Merge Statements (up to p.451) Jason C. H. Chen, Ph.D. Professor of MIS School of.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Chapter 11 Group Functions
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
6-1 Copyright  Oracle Corporation, All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Database Systems More SQL Database Design -- More SQL1.
Advanced SQL SMSU Computer Services Short Course.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Subqueries.
Subqueries.
Copyright © 2004, Oracle. All rights reserved. U SING S UBQUERIES TO S OLVE Q UERIES.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
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
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Subqueries.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
More SQL: Complex Queries,
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
SQL Subquery.
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Management System
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Chapter 7 Subqueries

Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish between an outer query and a subquery  Use a single-row subquery in a WHERE clause  Use a single-row subquery in a HAVING clause  Use a single-row subquery in a SELECT clause  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish between an outer query and a subquery  Use a single-row subquery in a WHERE clause  Use a single-row subquery in a HAVING clause  Use a single-row subquery in a SELECT clause

Chapter Objectives  Distinguish between single-row and multiple-row comparison operators  Use a multiple-row subquery in a WHERE clause  Use a multiple-row subquery in a HAVING clause  Use a multiple-column subquery in a WHERE clause  Distinguish between single-row and multiple-row comparison operators  Use a multiple-row subquery in a WHERE clause  Use a multiple-row subquery in a HAVING clause  Use a multiple-column subquery in a WHERE clause

Chapter Objectives  Create an inline view using a multiple- column subquery in a FROM clause  Compensate for NULL values in subqueries  Distinguish between correlated and uncorrelated subqueries  Nest a subquery inside another subquery  Create an inline view using a multiple- column subquery in a FROM clause  Compensate for NULL values in subqueries  Distinguish between correlated and uncorrelated subqueries  Nest a subquery inside another subquery

Subquery  Used when query is based on unknown value  A query nested inside another query  Requires SELECT and FROM clauses  Must be enclosed in parentheses  Place on right side of comparison operator  Used when query is based on unknown value  A query nested inside another query  Requires SELECT and FROM clauses  Must be enclosed in parentheses  Place on right side of comparison operator

Types of Subqueries

Single-Row Subquery Operators  Can only return one result to outer query  Operators include =, >, =,  Can only return one result to outer query  Operators include =, >, =,

Single-Row Subquery – In WHERE Clause Used for comparison against individual data

Single-Row Subquery – In HAVING Clause Required when returned value is compared to grouped data

Single-Row Subquery – In SELECT Clause Replicates subquery value for each row displayed

Multiple - Row Subqueries  Return more than one row of results  Require use of IN, ANY, ALL, or EXISTS operators  Return more than one row of results  Require use of IN, ANY, ALL, or EXISTS operators

ANY and ALL Operators Combine with arithmetic operators

EXISTS Operator Determines whether condition exists in subquery

Multiple-Row Subquery – In WHERE Clause

Multiple-Row Subquery – In HAVING Clause

Multiple-Column Subquery  Returns more than one column in results  Can return more than one row  Column list on left side of operator must be in parentheses  Uses IN operator for WHERE and HAVING clauses  Returns more than one column in results  Can return more than one row  Column list on left side of operator must be in parentheses  Uses IN operator for WHERE and HAVING clauses

Multiple-Column Subquery – In FROM Clause Creates temporary table

Multiple-Column Subquery – In WHERE Clause Returns multiple columns for evaluation

NULL Values When subquery might return NULL values, use NVL function

Uncorrelated Subqueries  Processing sequence: –Inner query executed first –Result passed to outer query –Outer query executed  Processing sequence: –Inner query executed first –Result passed to outer query –Outer query executed

Correlated Subqueries  Inner query executed once for each row processed by outer query  Inner query references row contained in outer query  Inner query executed once for each row processed by outer query  Inner query references row contained in outer query

Nested Subqueries  Maximum 255 subqueries if nested in WHERE clause  No limit if nested in FROM clause  Innermost subquery resolved first, then next level, etc.  Maximum 255 subqueries if nested in WHERE clause  No limit if nested in FROM clause  Innermost subquery resolved first, then next level, etc.

Nested Subquery Example Innermost resolved first (3), then second level (2), then outer query (1)