Download presentation
Presentation is loading. Please wait.
Published byLeslie Richard Modified over 9 years ago
1
6-1 Copyright Oracle Corporation, 1998. All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery CLERKMANAGER Main query Subquery returns Multiple-column subquery CLERK 7900 MANAGER 7698 Main query Subquery returns
2
6-2 Copyright Oracle Corporation, 1998. All rights reserved. Single-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
3
6-3 Copyright Oracle Corporation, 1998. All rights reserved. Executing 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 9FROMemp 10WHEREempno = 7876);
4
6-4 Copyright Oracle Corporation, 1998. All rights reserved. Multiple-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
6-5 Copyright Oracle Corporation, 1998. All rights reserved. Using Multiple-Column Subqueries 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> SELECTordid, prodid, qty 2 FROMitem 3 WHERE (prodid, qty) IN 4 (SELECT prodid, qty 5 FROM item 6 WHERE ordid = 605) 7 ANDordid <> 605;
6
6-6 Copyright Oracle Corporation, 1998. All rights reserved. Column Comparisons Pairwise PRODIDQTY 101863100 100861100 10213010 1008905 100870500 10186050 Nonpairwise PRODIDQTY 101863100 100861100 10213010 1008905 100870500 10186050
7
6-7 Copyright Oracle Corporation, 1998. All rights reserved. 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 ANDqty IN (SELECT qty 7 FROM item 8 WHERE ordid = 605) 9 ANDordid <> 605; 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.
8
6-8 Copyright Oracle Corporation, 1998. All rights reserved. Null Values in a Subquery SQL> SELECTemployee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN 4(SELECT manager.mgr 5 FROM emp manager); no rows selected.
9
6-9 Copyright Oracle Corporation, 1998. All rights reserved. Using a Subquery in the FROM Clause ENAME SAL DEPTNO SALAVG ---------- --------- --------- ---------- KING 5000 10 2916.6667 JONES 2975 20 2175 SCOTT 3000 20 2175... 6 rows selected. ENAME SAL DEPTNO SALAVG ---------- --------- --------- ---------- KING 5000 10 2916.6667 JONES 2975 20 2175 SCOTT 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;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.