0% found this document useful (0 votes)
64 views7 pages

At A Glance: Subqueries and MERGE Statements

This document provides an overview of subqueries and MERGE statements in Oracle 12c. It describes the different types of subqueries including single-row, multiple-row, and multiple-column subqueries. It explains how to use each type of subquery in various clauses and how to identify correlated vs uncorrelated subqueries. The document also introduces MERGE statements and describes how to nest multiple subqueries. Key learning objectives and troubleshooting tips are provided throughout.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views7 pages

At A Glance: Subqueries and MERGE Statements

This document provides an overview of subqueries and MERGE statements in Oracle 12c. It describes the different types of subqueries including single-row, multiple-row, and multiple-column subqueries. It explains how to use each type of subquery in various clauses and how to identify correlated vs uncorrelated subqueries. The document also introduces MERGE statements and describes how to nest multiple subqueries. Key learning objectives and troubleshooting tips are provided throughout.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Oracle 12c: SQL 12-1

Chapter 12
Subqueries and MERGE Statements

At a Glance
Instructor’s Notes
♦ Chapter Overview

♦ Chapter Objectives

♦ Instructor Notes

♦ Troubleshooting Tips

♦ Quick Quizzes

♦ Discussion Questions

♦ Key Terms
Oracle 12c: SQL 12-2

Chapter Overview
There are numerous occasions when a query will be based on some unknown value that is
already contained in the database. One option is to first look up the unknown value and then
issue the query. The alternative is to create a subquery—one query nested inside another query.
Subqueries are widely used in application development. This chapter addresses single-row,
multiple-row, and multiple-column subqueries. In addition, the rationales for placing subqueries
in various clauses of the outer query are presented. The use of subqueries is demonstrated in
subsequent chapters for creating tables, views, and so on. The last topic in the chapter introduces
the MERGE statement, which enables processing a group of DML actions with one statement.
This was not presented in the earlier DML chapter since it involves more complex query
construction.

Chapter Objectives
After completing this chapter, you should be able to do the following:

♦ 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
♦ 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
♦ 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
♦ Process multiple DML actions with a MERGE statement

Instructor Notes

Subqueries and Their Uses


A subquery is necessary when a search is based on an unknown value that is contained within a
database table. There are three basic types of subqueries: single-row, multiple-row, and multiple-
column. Multiple-row and multiple-column subqueries can be nested in FROM, WHERE, and
HAVING clauses, while a single-row subquery can be nested in SELECT, FROM, WHERE, and
HAVING clauses. When a subquery is used in a FROM clause, it creates a temporary table that
Oracle 12c: SQL 12-3

can be referenced by other clauses of the same SELECT statement. A subquery in a FROM
clause is usually referred to as an inline view.

If a subquery is being used to determine the value to be used in a comparison, it must appear on
the right side of the comparison operator. In addition, a subquery must be enclosed in a set of
parentheses to separate it from the outer query. A subquery cannot contain an ORDER BY
clause; any sorting should be performed in the outer query.

Single-Row Subqueries
A single-row subquery can only return a single data element. A single-row subquery is specified
by the use of a single-row operator. Valid single row operators include any of the mathematical
comparison operators. A single-row subquery is specified in the WHERE clause when the
comparison is not based on a group condition. If the subquery results are used for comparison
against grouped data, the subquery must be nested in a HAVING clause.

Troubleshooting Tip Demonstrate the use of a subquery in a WHERE clause, and then
use the same subquery in a HAVING clause and discuss the error
message returned.

Quick Quiz
1. How many columns can be returned by a single-row subquery?
ANSWER: One

2. A single-row subquery can be used in which clauses of the outer query?


ANSWER: SELECT, FROM, WHERE, and HAVING clauses

3. What type of comparison operators can be used with a single-row subquery?


ANSWER: Any comparison operator (except ANY and ALL operators); normally uses
mathematical comparison operators

4. Can a GROUP BY be included in a single-row subquery?


ANSWER: Yes

5. When should a single-row operator be included in a HAVING clause of the outer query?
ANSWER: When the result will be compared against a group function or grouped data

Multiple-Row Subqueries
A multiple-row subquery can return more than one row of results but only one column of output.
The most commonly used multiple-row operator is the IN comparison operator. In addition, the
Oracle 12c: SQL 12-4

ANY and ALL operators can be used with various mathematical operators to specify how the
results of the subquery should be evaluated. Note how the MIN and MAX operators can be used
to accomplish similar tasks that the ANY and ALL operators are used to resolve.

Troubleshooting Tip Create a multiple-row subquery and then re-execute using


variations of the ANY and ALL operators and discuss the results.

