Subqueries -Oracle DataBase

salmanmemon77964 2,181 views 19 slides Jan 01, 2016
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

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
http://phpexecutor.com


Slide Content

6
Copyright © Oracle Corporation, 2001. All rights reserved.
Subqueries

6-2 Copyright © Oracle Corporation, 2001. All rights reserved.
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

6-3 Copyright © Oracle Corporation, 2001. All rights reserved.
Using a Subquery
to Solve a Problem
Who has a salary greater than Abel’s?
Which employees have salaries greater
than Abel’s salary?
Main Query:
??
What is Abel’s salary?
??
Subquery

6-4 Copyright © Oracle Corporation, 2001. All rights reserved.
Subquery Syntax
•The subquery (inner query) executes once before
the main query.
•The result of the subquery is used by the main
query (outer query).
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);

6-5 Copyright © Oracle Corporation, 2001. All rights reserved.
SELECT last_name
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
Using a Subquery
11000

6-6 Copyright © Oracle Corporation, 2001. All rights reserved.
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.

6-7 Copyright © Oracle Corporation, 2001. All rights reserved.
Types of Subqueries
Main query
Subquery
returns
ST_CLERK
•Multiple-row subquery
ST_CLERK
SA_MAN
Main query
Subquery
returns
•Single-row subquery

6-8 Copyright © Oracle Corporation, 2001. All rights reserved.
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

6-9 Copyright © Oracle Corporation, 2001. All rights reserved.


SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary >
(SELECT salary
FROM employees
WHERE employee_id = 143);
Executing Single-Row Subqueries
ST_CLERK
2600

6-10 Copyright © Oracle Corporation, 2001. All rights reserved.
SELECT last_name, job_id, salary
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees);
Using Group Functions in a Subquery
2500

6-11 Copyright © Oracle Corporation, 2001. All rights reserved.
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) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
2500

6-12 Copyright © Oracle Corporation, 2001. All rights reserved.
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
What is Wrong
with this Statement?
ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
Single-row operator with multiple-row subquery

6-13 Copyright © Oracle Corporation, 2001. All rights reserved.
Will this Statement Return Rows?
no rows selected
no rows selected
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
Subquery returns no values

6-14 Copyright © Oracle Corporation, 2001. All rights reserved.
Multiple-Row Subqueries
•Return more than one row
•Use multiple-row comparison operators
Operator
IN
ANY

ALL
Meaning
Equal to any member in the list
Compare value to each value returned by
the subquery
Compare value to every value returned by
the subquery

6-15 Copyright © Oracle Corporation, 2001. All rights reserved.
Using the ANY Operator
in Multiple-Row Subqueries
9000, 6000, 4200
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';

6-16 Copyright © Oracle Corporation, 2001. All rights reserved.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
Using the ALL Operator
in Multiple-Row Subqueries
9000, 6000, 4200

6-17 Copyright © Oracle Corporation, 2001. All rights reserved.
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 selectedno rows selected

6-18 Copyright © Oracle Corporation, 2001. All rights reserved.
Summary
In this lesson, you should have learned how to:
•Identify when 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);

6-19 Copyright © Oracle Corporation, 2001. All rights reserved.
Practice 6 Overview
This practice covers the following topics:
•Creating subqueries to query values based on
unknown criteria
•Using subqueries to find out which values exist in
one set of data and not in another