11
7 7 Multiple-Column Subqueries Important Legal Notice: Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp. For further information, visit www.oracle.com This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.

7 Multiple-Column Subqueries

  • Upload
    ryu

  • View
    41

  • Download
    1

Embed Size (px)

DESCRIPTION

7 Multiple-Column Subqueries. Important Legal Notice: Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp. For further information, visit www.oracle.com - PowerPoint PPT Presentation

Citation preview

Page 1: 7  Multiple-Column Subqueries

7 7 Multiple-Column Subqueries

Important Legal Notice:

Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp.

For further information, visit www.oracle.com

This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.

Page 2: 7  Multiple-Column Subqueries

Objectives

After completing this lesson, you should 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

Page 3: 7  Multiple-Column Subqueries

Multiple-Column Subqueries

Main query

MANAGER 10Subquery SALESMAN 30 MANAGER 10 CLERK 20

Main query compares to

Values from a multiple-row and multiple-column subquery

MANAGER 10 SALESMAN 30 MANAGER 10 CLERK 20

Page 4: 7  Multiple-Column Subqueries

Using Multiple-Column SubqueriesDisplay the order id, product id, and quantity of items in the item table that match both the product id and quantity of an item in order 605. Note: This example applies to a different database from our class database (scott-tiger).SQL> SELECT ordid, prodid, qty

2 FROM item3 WHERE (prodid, qty) IN456

7 AND ordid <> 605;

(SELECT prodid, qty FROM item WHERE ordid = 605)

Page 5: 7  Multiple-Column Subqueries

Using Multiple-Column Subqueries (continued)

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, qty2 FROM item3 WHERE (prodid, qty) IN

456

7 AND ordid <> 605;

(SELECT prodid, qty

FROM item WHERE ordid = 605

Page 6: 7  Multiple-Column Subqueries

Column Comparisons

PairwisePRODID QTY 101863 100 100861 100 102130 10 100890 5100870 500101860 50

NonpairwisePRODID QTY 101863 100 100861 100 102130 10 100890 5100870 500101860 50

Page 7: 7  Multiple-Column Subqueries

Nonpairwise Comparison SubqueryDisplay the order number, product number, and quantity of any item in which the product number and quantity match any product number and any quantity of an item in Order 605.

SQL> SELECT ordid, prodid, qty2 FROM item3 WHERE prodid IN456

7 AND qty IN8

9 AND ordid <> 605;

(SELECT prodid, FROM item WHERE ordid = 605)

(SELECT qty FROM item WHERE ordid = 605)

Page 8: 7  Multiple-Column Subqueries

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.

Page 9: 7  Multiple-Column Subqueries

Null Values in a Subquery

SQL> SELECT employee.ename2 FROM emp employee

3 WHERE employee.empno NOT IN

45

no rows selected.

(SELECT manager.mgr FROM emp manager);

Page 10: 7  Multiple-Column Subqueries

Using a Subquery in the FROM ClauseSQL> SELECT a.ename, a.sal, a.deptno, b.salavg

2 FROM emp a,3 4

5 WHERE a.deptno = b.deptno6 AND a.sal > b.salavg;

(SELECT deptno, avg(sal) salavg FROM emp GROUP BY deptno) b

ENAME SAL DEPTNO SALAV

----------------- ------------ --------------- -----------------

KING 5000 10 2916.6667

JONES 2975 20 2175

SCOTT 3000 20 2175. . .6 rows selected.

Page 11: 7  Multiple-Column Subqueries

Summary

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