Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1998. All rights reserved. 6 Writing Correlated Subqueries.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1998. All rights reserved. 6 Writing Correlated Subqueries."— Presentation transcript:

1 Copyright  Oracle Corporation, 1998. All rights reserved. 6 Writing Correlated Subqueries

2 6-2 Copyright  Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that can be solved with correlated subqueries Write correlated subqueries Use the EXISTS and NOT EXISTS operators Update and delete rows using correlated subqueries After completing this lesson, you should be able to do the following: Describe the types of problems that can be solved with correlated subqueries Write correlated subqueries Use the EXISTS and NOT EXISTS operators Update and delete rows using correlated subqueries

3 6-3 Copyright  Oracle Corporation, 1998. All rights reserved. What Is a Subquery? A subquery is a SELECT statement embedded in a clause of another SQL statement. SELECT... FROM... WHERE... (SELECT... FROM... WHERE...) MainQuery Subquery

4 6-4 Copyright  Oracle Corporation, 1998. All rights reserved. Subqueries The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). SELECTselect_list FROMtable WHEREexpr operator (SELECTselect_list FROMtable);

5 6-5 Copyright  Oracle Corporation, 1998. All rights reserved. 2975 SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno = 7566); Using a Subquery ENAME ---------- KING FORD SCOTT ENAME ---------- KING FORD SCOTT

6 6-6 Copyright  Oracle Corporation, 1998. All rights reserved. Correlated Subqueries Used to affect row-by-row processing, each subquery is executed once for every row of the outer query. GET candidate row EXECUTE inner query using candidate row value USE value(s) from inner query to qualify candidate row

7 6-7 Copyright  Oracle Corporation, 1998. All rights reserved. Correlated Subqueries SELECT outer1, outer2,... FROM table1 alias1 WHERE outer1 operator (SELECT inner1 FROM table2 alias2 WHERE alias1.outer2 = alias2.inner1); subquery references a column from a table in the parent query. The subquery references a column from a table in the parent query.

8 6-8 Copyright  Oracle Corporation, 1998. All rights reserved. Using Correlated Subqueries Each time the outer query is processed the inner query is evaluated. SQL> SELECT empno, sal, deptno 2 FROM emp outer 3 WHERE sal > (SELECT AVG(sal) 4 FROM emp inner 5 WHERE outer.deptno = inner.deptno); EMPNO SAL DEPTNO -------- --------- --------- 7839 5000 10 7698 2850 30 7566 2975 20... 6 rows selected. EMPNO SAL DEPTNO -------- --------- --------- 7839 5000 10 7698 2850 30 7566 2975 20... 6 rows selected. Find all employees who make more than the average salary in their department.

9 6-9 Copyright  Oracle Corporation, 1998. All rights reserved. Using the EXISTS Operator If a subquery row value is found: – The search does not continue in the inner query. – The condition is flagged TRUE. If a subquery row value is not found: – The condition is flagged FALSE. – The search continues in the inner query. If a subquery row value is found: – The search does not continue in the inner query. – The condition is flagged TRUE. If a subquery row value is not found: – The condition is flagged FALSE. – The search continues in the inner query.

10 6-10 Copyright  Oracle Corporation, 1998. All rights reserved. Find employees who have at least one person reporting to them. Using the EXISTS Operator EMPNO ENAME JOB DEPTNO --------- ---------- --------- --------- 7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 7782 CLARK MANAGER 10 7566 JONES MANAGER 20... 6 rows selected. SQL> SELECT empno, ename, job, deptno 2 FROM emp outer 3 WHERE EXISTS (SELECT empno 4 FROM emp inner 5 WHERE inner.mgr = outer.empno);

11 6-11 Copyright  Oracle Corporation, 1998. All rights reserved. Find all departments that do not have any employees. Using the NOT EXISTS Operator DEPTNO DNAME --------- ---------- 40 OPERATIONS DEPTNO DNAME --------- ---------- 40 OPERATIONS SQL> SELECTdeptno, dname 2 FROM dept d 3 WHERENOT EXISTS (SELECT '1' 4 FROM emp e 5 WHERE d.deptno = e.deptno);

12 6-12 Copyright  Oracle Corporation, 1998. All rights reserved. Correlated UPDATE Use a correlated subquery to update rows in one table based on rows from another table. UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column);

13 6-13 Copyright  Oracle Corporation, 1998. All rights reserved. DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); Use a correlated subquery to delete only those rows that also exist in another table. Correlated DELETE

14 6-14 Copyright  Oracle Corporation, 1998. All rights reserved. Summary Correlated subqueries are useful whenever a subquery must return a different result for each candidate row. The EXISTS operator is a Boolean operator, testing the presence of a value. Correlated subqueries can be used with SELECT, UPDATE, and DELETE statements. Correlated subqueries are useful whenever a subquery must return a different result for each candidate row. The EXISTS operator is a Boolean operator, testing the presence of a value. Correlated subqueries can be used with SELECT, UPDATE, and DELETE statements.

15 6-15 Copyright  Oracle Corporation, 1998. All rights reserved. Practice Overview Writing correlated subqueries Using the EXISTS operator Writing correlated subqueries Using the EXISTS operator

16 6-16 Copyright  Oracle Corporation, 1998. All rights reserved.

17 6-17 Copyright  Oracle Corporation, 1998. All rights reserved.

18 6-18 Copyright  Oracle Corporation, 1998. All rights reserved.


Download ppt "Copyright  Oracle Corporation, 1998. All rights reserved. 6 Writing Correlated Subqueries."

Similar presentations


Ads by Google