Database Reference
In-Depth Information
12
Subqueries
In this chapter:
What is a subquery?
What are the types of subqueries?
Where can subqueries be used?
Why do we need subqueries?
Subqueries are probably one of the more complex aspects of SQL. Sub-
queries are often used to resolve complexity by breaking down large queries
into many smaller queries, which interact with each other. In some situa-
tions, subqueries can also be used to improve SQL statement performance.
Let's begin by looking at types of subqueries.
12.1
Types of Subqueries
I like to think of subqueries such that there are two methods of categorizing
subqueries. The first method of categorization is that a subquery can be sca-
lar, correlated, nested, or an inline view. We will get to some of these types of
subqueries later on. I am going to define subquery types based on the second
method of categorization, including some of the first. I find the second
method clearer. Following is my second method of categorization. Note that
the different types of subqueries as defined here can be combinations of sev-
eral types. At the end of this chapter you should understand why.
A subquery is an SQL statement called from another query or another
subquery. Subqueries can return various result sets and can be defined based
on what they return.
Search WWH ::




Custom Search