0% found this document useful (0 votes)
32 views18 pages

CH 6 Sub Query

Uploaded by

Danish Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views18 pages

CH 6 Sub Query

Uploaded by

Danish Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

SUBQUERIES

Objectives

 After completing this lesson, you should be


able to do the following:
 Describe the types of problems 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 Jones’?”

Main Query

“Which employees have a salary greater


? than Jones’ salary?”

Subquery

?
“What is Jones’ salary?”
Subqueries

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
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);

ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
Guidelines for Using
Subqueries
 Enclose subqueries in parentheses.
 Place subqueries on the right side of the
comparison operator.
 Do not add an ORDER BY clause to a
subquery.
 Use single-row operators with single-row
subqueries.
 Use multiple-row operators with multiple-
row subqueries.
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery CLERK

•• Multiple-row
Multiple-row subquery
subquery
Main query

Subquery
returns CLERK
MANAGER
•• Multiple-column
Multiple-column subquery
subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
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
SQL> SELECT ename, job
2 FROM emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);

ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
Using Group Functions
in a Subquery
SQL> SELECT ename, job, sal
800
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);

ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
HAVING Clause with
Subqueries
 The Oracle Server executes subqueries first.
 The Oracle Server returns results into the
HAVING clause of the main query.

SQL> SELECT deptno, MIN(sal)


2 FROM emp
3 GROUP BY deptno
800
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
What Is Wrong
with This Statement?
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4
w i t h(SELECT MIN(sal)
5 r at or FROM emp
6 ow op
e y
l e -r sub quer GROUP BY deptno);
g
Sin ple-row
ul t i
m
ERROR:
ERROR:
ORA-01427:
ORA-01427: single-row
single-row subquery
subquery returns
returns more
more than
than
one
one row
row

no
no rows
rows selected
selected
Will This Statement Work?

SQL> SELECT ename, job


2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
l ues
o va
no
no rows
rows selected rn sn
selected t u
er y re
bqu
Su
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 ANY Operator
in Multiple-Row Subqueries
SQL> SELECT empno, ename, job 1300
2 FROM emp 1100
800
3 WHERE sal < ANY 950
4 (SELECT sal
5 FROM emp
6 WHERE job = 'CLERK')
7 AND job <> 'CLERK';

EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7654
7654 MARTIN
MARTIN SALESMAN
SALESMAN
7521
7521 WARD
WARD SALESMAN
SALESMAN
Using ALL Operator
in Multiple-Row Subqueries
SQL> SELECT empno, ename, job 1566.6667
2 FROM emp 2175
2916.6667
3 WHERE sal > ALL
4 (SELECT avg(sal)
5 FROM emp
6 GROUP BY deptno);

EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7839
7839 KING
KING PRESIDENT
PRESIDENT
7566
7566 JONES
JONES MANAGER
MANAGER
7902
7902 FORD
FORD ANALYST
ANALYST
7788
7788 SCOTT
SCOTT ANALYST
ANALYST
Summary
 Subqueries are useful when a query is based on
unknown values.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Practice Overview
 Creating subqueries to query values based on unknown criteria
 Using subqueries to find out what values exist in one set of data and not in another

You might also like