Copyright  Oracle Corporation, 1998. All rights reserved.
6
Subqueries
6-2 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-3 Copyright  Oracle Corporation, 1998. All rights reserved.
Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’?”
“Which employees have a salary greater
than Jones’ salary?”
Main Query
?
“What is Jones’ salary?”
?
Subquery
6-4 Copyright  Oracle Corporation, 1998. All rights reserved.
Subqueries
• 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, 1998. All rights reserved.
Using a Subquery
Display the names of those employees
who earn more than
employee no 7566.
6-6 Copyright  Oracle Corporation, 1998. All rights reserved.
2975
SQL> SELECT ename
2 FROM emp
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
Using a Subquery
ENAME
----------
KING
FORD
SCOTT
6-7 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-8 Copyright  Oracle Corporation, 1998. All rights reserved.
Types of Subqueries
• Single-row subquery
Main query
Subquery
returns
CLERK
• Multiple-row subquery
CLERK
MANAGER
Main query
Subquery
returns
• Multiple-column subquery
CLERK 7900
MANAGER 7698
Main query
Subquery
returns
6-9 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-10 Copyright  Oracle Corporation, 1998. All rights reserved.
Executing Single-Row Subqueries
Display name and job of all those employees
who have same job as employee no 7369
and
who have salary greater than the salary of
employee no 7876.
6-11 Copyright  Oracle Corporation, 1998. All rights reserved.
Executing Single-Row Subqueries
CLERK
1100
ENAME JOB
---------- ---------
MILLER CLERK
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal >
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);
6-12 Copyright  Oracle Corporation, 1998. All rights reserved.
Using Group Functions
in a Subquery
Display name, job and salary of those employees
who have
Salary equal to the minimum salary.
6-13 Copyright  Oracle Corporation, 1998. All rights reserved.
Using Group Functions
in a Subquery
800
ENAME JOB SAL
---------- --------- ---------
SMITH CLERK 800
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);
6-14 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.
800
Display department no, minimum salary
of each department
who have minimum salary
greater than the
minimum salary of department no 20.
6-15 Copyright  Oracle Corporation, 1998. All rights reserved.
HAVING Clause with Subqueries
800
SQL> SELECT deptno, MIN(sal)
2 FROM emp
3 GROUP BY deptno
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
6-16 Copyright  Oracle Corporation, 1998. All rights reserved.
What Is Wrong with This Statement?
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows selected
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp
6 GROUP BY deptno);
6-17 Copyright  Oracle Corporation, 1998. All rights reserved.
Will This Statement Work?
no rows selected
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
6-18 Copyright  Oracle Corporation, 1998. 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-19 Copyright  Oracle Corporation, 1998. All rights reserved.
Using the IN Operator
Use the IN operator to test for values in a
list.
SQL> SELECT empno, ename, sal, mgr
2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);
EMPNO ENAME SAL MGR
--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
7876 ADAMS 1100 7788
Subquery
6-20 Copyright  Oracle Corporation, 1998. All rights reserved.
Using ANY Operator
in Multiple-Row Subqueries
950
800
1100
1300
EMPNO ENAME JOB
--------- ---------- ---------
7654 MARTIN SALESMAN
7521 WARD SALESMAN
SQL> SELECT empno, ename, job
2 FROM emp
3 WHERE sal < ANY
4 (SELECT sal
5 FROM emp
6 WHERE job = 'CLERK')
7 AND job <> 'CLERK';
EMP
DEPTNO JOB SAL
------ --------- -------
10 MANAGER 2450
10 PRESIDENT 5000
10 CLERK 1300
20 CLERK 800
20 CLERK 1100
20 ANALYST 3000
20 ANALYST 3000
20 MANAGER 2975
30 SALESMAN 1600
30 MANAGER 2850
30 SALESMAN 1250
30 CLERK 950
30 SALESMAN 1500
30 SALESMAN 1250
6-21 Copyright  Oracle Corporation, 1998. All rights reserved.
Using ALL Operator
in Multiple-Row Subqueries
2916.6667
2175
1566.6667
EMPNO ENAME JOB
--------- ---------- ---------
7839 KING PRESIDENT
7566 JONES MANAGER
7902 FORD ANALYST
7788 SCOTT ANALYST
SQL> SELECT empno, ename, job
2 FROM emp
3 WHERE sal > ALL
4 (SELECT avg(sal)
5 FROM emp
6 GROUP BY deptno);
DEPTNO SAL
------ -------
10 2450
10 5000
10 1300
20 800
20 1100
20 3000
20 3000
20 2975
30 1600
30 2850
30 1250
30 950
30 1500
30 1250
DEPTNO AVG(SAL)
------- ---------
10 2916.6667
20 2175
30 1566.6667
6-22 Copyright  Oracle Corporation, 1998. All rights reserved.
Select all customers where rating is bigger than
the rating of all customers living in Berlin.
SELECT *
FROM XYZ
WHERE rating > ALL ( SELECT rating FROM XYZ
WHERE stadt='Berlin' );
6-23 Copyright  Oracle Corporation, 1998. All rights reserved.
Select all customers with rating bigger than
rating of at least one of living in Berlin.
SELECT *
FROM tkunden
WHERE rating > ANY ( SELECT rating FROM
tkunden WHERE stadt='Berlin' );

