Chapter 12 Subqueries and Merge Statements

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
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.
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.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
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.
Introduction to Structured Query Language (SQL)
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.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
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 9 Joining Data from Multiple Tables
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.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Using Special Operators (LIKE and IN)
Subqueries.
Subqueries.
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.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Using Subqueries to Solve Queries
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries.
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
Presentation transcript:

Chapter 12 Subqueries and Merge Statements Oracle11g: SQL Chapter 12 Subqueries and Merge Statements

Objectives Determine when using a subquery is appropriate 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 Oracle 11g: SQL 2

Objectives (continued) 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 Oracle 11g: SQL

Objectives (continued) 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 Use a subquery in a DML action Process multiple DML actions with a MERGE statement Oracle 11g: SQL

Subqueries and Their Uses Subquery – a query nested inside another query Used when a query is based on an unknown value Requires SELECT and FROM clauses Must be enclosed in parentheses Place on right side of comparison operator Oracle 11g: SQL 5

Types of Subqueries Oracle 11g: SQL 6

Single-Row Subqueries Can only return one result to the outer query Operators include =, >, <, >=, <=, < > Oracle 11g: SQL

Single-Row Subquery in a WHERE Clause Used for comparison against individual data Oracle 11g: SQL

Single-Row Subquery in a HAVING Clause Required when returned value is compared to grouped data Oracle 11g: SQL

Single-Row Subquery in a SELECT Clause Replicates subquery value for each row displayed Oracle 11g: SQL

Multiple-Row Subqueries Return more than one row of results Require use of IN, ANY, ALL, or EXISTS operators Oracle 11g: SQL

ANY and ALL Operators Combine with arithmetic operators Oracle 11g: SQL

Multiple-Row Subquery in a WHERE Clause Note: Could use IN operator or =ANY Oracle 11g: SQL 13

Multiple-Row Subquery in a WHERE Clause (continued) Oracle 11g: SQL 14

Multiple-Row Subquery in a HAVING Clause Oracle 11g: SQL 15

Multiple-Column Subqueries Return more than one column in results Can return more than one row Column list on the left side of operator must be in parentheses Use the IN operator for WHERE and HAVING clauses Oracle 11g: SQL 16

Multiple-Column Subquery in a FROM Clause Creates a temporary table Oracle 11g: SQL

Multiple-Column Subquery in a WHERE Clause Returns multiple columns for evaluation Oracle 11g: SQL

NULL Values When a subquery might return NULL values, use NVL function Oracle 11g: SQL

Uncorrelated Subqueries Processing sequence Inner query is executed first Result is passed to outer query Outer query is executed Oracle 11g: SQL 20

Correlated Subqueries Inner query is executed once for each row processed by the outer query Inner query references the row contained in the outer query Oracle 11g: SQL 21

Correlated Subqueries (continued) Oracle 11g: SQL 22

Nested Subqueries Maximum of 255 subqueries if nested in the WHERE clause No limit if nested in the FROM clause Innermost subquery is resolved first, then the next level, etc. Oracle 11g: SQL 23

Nested Subqueries (continued) Innermost is resolved first (A), then the second level (B), then the outer query (C) Oracle 11g: SQL

Subquery in a DML action Oracle 11g: SQL

MERGE Statement With a MERGE statement, a series of DML actions can occur with a single SQL statement Conditionally updates one data source based on another Oracle 11g: SQL 26

MERGE Statement (continued) Oracle 11g: SQL 27

MERGE Statement (continued) The following explains each part of the previous MERGE statement: MERGE INTO books_1 a: The BOOKS_1 table is to be changed and a table alias of “a” is assigned to this table USING books_2 b: The BOOKS_2 table will provide the data to update and/or insert into BOOKS_1 and a table alias of “b” is assigned to this table ON (a.isbn = b.isbn): The rows of the two tables will be joined or matched based on isbn WHEN MATCHED THEN: If a row match based on ISBN is discovered, execute the UPDATE action in this clause. The UPDATE action instructs the system to modify only two columns (Retail and Category) WHEN NOT MATCHED THEN: If no match is found based on the ISBN (a books exists in BOOKS_2 that is not in BOOKS_1), then perform the INSERT action in this clause Oracle 11g: SQL 28

MERGE with WHERE conditions Oracle 11g: SQL 29

MERGE with DELETE Oracle 11g: SQL 30

Summary A subquery is a complete query nested in the SELECT, FROM, HAVING, or WHERE clause of another query The subquery must be enclosed in parentheses and have a SELECT and a FROM clause, at a minimum Subqueries are completed first; the result of the subquery is used as input for the outer query A single-row subquery can return a maximum of one value Single-row operators include =, >, <, >=, <=, and <> Multiple-row subqueries return more than one row of results Oracle 11g: SQL 31

Summary (continued) Operators that can be used with multiple-row subqueries include IN, ALL, ANY, and EXISTS Multiple-column subqueries return more than one column to the outer query NULL values returned by a multiple-row or multiple-column subquery will not present a problem if the IN or =ANY operator is used Correlated subqueries reference a column contained in the outer query Subqueries can be nested to a maximum depth of 255 subqueries in the WHERE clause of the parent query Oracle 11g: SQL 32

Summary (continued) With nested subqueries, the innermost subquery is executed first, then the next highest level subquery is executed, and so on, until the outermost query is reached A MERGE statement allows multiple DML actions to be conditionally performed while comparing data of two tables Oracle 11g: SQL