6 Writing Correlated Subqueries Copyright Oracle Corporation 1998
6 Writing Correlated Subqueries 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 6 -2 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. Main Query 6 -3 SELECT. . . FROM. . . WHERE. . . (SELECT. . . FROM. . . WHERE. . . ) Copyright Ó Oracle Corporation, 1998. All rights reserved. Subquery
Subqueries SELECT FROM WHERE select_list table expr operator (SELECT select_list FROM table); • The subquery (inner query) executes once before the main query. • The result of the subquery is used by the main query (outer query). 6 -4 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Using a Subquery SQL> SELECT ename 2 FROM emp 2975 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno = 7566); ENAME -----KING FORD SCOTT 6 -5 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 6 -6 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Correlated Subqueries SELECT outer 1, outer 2, . . . FROM table 1 alias 1 WHERE outer 1 operator (SELECT inner 1 FROM table 2 alias 2 WHERE alias 1. outer 2 = alias 2. inner 1); The subquery references a column from a table in the parent query. 6 -7 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Using Correlated Subqueries Find all employees who make more than the average salary in their department. SQL> SELECT empno, sal, deptno Each time the outer query is processed the 2 FROM emp outer inner query is 3 WHERE sal > (SELECT AVG(sal) evaluated. 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. 6 -8 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. 6 -9 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Using the EXISTS Operator Find employees who have at least one person reporting to them. 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); EMPNO ENAME ---------7839 KING 7698 BLAKE 7782 CLARK 7566 JONES. . . 6 rows selected. 6 -10 JOB DEPTNO -----PRESIDENT 10 MANAGER 30 MANAGER 10 MANAGER 20 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Using the NOT EXISTS Operator Find all departments that do not have any employees. SQL> SELECT 2 FROM 3 WHERE 4 5 deptno, dname dept d NOT EXISTS (SELECT '1' FROM emp e WHERE d. deptno = e. deptno); DEPTNO DNAME ---------40 OPERATIONS 6 -11 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Correlated UPDATE table 1 alias 1 SET column = (SELECT expression FROM table 2 alias 2 WHERE alias 1. column = alias 2. column); Use a correlated subquery to update rows in one table based on rows from another table. 6 -12 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Correlated DELETE FROM table 1 alias 1 WHERE column operator (SELECT expression FROM table 2 alias 2 WHERE alias 1. column = alias 2. column); Use a correlated subquery to delete only those rows that also exist in another table. 6 -13 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. 6 -14 Copyright Ó Oracle Corporation, 1998. All rights reserved.
Practice Overview • Writing correlated subqueries • Using the EXISTS operator 6 -15 Copyright Ó Oracle Corporation, 1998. All rights reserved.
- Slides: 15