Quick Quiz
1. How many columns can be returned by a single-row subquery?
ANSWER: One

2. A multiple-row subquery is most commonly used in which clauses of the outer query?
ANSWER: WHERE and HAVING clauses

3. What type of comparison operators can be used with a multiple-row subquery?


ANSWER: The IN comparison operator and the ANY and ALL operators combined with
mathematical operators

4. If the “greater than” comparison operator is used with a multiple-row subquery, what
type of result will be returned?
ANSWER: An error message

5. Which of the variations of the ANY operator is the same as using the IN comparison
operator?
ANSWER: =ANY
Oracle 12c: SQL 12-5

Multiple-Column Subqueries
A multiple-column subquery is a subquery that can return several columns, as well as several
rows, in its results. When a multiple-column subquery is used in a FROM clause, it is referred to
an inline view. When using a multiple-column subquery for comparison purposes, the column
list specified in the subquery must be in the same order as the column list specified in the
WHERE or HAVING clause. The column list included in the WHERE or HAVING clause must
be enclosed in parentheses.

Quick Quiz
1. Multiple-column subqueries can be used in which clauses of the outer query?
ANSWER: FROM, WHERE, or HAVING clauses

2. What is an inline view?


ANSWER: A temporary table created by using a multiple-column subquery in the FROM
clause of the outer query

3. Which operator is used when using the results of a multiple-column subquery for
comparison in the outer query?
ANSWER: IN

4. The column listed provided in a WHERE clause that is used for comparison to a
multiple-column subquery must be enclosed in ________.
ANSWER: parentheses

5. How do you reference columns returned through an inline view?


ANSWER: Specify a table alias for the subquery

NULL Values
Because NULL values cannot be used for comparison purposes, a NULL value returned by a
subquery will not match to any values in the outer query. If it is possible that NULL values may
generate erroneous results from the outer query, the NVL function should be used in the
subquery to provide a substitute for the NULL value.

A correlated subquery references a column contained in the outer query. During processing, the
outer query is executed, and then the inner query is executed once for each row retrieved by the
outer query. With an uncorrelated subquery, the inner query is executed first, and the value is
returned to the outer query for processing.
Oracle 12c: SQL 12-6

Quick Quiz
1. What is the result if a subquery returns a NULL value to the outer query?
ANSWER: No rows will be returned by the outer query.

2. If a query must be based on whether a NULL value exists, what function can be included
in the inner and outer queries to ensure the correct processing will occur?
ANSWER: NVL function

3. How is an uncorrelated subquery processed?


ANSWER: The inner query is executed, first and the result is returned to the outer query,
which is then executed.

4. How is a correlated subquery processed?


ANSWER: The outer query is executed first; the inner query is executed once for every
row processed by the outer query.

5. How can you identify a correlated subquery?


ANSWER: The subquery will reference a column in the outer query.

Nested Subqueries
Subqueries can be nested in a WHERE or HAVING clause to a maximum depth of 255. There is
no depth limit when the subqueries are nested in a FROM clause. In all cases, the innermost
query is executed first, and the result is passed to the next level query. Each subquery must be
complete with the minimum of a SELECT clause and a FROM clause for each query. The
WITH clause is an alternative to using subqueries that offers potential improvements in
statement readability and processing efficiency.

Troubleshooting Tip Demonstrate how triple nested subqueries work by first executing
the individual subqueries, and then nest the subqueries and execute
the final version.

Quick Quiz
1. Subqueries in a WHERE clause can be nested to a maximum depth of ________.
ANSWER: 255

2. When subqueries are nested to a depth of four, which subquery will be executed first?
ANSWER: Innermost subquery
Oracle 12c: SQL 12-7

3. What is the maximum depth for nesting subqueries in a FROM clause?


ANSWER: There is no limit.

Discussion Questions
1. Identify a scenario in which a user is forced to use a subquery.

2. What are the alternatives to using a subquery? Joins?

Key Terms
correlated subquery — A subquery that references a column in the outer query. The outer
query executes the subquery once for every row in the outer query.

multiple-column subquery — A nested query that returns more than one column of results to
the outer query. It can be listed in the FROM, WHERE, or HAVING clause.

multiple-row subquery — Nested queries that return more than one row of results to the parent
query. They are most commonly used in WHERE and HAVING clauses and require multiple-
row operators.

single value — The output of a single-row subquery.

single-row subquery — A nested subquery that can return to the outer query only one row of
results that consists of only one column. The output of a single-row subquery is a single value.

uncorrelated subquery — A subquery that follows this method of processing: the subquery is
executed, then the results of the subquery are passed to the outer query, and finally the outer
query is executed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy