At A Glance: Subqueries and MERGE Statements
At A Glance: Subqueries and MERGE Statements
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:
Instructor Notes
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
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.
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
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
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
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
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
Discussion Questions
1. Identify a scenario in which a user is forced to use a subquery.
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-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.