USING SUBQUERIES TO
SOLVE QUERIES
Copyright © 2004, Oracle. All rights reserved.
OBJECTIVES
After completing this lesson, you should be
able to do the following:
Defne subqueries
Describe the types of problems that subqueries
can solve
List the types of subqueries
Write single-row and multiple-row subqueries
USING A SUBQUERY
TO SOLVE A PROBLEM
Who has a salary greater than Abel’s?
Main query:
Which employees have salaries greater
than Abel’s salary?
Subquery:
What is Abel’s salary?
SUBQUERY SYNTAX
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
The subquery (inner query) executes once
before the main query (outer query).
The result of the subquery is used by the main
query.
USING A SUBQUERY
SELECT last_name
FROM employees 11000
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
GUIDELINES FOR USING SUBQUERIES
Enclose subqueries in parentheses.
Place subqueries on the right side of the
comparison condition.
The ORDER BY clause in the subquery is not
needed.
Use single-row operators with single-row
subqueries, and use multiple-row operators with
multiple-row subqueries.
TYPES OF SUBQUERIES
Single-row subquery
Main query
returns
Subquery ST_CLERK
Multiple-row subquery
Main query
returns ST_CLERK
Subquery
SA_MAN
SINGLE-ROW SUBQUERIES
Return only one row
Use single-row comparison operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
EXECUTING SINGLE-ROW
SUBQUERIES
SELECT last_name, job_id, salary
FROM employees
ST_CLERK
WHERE job_id =
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary > 2600
(SELECT salary
FROM employees
WHERE employee_id = 143);
USING GROUP FUNCTIONS IN A
SUBQUERY
SELECT last_name, job_id, salary
FROM employees 2500
WHERE salary =
(SELECT MIN(salary)
FROM employees);
THE HAVING CLAUSE WITH
SUBQUERIES
The Oracle server executes subqueries frst.
The Oracle server returns results into the
HAVING clause of the main query.
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id 2500
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
WHAT IS WRONG WITH THIS
STATEMENT?
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
Single-row operator with multiple-row subquery
WILL THIS STATEMENT RETURN
ROWS?
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
no rows selected
Subquery returns no values.
MULTIPLE-ROW SUBQUERIES
Returnmore than one row
Use multiple-row comparison operators
Operator Meaning
IN Equal to any member in the list
ANY Compare value to each value returned by the
subquery
ALL Compare value to every value returned by
the subquery
USING THE ANY OPERATOR
IN MULTIPLE-ROW SUBQUERIES
SELECT employee_id, last_name, job_id, salary
FROM employees 9000, 6000, 4200
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
…
USING THE ALL OPERATOR
IN MULTIPLE-ROW SUBQUERIES
SELECT employee_id, last_name, job_id, salary
FROM employees 9000, 6000, 4200
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
NULL VALUES IN A SUBQUERY
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
no rows selected
SUMMARY
In this lesson, you should have learned how to:
Identifywhen a subquery can help solve a question
Write subqueries when a query is based on unknown
values
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
PRACTICE 6: OVERVIEW
This practice covers the following topics:
Creating subqueries to query values based on
unknown criteria
Using subqueries to fnd out which values exist
in one set of data and not in another