Copyright Oracle Corporation, 1997. All rights reserved. 5 5 Subqueries
Copyright Oracle Corporation, 1997. All rights reserved.
55
SubqueriesSubqueries
5-2 Copyright Oracle Corporation, 1997. All rights reserved.
ObjectivesObjectives
At the end of this lesson, you should be At the end of this lesson, you should be able to:able to:
• Describe the types of problems that subqueries can solve
• Define subqueries
• List the types of subqueries
• Write single-row and multiple-row subqueries
At the end of this lesson, you should be At the end of this lesson, you should be able to:able to:
• Describe the types of problems that subqueries can solve
• Define subqueries
• List the types of subqueries
• Write single-row and multiple-row subqueries
5-3 Copyright Oracle Corporation, 1997. All rights reserved.
Using a Subquery to Solve a ProblemUsing a Subquery to Solve a Problem
““Who has a salary greater than Jones’s?”Who has a salary greater than Jones’s?”““Who has a salary greater than Jones’s?”Who has a salary greater than Jones’s?”
“Which employees have a salary greater than Jones’s salary?”
Main Query
??
“What is Jones’s salary?”??
Subquery
5-4 Copyright Oracle Corporation, 1997. All rights reserved.
SubqueriesSubqueries
• The subquery (inner query) executes once before the main query.
• The result of the subquery is used by the main query (outer query).
• 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_listFROM tableWHERE expr operator
(SELECT select_list FROM table);
5-5 Copyright Oracle Corporation, 1997. 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 SubqueryUsing a Subquery
ENAME----------KINGFORDSCOTT
ENAME----------KINGFORDSCOTT
5-6 Copyright Oracle Corporation, 1997. All rights reserved.
Guidelines for Using SubqueriesGuidelines 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.
• 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.
5-7 Copyright Oracle Corporation, 1997. All rights reserved.
Types of SubqueriesTypes of Subqueries• Single-row subquery• Single-row subquery
Main query
Subquery returnsreturns
CLERKCLERK
• Multiple-row subquery• Multiple-row subquery
CLERKCLERKMANAGERMANAGER
Main query
Subquery returnsreturns
• Multiple-column subquery• Multiple-column subquery
CLERK 7900CLERK 7900MANAGER 7698MANAGER 7698
Main query
Subquery returnsreturns
5-8 Copyright Oracle Corporation, 1997. All rights reserved.
Single-Row SubqueriesSingle-Row Subqueries
• Return only one row
• Use single-row comparison operators
• 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
5-9 Copyright Oracle Corporation, 1997. All rights reserved.
Executing Single-Row SubqueriesExecuting Single-Row Subqueries
CLERK
1100
ENAME JOB---------- ---------MILLER CLERK
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);
5-10 Copyright Oracle Corporation, 1997. All rights reserved.
Using Group Functions in a Subquery
Using Group Functions in a Subquery
800
ENAME JOB SAL---------- --------- ---------SMITH CLERK 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);
5-11 Copyright Oracle Corporation, 1997. All rights reserved.
HAVING Clause with SubqueriesHAVING Clause with Subqueries
• The Oracle10 Server executes subqueries first.
• The Oracle10 Server returns results into the main query’s HAVING clause.
• The Oracle10 Server executes subqueries first.
• The Oracle10 Server returns results into the main query’s HAVING clause.
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);
5-12 Copyright Oracle Corporation, 1997. All rights reserved.
What Is Wrong with This Statement?
What Is Wrong with This Statement?
ERROR:ORA-01427: single-row subquery returns more thanone row
no rows selected
ERROR:ORA-01427: single-row subquery returns more thanone 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);
Single
-row o
perat
or with
multi
ple-ro
w subquer
y
Single
-row o
perat
or with
multi
ple-ro
w subquer
y
5-13 Copyright Oracle Corporation, 1997. All rights reserved.
Will This Statement Work?Will This Statement Work?
no rows selectedno rows selected
Subquery
retu
rns
no val
ues
Subquery
retu
rns
no val
ues
SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE ename='SMYTHE');
5-14 Copyright Oracle Corporation, 1997. All rights reserved.
Multiple-Row SubqueriesMultiple-Row Subqueries
• Return more than one row
• Use multiple-row comparison operators
• 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
5-15 Copyright Oracle Corporation, 1997. All rights reserved.
Using ANY Operator in Multiple-Row Subqueries
Using ANY Operator in Multiple-Row Subqueries
9508001100
1300
EMPNO ENAME JOB--------- ---------- --------- 7654 MARTIN SALESMAN 7521 WARD SALESMAN
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';
5-16 Copyright Oracle Corporation, 1997. All rights reserved.
Using ALL Operator in Multiple-Row Subqueries
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
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)
5-17 Copyright Oracle Corporation, 1997. All rights reserved.
Multiple-Column SubqueriesMultiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
Main query Main query
comparescompares
MANAGER 10MANAGER 10
Values from a multiple-row andValues from a multiple-row and
multiple-column subquerymultiple-column subquery
SALESMAN SALESMAN 3030
MANAGER MANAGER 1010
CLERK CLERK 2020
toto
5-18 Copyright Oracle Corporation, 1997. All rights reserved.
Using Multiple-Column Subqueries
Using Multiple-Column Subqueries
Display the name, department number, salary, Display the name, department number, salary, and commission of any employee whose salary and commission of any employee whose salary and commission matches and commission matches bothboth the commission the commission and salary of any employee in department 30.and salary of any employee in department 30.
Display the name, department number, salary, Display the name, department number, salary, and commission of any employee whose salary and commission of any employee whose salary and commission matches and commission matches bothboth the commission the commission and salary of any employee in department 30.and salary of any employee in department 30.
SQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE (sal, NVL(comm,-1)) IN 4 (SELECT sal, NVL(comm,-1) 5 FROM emp 6 WHERE deptno = 30);
5-19 Copyright Oracle Corporation, 1997. All rights reserved.
Column ComparisonsColumn Comparisons
Pairwise
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
Nonpairwise
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
5-20 Copyright Oracle Corporation, 1997. All rights reserved.
Nonpairwise Comparison Subquery
Nonpairwise Comparison Subquery
SQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE sal IN (SELECT sal 4 FROM emp 5 WHERE deptno = 30) 6 AND 7 NVL(comm,-1) IN (SELECT NVL(comm,-1) 8 FROM emp 9 WHERE deptno = 30);
Display the name, department number, salary, Display the name, department number, salary, and commission of any employee whose salary and commission of any employee whose salary and commission matches the commission and and commission matches the commission and salary of any employee in department 30.salary of any employee in department 30.
Display the name, department number, salary, Display the name, department number, salary, and commission of any employee whose salary and commission of any employee whose salary and commission matches the commission and and commission matches the commission and salary of any employee in department 30.salary of any employee in department 30.
5-21 Copyright Oracle Corporation, 1997. All rights reserved.
Modifying the EMP TableModifying the EMP Table
• Assume that salary and commission for Clark are modified.
• Assume that salary and commission for Clark are modified.
• Salary is changed to $1500 and commission to $300.
• Salary is changed to $1500 and commission to $300.
ENAME SAL COMM---------- --------- ---------...CLARK 1500 300...ALLEN 1600 300TURNER 1500 0...14 rows selected.
5-22 Copyright Oracle Corporation, 1997. All rights reserved.
Pairwise SubqueryPairwise SubquerySQL> SELECT ename, deptno, sal, comm 2 FROM emp 3 WHERE (sal, NVL(comm,-1)) IN 4 (SELECT sal, NVL(comm,-1) 5 FROM emp 6 WHERE deptno = 30);
ENAME DEPTNO SAL COMM---------- --------- --------- ---------JAMES 30 950WARD 30 1250 500MARTIN 30 1250 1400TURNER 30 1500 0ALLEN 30 1600 300BLAKE 30 2850
6 rows selected.
ENAME DEPTNO SAL COMM---------- --------- --------- ---------JAMES 30 950WARD 30 1250 500MARTIN 30 1250 1400TURNER 30 1500 0ALLEN 30 1600 300BLAKE 30 2850
6 rows selected.
5-23 Copyright Oracle Corporation, 1997. All rights reserved.
SQL> SELECT ename,deptno, sal, comm 2 FROM emp 3 WHERE sal IN (SELECT sal 4 FROM emp 5 WHERE deptno = 30) 6 AND 7 NVL(comm,-1) IN (SELECT NVL(comm,-1) 8 FROM emp 9 WHERE deptno = 30);
Nonpairwise SubqueryNonpairwise Subquery
ENAME DEPTNO SAL COMM---------- --------- --------- ---------JAMES 30 950BLAKE 30 2850TURNER 30 1500 0CLARK 10 1500 300...7 rows selected.
5-24 Copyright Oracle Corporation, 1997. All rights reserved.
Null Values in a SubqueryNull Values in a Subquery
SQL> SELECT employee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN
(SELECT manager.mgr FROM emp manager);
no rows selected.no rows selected.
5-25 Copyright Oracle Corporation, 1997. All rights reserved.
Using a Subquery in the FROM ClauseUsing a Subquery
in the FROM Clause
ENAME SAL DEPTNO SALAVG---------- --------- --------- ----------KING 5000 10 2916.6667JONES 2975 20 2175SCOTT 3000 20 2175...6 rows selected.
ENAME SAL DEPTNO SALAVG---------- --------- --------- ----------KING 5000 10 2916.6667JONES 2975 20 2175SCOTT 3000 20 2175...6 rows selected.
SQL> SELECT a.ename, a.sal, a.deptno, b.salavg 2 FROM emp a, (SELECT deptno, avg(sal) salavg 3 FROM emp 4 GROUP BY deptno) b 5 WHERE a.deptno = b.deptno 6 AND a.sal > b.salavg;
5-26 Copyright Oracle Corporation, 1997. All rights reserved.
SummarySummary
• A multiple-column subquery returns more than one column.
• Column comparisons in a multiple- column comparisons can be pairwise or nonpairwise.
• A multiple-column subquery can also be used in the FROM clause of a SELECT statement.
• A multiple-column subquery returns more than one column.
• Column comparisons in a multiple- column comparisons can be pairwise or nonpairwise.
• A multiple-column subquery can also be used in the FROM clause of a SELECT statement.
5-27 Copyright Oracle Corporation, 1997. All rights reserved.
Practice OverviewPractice Overview
Creating subqueriesCreating subqueries