0% found this document useful (0 votes)
29 views

SQL67

The document discusses subqueries in SQL, including defining subqueries, the types of subqueries, and how to write single-row and multiple-row subqueries. It covers using comparison operators, group functions, HAVING clauses, and multiple-column subqueries with examples.

Uploaded by

Safeer Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

SQL67

The document discusses subqueries in SQL, including defining subqueries, the types of subqueries, and how to write single-row and multiple-row subqueries. It covers using comparison operators, group functions, HAVING clauses, and multiple-column subqueries with examples.

Uploaded by

Safeer Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

6

Subqueries

Copyright Ó Oracle Corporation, 1998. All rights reserved.


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

6-2 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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?”

6-3 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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).

6-4 Copyright Ó Oracle Corporation, 1998. All rights reserved.


Using a Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);

ENAME
----------
KING
FORD
SCOTT

6-5 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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.

6-6 Copyright Ó Oracle Corporation, 1998. All rights reserved.


Types of Subqueries
• Single-row subquery
Main query
returns
Subquery CLERK

• Multiple-row subquery
Main query

Subquery
returns CLERK
MANAGER
• Multiple-column subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
6-7 Copyright Ó Oracle Corporation, 1998. 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-8 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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 JOB
---------- ---------
MILLER CLERK

6-9 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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 JOB SAL


---------- --------- ---------
SMITH CLERK 800

6-10 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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);

6-11 Copyright Ó Oracle Corporation, 1998. All rights reserved.


What Is Wrong
with This Statement?
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4
w it h(SELECT MIN(sal)
5 r ator FROM emp
6 ow o p
e er y GROUP BY
-r qu deptno);
n g l e w sub
Si le- ro
u lt ip
m
ERROR:
ORA-01427: single-row subquery returns more than
one row

no rows selected

6-12 Copyright Ó Oracle Corporation, 1998. All rights reserved.


Will This Statement Work?

SQL> SELECT ename, job


2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
lues
o va
s n
no rows selected
tu rn
re
uery
ubq
S

6-13 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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

6-14 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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 ENAME JOB


--------- ---------- ---------
7654 MARTIN SALESMAN
7521 WARD SALESMAN

6-15 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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 ENAME JOB


--------- ---------- ---------
7839 KING PRESIDENT
7566 JONES MANAGER
7902 FORD ANALYST
7788 SCOTT ANALYST

6-16 Copyright Ó Oracle Corporation, 1998. All rights reserved.


Multiple-Column Subqueries
Display the order number ,product
number, and quantity of any item in which
the product number and quantity match
both the product number and quantity of
an item in order 605
SQL> SELECT ordid, prodid, qty
2 FROM item
3 WHERE (proid, qty) IN
4 (SELECT proid, qty
5 FROM item
6 WHERE ordid = 605)
7 AND ordid<>605;

6-17 Copyright Ó Oracle Corporation, 1998. All rights reserved.


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);

6-20 Copyright Ó Oracle Corporation, 1998. All rights reserved.

You might also like