Chapter 14-1 Subqueries Single Row
Chapter 14-1 Subqueries Single Row
Subquery
• There are many times when a query requires more
than one step to obtain the required results
• For example, if a request is made to list employees
with a salary higher than the average salary in the
company, two queries need to run where the
second query depends on the result of the first
query
2
Subquery
• A SELECT statement
inserted into a clause of
another SELECT statement
3
Subquery
• The query inserted into
another query is called the
inner query and is
executed first
• The query containing the
inner query is called the
outer query and is
executed after receiving
the result set from the
inner query
4
Subquery
• Used when a query is
based on an unknown
value at execution time
and the subquery
determines that value
5
Types of Subqueries
• Single-row subquery
– Returns single row (single value) to the outer query
• Multiple-row subquery
– Returns one or more rows to the outer query
• Correlated subquery
– Contains a reference to one or more columns in the
outer query
6
Single-Row (single-value) Subquery
• Uses single-row operators (>, =, >=, <, <=, <>)
• Returns only one row (one value) from the inner
query
– Referred to as single-value subquery
– Used when the result set of the outer query is
determined on a single value from the inner query
• Can be executed and tested independently of the
outer query
7
Single-Row Subquery
• List employees from the EMPLOYEES table where
salary is greater than the average salary for all
employees?
• This requires two queries:
– Determine the average salary for all employees – INNER
Query
– Determine which employees have an above-average
salary – OUTER Query
8
Single-Row Subquery
INNER Query
– Determine the average salary for all employees
– No need to format the value from the subquery as the
result set is used by the outer query
9
Single-Row Subquery
OUTER Query
• Determine which employees have an above-
average salary
10
Single-Row Subquery
OUTER Query
11
Single-Row Subquery
Combined
12
Subquery Syntax
• A subquery:
– Enclosed within brackets or parentheses
– Begins with the SELECT keyword
– Followed by:
A list of columns or expressions
A FROM clause
Optional WHERE, GROUP BY, and HAVING clauses
13
Subquery Syntax
• A subquery:
– Does not allow an ORDER BY clause or a UNION operator
– Only one ORDER BY clause can be used for a SELECT
statement; and, if used, it must be the last clause in the
outer query
– The subquery is placed on the right side of the
comparison condition
14
Single-Row Subquery
• The outer query:
15
Subquery Returns NULL Value or No Rows
• If a subquery returns a NULL value or no rows, the
outer query takes the result of the subquery (NULL)
and uses this result in its WHERE clause
16
Subquery Returns NULL Value or No Rows
• Next slide: Who works in the same department as
employee Best?
17
Subquery Returns NULL Value
18
Outer and Inner Queries can Access
Data from Different Tables
• The outer and inner queries can access data from
different tables
19
Outer and Inner Queries can Access
Data from Different Tables
20
Group Functions in Subqueries
• Group functions can be used in a subquery
• A group function without a GROUP BY clause in the
subquery returns a single row
21
Group Functions
in Subqueries
22
Updating Data with a Value
from a Subquery
• An UPDATE statement can modify data in a table
using the result of a single-row subquery
23
Updating Data with a Value
from a Subquery
24
25