SUBQUERIES
OBJECTIVES
After completing this lesson, you should be able to
do the following:
◼ Describe the types of problem that subqueries can solve
◼ Define subqueries
◼ 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?
Subquer
y
?
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.
◼ The result of the subquery is used by the main query (outer
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 unless
you are performing Top-N analysis.
◼ 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
Subquer ST_CLERK
y
• Multiple-row subquery
Main
query returns ST_CLERK
Subquer
y 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
WHERE job_id = ST_CLERK
(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
WHERE salary = 2500
(SELECT MIN(salary)
FROM employees);
THE HAVING CLAUSE WITH SUBQUERIES
◼ The Oracle server executes subqueries first.
◼ The Oracle server returns results into the HAVING clause of
the main query.
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > 2500
(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
ERROR at
at line
line 4:
4:
ORA-01427:
ORA-01427: single-row
single-row subquery
subquery returns
returns more
more than
than
one
one row
row
Single-row
Single-row operator
operator with
with multiple-row
multiple-row subquery
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
no rows
rows selected
selected
Subqueryy returns
Subquer no values
returns no values
MULTIPLE-ROW SUBQUERIES
◼ Return more 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
Compare value to every value returned by
ALL
the subquery
USING THE ANY OPERATOR
IN MULTIPLE-ROW SUBQUERIES
SELECT employee_id, last_name, job_id,
salary
FROM employees 9000, 6000,
WHERE salary < ANY 4200
(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
EXCERCISE
◼Write a query to display the last name and
hire date of any employee in the same
department as Zlotkey.
ANSWER
SELECT last_name, hire_date
FROM employees
WHERE department_id = (SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey')
AND last_name <> 'Zlotkey';
QN2
◼Create a query to display the employee
numbers and last names of all
employees who earn more than the
average salary. Sort the results in
ascending order of salary.
ANSWER
SELECT employee_id, last_name
FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees)
ORDER BY salary;
QN3
◼Write a query that displays the employee
numbers and last names of all employees
who work in a
department with any employee whose
last name contains a u.
ANS
SELECT employee_id, last_name
FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%');
QN4
◼ Display the department number, last name, and job ID for
every employee in the Executive department.