Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Multiple-Row Subqueries

Multiple-Row Subquery
• Uses the multiple-row operators (IN, ANY, and ALL)

• Returns one or more rows to the outer query

2
IN Operator
• Once the inner query executes and returns a result
set

• The IN operator is used by the outer query to


determine whether the current row of the outer
query is found in the list returned by the inner
query

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:

– Greater than ALL (> ALL) – greater than the highest


value

– Greater than or equal to ALL (>= ALL) – greater than or


equal to the highest value

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

– Less than or equal to ALL (<= ALL) – less than or equal


to 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:

– Greater than ANY (> ANY) – greater than the lowest


value

– Greater than or equal to ANY (>= ANY) – greater than


or equal to the lowest value

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

– Less than or equal to ANY (<= ANY) – less than or equal


to 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

• Example, return manager names from the


employee's table

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

• The inner query returns a NULL value

• The outer query returns no rows because ALL


compares the outer query row with every value
returned by the subquery, including the NULL value

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

You might also like