Les06- Subqueries.ppt

  • 1.
    Copyright  OracleCorporation, 1998. All rights reserved. 6 Subqueries
  • 2.
    6-2 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
  • 3.
    6-3 Copyright Oracle Corporation, 1998. All rights reserved. Using a Subquery to Solve a Problem “Who has a salary greater than Jones’?” “Which employees have a salary greater than Jones’ salary?” Main Query ? “What is Jones’ salary?” ? Subquery
  • 4.
    6-4 Copyright Oracle Corporation, 1998. All rights reserved. Subqueries • 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);
  • 5.
    6-5 Copyright Oracle Corporation, 1998. All rights reserved. Using a Subquery Display the names of those employees who earn more than employee no 7566.
  • 6.
    6-6 Copyright Oracle Corporation, 1998. All rights reserved. 2975 SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno=7566); Using a Subquery ENAME ---------- KING FORD SCOTT
  • 7.
    6-7 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.
  • 8.
    6-8 Copyright Oracle Corporation, 1998. All rights reserved. Types of Subqueries • Single-row subquery Main query Subquery returns CLERK • Multiple-row subquery CLERK MANAGER Main query Subquery returns • Multiple-column subquery CLERK 7900 MANAGER 7698 Main query Subquery returns
  • 9.
    6-9 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
  • 10.
    6-10 Copyright Oracle Corporation, 1998. All rights reserved. Executing Single-Row Subqueries Display name and job of all those employees who have same job as employee no 7369 and who have salary greater than the salary of employee no 7876.
  • 11.
    6-11 Copyright Oracle Corporation, 1998. All rights reserved. Executing Single-Row Subqueries CLERK 1100 ENAME JOB ---------- --------- MILLER CLERK SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8 (SELECT sal 9 FROM emp 10 WHERE empno = 7876);
  • 12.
    6-12 Copyright Oracle Corporation, 1998. All rights reserved. Using Group Functions in a Subquery Display name, job and salary of those employees who have Salary equal to the minimum salary.
  • 13.
    6-13 Copyright Oracle Corporation, 1998. All rights reserved. Using Group Functions in a Subquery 800 ENAME JOB SAL ---------- --------- --------- SMITH CLERK 800 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp);
  • 14.
    6-14 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. 800 Display department no, minimum salary of each department who have minimum salary greater than the minimum salary of department no 20.
  • 15.
    6-15 Copyright Oracle Corporation, 1998. All rights reserved. HAVING Clause with Subqueries 800 SQL> SELECT deptno, MIN(sal) 2 FROM emp 3 GROUP BY deptno 4 HAVING MIN(sal) > 5 (SELECT MIN(sal) 6 FROM emp 7 WHERE deptno = 20);
  • 16.
    6-16 Copyright Oracle Corporation, 1998. All rights reserved. What Is Wrong with This Statement? ERROR: ORA-01427: single-row subquery returns more than one row no rows selected SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp 6 GROUP BY deptno);
  • 17.
    6-17 Copyright Oracle Corporation, 1998. All rights reserved. Will This Statement Work? no rows selected SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE ename='SMYTHE');
  • 18.
    6-18 Copyright Oracle Corporation, 1998. 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
  • 19.
    6-19 Copyright Oracle Corporation, 1998. All rights reserved. Using the IN Operator Use the IN operator to test for values in a list. SQL> SELECT empno, ename, sal, mgr 2 FROM emp 3 WHERE mgr IN (7902, 7566, 7788); EMPNO ENAME SAL MGR --------- ---------- --------- --------- 7902 FORD 3000 7566 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788 Subquery
  • 20.
    6-20 Copyright Oracle Corporation, 1998. All rights reserved. Using ANY Operator in Multiple-Row Subqueries 950 800 1100 1300 EMPNO ENAME JOB --------- ---------- --------- 7654 MARTIN SALESMAN 7521 WARD SALESMAN SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4 (SELECT sal 5 FROM emp 6 WHERE job = 'CLERK') 7 AND job <> 'CLERK'; EMP DEPTNO JOB SAL ------ --------- ------- 10 MANAGER 2450 10 PRESIDENT 5000 10 CLERK 1300 20 CLERK 800 20 CLERK 1100 20 ANALYST 3000 20 ANALYST 3000 20 MANAGER 2975 30 SALESMAN 1600 30 MANAGER 2850 30 SALESMAN 1250 30 CLERK 950 30 SALESMAN 1500 30 SALESMAN 1250
  • 21.
    6-21 Copyright Oracle Corporation, 1998. All rights reserved. Using ALL Operator in Multiple-Row Subqueries 2916.6667 2175 1566.6667 EMPNO ENAME JOB --------- ---------- --------- 7839 KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal > ALL 4 (SELECT avg(sal) 5 FROM emp 6 GROUP BY deptno); DEPTNO SAL ------ ------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 DEPTNO AVG(SAL) ------- --------- 10 2916.6667 20 2175 30 1566.6667
  • 22.
    6-22 Copyright Oracle Corporation, 1998. All rights reserved. Select all customers where rating is bigger than the rating of all customers living in Berlin. SELECT * FROM XYZ WHERE rating > ALL ( SELECT rating FROM XYZ WHERE stadt='Berlin' );
  • 23.
    6-23 Copyright Oracle Corporation, 1998. All rights reserved. Select all customers with rating bigger than rating of at least one of living in Berlin. SELECT * FROM tkunden WHERE rating > ANY ( SELECT rating FROM tkunden WHERE stadt='Berlin' );