Professional Documents
Culture Documents
Chapter 14-2 Subqueries Multiple-Row
Chapter 14-2 Subqueries Multiple-Row
Multiple-Row Subquery
• Uses the multiple-row operators (IN, ANY, and ALL)
2
IN Operator
• Once the inner query executes and returns a result
set
3
IN Operator – Example
• List employees who have a salary equal to the
highest salary of any department
• This requires two queries:
– INNER query to determine the highest salary for each
department
– OUTER query uses the list of salaries from inner query to
determine which employee's have a salary equal to any
of the salaries
4
IN Operator – INNER Subquery
• Return the highest salary for each department
• Returns multiple rows
5
IN Operator – INNER Subquery
6
IN Operator – Outer Query
• Determine which employee's have a salary equal to
any of the salaries in the list
7
IN Operator - Combined
8
> ALL Operator
• Used when the outer query WHERE clause needs to
select the rows that are:
9
> ALL Operator
– > ALL means greater than all the values returned
from the subquery
– That is, greater than highest value
10
> ALL Operator
– > ALL means greater than all the values returned
from the subquery
– That is, greater than highest value
11
> ALL Operator
12
< ALL Operator
• Used when the outer query WHERE clause needs to
select the rows that are:
– Less than ALL (< ALL) – less than the lowest value
13
< ALL Operator
– < ALL means less than all the values returned from
the subquery
– That is, less than the lowest value
14
< ALL Operator
– < ALL means less than all the values returned from
the subquery
– That is, less than the lowest value
15
> ANY Operator
• Used when the outer query WHERE clause needs
to select the rows that are:
16
> ANY Operator
– > ANY means greater than at least one value
returned in the subquery result set
– That is, greater than lowest value
17
> ANY Operator
– > ANY means greater than at least one value
returned in the subquery result set
– That is, greater than lowest value
18
> ANY Operator
19
< ANY Operator
• Used when the outer query WHERE clause needs
to select the rows that are:
– Less than ANY (< ANY) – less than the highest value
20
< ANY Operator
– < ANY means less than at least one value returned
in the subquery result set
– That is, less than the highest value
21
ANY Operator
– < ANY means less than at least one value returned
in the subquery result set
– That is, less than the highest value
22
IN and ANY Operator and NULL Values
• NULL value is returned from a subquery
• IN or ANY operators are used in the outer query
• Outer query returned rows that match the non-null
values
23
IN and ANY Operator and NULL Values
24
IN and ANY Operator and NULL Values
25
ALL operator and NULL Values
• ALL operator is used in an outer query
26
ALL Operator and NULL Values
27
Correlated Subqueries
28
Single-Row and Multiple-Row Subqueries
• The subquery executes once
• The results from the subquery are returned to the
search condition of the outer query
• The outer query is executed based on the value of
the search condition
• The subquery can be executed independently of the
outer query
29
Correlated Subquery
• A subquery that contains a reference to one or
more columns in the outer query
• Executed once for each row processed by the outer
query
30
Correlated Subquery
Example
• List all employees in the EMPLOYEES table that
have a salary greater than the average salary for
their department
31
Correlated Subquery
Example
32
33