Presentation is loading. Please wait.

Presentation is loading. Please wait.

7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina,

Similar presentations


Presentation on theme: "7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina,"— Presentation transcript:

1 7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corporation.  For further information, visit www.oracle.com  This presentation must be used for only education purpose for students at Lake Superior State University which has used Oracle systems as a database platform embedded on its enterprise systems.

2 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

3 Multiple-Column Subqueries Main query MANAGER 10 Subquery SALESMAN30 MANAGER10 CLERK20 Main query compares to Values from a multiple-row and multiple-column subquery MANAGER 10SALESMAN 30 MANAGER 10 CLERK 20

4 Using Multiple-Column Subqueries Display 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> SELECTord_id, prod_id, qty 2 FROMitem 3 WHERE(prod_id, qty) IN 4 5 6 7 ANDord_id <> 605; (SELECT prod_id, qty FROMitem WHERE ord_id = 605)

5 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> SELECTord_id, prod_id, qty 2 FROMitem 3 WHERE( prod_id, qty ) IN 4 5 6 7 ANDord_id <> 605; (SELECT prod_id, qty FROMitem WHERE ord_id = 605

6 Column Comparisons Pairwise PROD_IDQTY 101863100 100861100 10213010 1008905 100870500 10186050 Nonpairwise PROD_IDQTY 101863100 100861100 10213010 1008905 100870500 10186050

7 Nonpairwise Comparison Subquery Display 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> SELECTordid, prod_id, qty 2 FROMitem 3 WHEREprod_id IN 4 5 6 7 ANDqty IN 8 9 ANDord_id <> 605; (SELECT prod_id, FROMitem WHERE ord_id = 605) (SELECT qty FROMitem WHERE ord_id = 605)

8 Nonpairwise Subquery ORDIDPRODIDQTY ------------------------ ---------------- 609100870 5 616100861 10 616102130 10 621100861 10 618100870 10 618100861 50 616100870 50 617100861 100 619102130 100 615100870 100 617101860 100 621100870 100 617102130 100... 16 rows selected.

9 Null Values in a Subquery SQL> SELECT employee.ename 2 FROMemp employee 3 WHEREemployee.empno IN (SELECT manager.mgr 4 FROM emp manager); ENAME -------------- KING …….. 6 rows selected. Display the employees who have subordinates.

10 Null Values in a Subquery SQL> SELECT employee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN 4 5 no rows selected. (SELECT manager.mgr FROM emp manager); Display all the employees who do not have any subordinates (= who are not managers).

11 Using a Subquery in the FROM Clause SQL> SELECTa.ename, a.sal, a.deptno, b.sal_avg 2 FROM emp a, 3 4 5 WHEREa.deptno = b.deptno 6 AND a.sal > b.sal_avg; (SELECT deptno, avg(sal) sal_avg FROM emp GROUP BY deptno) b ENAMESALDEPTNO SAL_AVG ----------------- ------------ --------------- ----------------- KING5000 10 2916.6667 JONES2975 20 2175 SCOTT3000 20 2175... 6 rows selected.

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


Download ppt "7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina,"

Similar presentations


Ads by Google