Sie sind auf Seite 1von 11

7

Multiple-Column Subqueries
Objectives

After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Write
Write aa multiple-column
multiple-column subquery
subquery
•• Describe
Describe and
and explain
explain thethe behavior
behavior of
of
subqueries
subqueries when
when null
null values
values are
are
retrieved
retrieved
•• Write
Write aa subquery
subquery inin aa FROM
FROM clause
clause

7-2
Multiple-Column Subqueries
Main query
MANAGER 10

Subquery
SALESMAN 30
MANAGER 10
CLERK 20

Main query Values from a multiple-row and


compares to
multiple-column subquery

SALESMAN 30
MANAGER 10
MANAGER 10
CLERK 20
7-3
Using Multiple-Column
Subqueries
Display
Display the
the order
order number,
number, product
product number,
number, and
and
quantity
quantity of
of any
any item
item in
in which
which the
the product
product
number
number and
and quantity
quantity match
match both
both the
the product
product
number
number and
and quantity
quantity of
of an
an item
item in
in order
order 605.
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-4
Column Comparisons

Pairwise Nonpairwise
PRODID QTY PRODID QTY
101863 100 101863 100
100861 100 100861 100
102130 10 102130 10
100890 5 100890 5
100870 500 100870 500
101860 50 101860 50

7-6
Nonpairwise Comparison
Subquery
Display
Display the
the order
order number,
number, product
product number,
number, and
and
quantity
quantity of
of any
any item
item in
in which
which the
the product
product number
number
and
and quantity
quantity match
match any
any product
product number
number and
and any
any
quantity
quantity of
of an
an item
item in
in order
order 605.
605.
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;

7-7
Nonpairwise 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-8
Null 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.

7-9
Using a Subquery
in the FROM Clause
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;

ENAME
ENAME SAL
SAL DEPTNO
DEPTNO SALAVG
SALAVG
----------
---------- ---------
--------- ---------
--------- ----------
----------
KING
KING 5000
5000 10
10 2916.6667
2916.6667
JONES
JONES 2975
2975 20
20 2175
2175
SCOTT
SCOTT 3000
3000 20
20 2175
2175
...
...
66 rows
rows selected.
selected.

7-10
Summary
•• A
A multiple-column
multiple-column subquery
subquery returns
returns
more
more than
than one
one column.
column.
•• Column
Column comparisons
comparisons in
in multiple-
multiple-
column
column comparisons
comparisons can
can be
be pairwise
pairwise or
or
nonpairwise.
nonpairwise.
•• A
A multiple-column
multiple-column subquery
subquery cancan also
also be
be
used
used in
in the
the FROM
FROM clause
clause of
of aa SELECT
SELECT
statement.
statement.

7-11
Practice Overview

Creating
Creating multiple-column
multiple-column subqueries
subqueries

7-12

Das könnte Ihnen auch gefallen