Multiple-Column Subqueries
-
Upload
serina-adkins -
Category
Documents
-
view
20 -
download
0
description
Transcript of Multiple-Column Subqueries
Copyright Oracle Corporation, 1998. All rights reserved.
77
Multiple-Column SubqueriesMultiple-Column Subqueries
7-2 Copyright Oracle Corporation, 1998. All rights reserved.
ObjectivesObjectives
After completing this lesson, you should After completing this lesson, you should be able to do the following:be able to do the following:
• Write a multiple-column subquery
• Describe and explain the behavior of subqueries when null values are retrieved
• Write a subquery in a FROM clause
After completing this lesson, you should After completing this lesson, you should be able to do the following:be able to do the following:
• Write a multiple-column subquery
• Describe and explain the behavior of subqueries when null values are retrieved
• Write a subquery in a FROM clause
7-3 Copyright Oracle Corporation, 1998. All rights reserved.
Multiple-Column SubqueriesMultiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
Main queryMain querycomparescompares
MANAGER 10MANAGER 10
Values from a multiple-row andValues from a multiple-row andmultiple-column subquerymultiple-column subquery
SALESMAN SALESMAN 3030
MANAGER MANAGER 1010
CLERK CLERK 2020
toto
7-4 Copyright Oracle Corporation, 1998. All rights reserved.
Using Multiple-Column Subqueries
Using Multiple-Column Subqueries
Display the order number, product number, and Display the order number, product number, and quantity of any item in which the product quantity of any item in which the product number and quantity match number and quantity match bothboth the product the product number and quantity of an item in order 605.number and quantity of an item in order 605.
Display the order number, product number, and Display the order number, product number, and quantity of any item in which the product quantity of any item in which the product number and quantity match number and quantity match bothboth the product the product number and quantity of an item in order 605.number and quantity of an item in order 605.
SQL> SELECT ordid, prodid, qty 2 FROM item 3 WHERE (prodid, qty) IN 4 (SELECT prodid, qty 5 FROM item 6 WHERE ordid = 605) 7 AND ordid <> 605;
7-6 Copyright Oracle Corporation, 1998. All rights reserved.
Pair wisePair wise
select ename,deptno,salselect ename,deptno,sal
from empfrom emp
where (deptno,sal) in (select deptno,sal where (deptno,sal) in (select deptno,sal from empfrom emp
where comm is not null);where comm is not null);
select ename,deptno,salselect ename,deptno,sal
from empfrom emp
where (deptno,sal) in (select deptno,sal where (deptno,sal) in (select deptno,sal from empfrom emp
where comm is not null);where comm is not null);
7-7 Copyright Oracle Corporation, 1998. All rights reserved.
Column ComparisonsColumn Comparisons
Pairwise
PRODID QTY
101863 100
100861 100
102130 10
100890 5
100870 500
101860 50
Nonpairwise
PRODID QTY
101863 100
100861 100
102130 10
100890 5
100870 500
101860 50
7-8 Copyright Oracle Corporation, 1998. All rights reserved.
Nonpairwise Comparison Subquery
Nonpairwise Comparison Subquery
SQL> SELECT ordid, prodid, qty 2 FROM item 3 WHERE prodid IN (SELECT prodid 4 FROM item 5 WHERE ordid = 605) 6 AND qty IN (SELECT qty 7 FROM item 8 WHERE ordid = 605) 9 AND ordid <> 605;
Display the order number, product number, and Display the order number, product number, and quantity of any item in which the product number quantity of any item in which the product number and quantity match any product number and any and quantity match any product number and any quantity of an item in order 605.quantity of an item in order 605.
Display the order number, product number, and Display the order number, product number, and quantity of any item in which the product number quantity of any item in which the product number and quantity match any product number and any and quantity match any product number and any quantity of an item in order 605.quantity of an item in order 605.
7-9 Copyright Oracle Corporation, 1998. All rights reserved.
Non Pair wiseNon Pair wise
select ename,deptno,salselect ename,deptno,sal
from empfrom emp
where deptno in (select deptno from emp where deptno in (select deptno from emp where comm is not null)where comm is not null)
and sal in (select sal from emp where and sal in (select sal from emp where comm is not null);comm is not null);
select ename,deptno,salselect ename,deptno,sal
from empfrom emp
where deptno in (select deptno from emp where deptno in (select deptno from emp where comm is not null)where comm is not null)
and sal in (select sal from emp where and sal in (select sal from emp where comm is not null);comm is not null);
7-10 Copyright Oracle Corporation, 1998. All rights reserved.
Nonpairwise SubqueryNonpairwise Subquery
ORDID PRODID QTY--------- --------- --------- 609 100870 5 616 100861 10 616 102130 10 621 100861 10 618 100870 10 618 100861 50 616 100870 50 617 100861 100 619 102130 100 615 100870 100 617 101860 100 621 100870 100 617 102130 100 . . . 16 rows selected.
7-11 Copyright Oracle Corporation, 1998. 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 4 (SELECT manager.mgr 5 FROM emp manager);no rows selected.no rows selected.
7-12 Copyright Oracle Corporation, 1998. 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;
7-13 Copyright Oracle Corporation, 1998. All rights reserved.
Correlated SubqueryCorrelated Subquery
The outer Query is executed first and then The outer Query is executed first and then the inner query is executed.the inner query is executed.
Find the employee list who earn more Find the employee list who earn more than the avg salary of their own than the avg salary of their own departmentdepartment
Example:Example:
Select * from emp XSelect * from emp X
Where sal > (Select avg(sal) from emp Where sal > (Select avg(sal) from emp where x.deptno=deptno)where x.deptno=deptno)
The outer Query is executed first and then The outer Query is executed first and then the inner query is executed.the inner query is executed.
Find the employee list who earn more Find the employee list who earn more than the avg salary of their own than the avg salary of their own departmentdepartment
Example:Example:
Select * from emp XSelect * from emp X
Where sal > (Select avg(sal) from emp Where sal > (Select avg(sal) from emp where x.deptno=deptno)where x.deptno=deptno)
7-14 Copyright Oracle Corporation, 1998. All rights reserved.
Exists operatorExists operator
Find the list of employees who has Find the list of employees who has subordinates.subordinates.
Select * from emp eSelect * from emp e
Where exists(select 'X' from empWhere exists(select 'X' from emp
Where mgr=e.empno);Where mgr=e.empno);
Find the list of employees who has Find the list of employees who has subordinates.subordinates.
Select * from emp eSelect * from emp e
Where exists(select 'X' from empWhere exists(select 'X' from emp
Where mgr=e.empno);Where mgr=e.empno);
7-15 Copyright Oracle Corporation, 1998. All rights reserved.
Exists alternativeExists alternative
Select * from empSelect * from emp
Where empno in (select mgr from emp Where empno in (select mgr from emp where mgr is not null);where mgr is not null);
select distinct a.ename from emp a,emp bselect distinct a.ename from emp a,emp b
where a.empno=b.mgr;where a.empno=b.mgr;
select empno from emp where empno in select empno from emp where empno in (select mgr from emp);(select mgr from emp);
Select * from empSelect * from emp
Where empno in (select mgr from emp Where empno in (select mgr from emp where mgr is not null);where mgr is not null);
select distinct a.ename from emp a,emp bselect distinct a.ename from emp a,emp b
where a.empno=b.mgr;where a.empno=b.mgr;
select empno from emp where empno in select empno from emp where empno in (select mgr from emp);(select mgr from emp);
7-16 Copyright Oracle Corporation, 1998. All rights reserved.
Not ExistsNot Exists
Find the list of employees who has Find the list of employees who has managermanager
Select * from emp eSelect * from emp e
Where not exists(select 'X' from empWhere not exists(select 'X' from emp
Where mgr=e.empno)Where mgr=e.empno)
Find the list of employees who has Find the list of employees who has managermanager
Select * from emp eSelect * from emp e
Where not exists(select 'X' from empWhere not exists(select 'X' from emp
Where mgr=e.empno)Where mgr=e.empno)
7-17 Copyright Oracle Corporation, 1998. All rights reserved.
Non Exists AlternativeNon Exists Alternative
Select * from empSelect * from emp
Where empno not in (select mgr from emp Where empno not in (select mgr from emp where mgr is not null)where mgr is not null)
Select * from empSelect * from emp
Where empno not in (select mgr from emp Where empno not in (select mgr from emp where mgr is not null)where mgr is not null)
7-18 Copyright Oracle Corporation, 1998. All rights reserved.
SummarySummary
• A multiple-column subquery returns more than one column.
• Column comparisons in 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 multiple- column comparisons can be pairwise or nonpairwise.
• A multiple-column subquery can also be used in the FROM clause of a SELECT statement.
7-19 Copyright Oracle Corporation, 1998. All rights reserved.
Practice OverviewPractice Overview
Creating multiple-column subqueriesCreating multiple-column subqueries