Download presentation
Presentation is loading. Please wait.
Published byEdmund Gardner Modified over 9 years ago
1
6 6 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 Lake Superior State University which has used Oracle systems as a database platform embedded on its ERP systems.
2
Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries
3
Using a Subquery to Solve a Problem Main Query “Which employees have a salary greater than Jones’ salary?” Subquery “What is Jones’ salary?” ? “Who has a salary greater than Jones’ salary?”
4
Subqueries SELECTselect_list FROMtable WHEREexpr operator ( SELECTselect_list FROMtable); The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query).
5
Using a Subquery SQL> SELECTename 2FROMemp 3WHEREsal > 4 5 6 ( SELECT sal FROM emp WHERE empno=7566 ) ; ENAME -------------------- KING FORD SCOTT Display employees who make more salary than the employee (7566)’ salary.
6
Guidelines for Using Subqueries Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Do not add an “ORDER BY” clause to a subquery. Use single-row operators with single-row subqueries. Use multiple-row operators with multiple-row subqueries.
7
Types of Subqueries Single-row subquery Subquery Main query returns CLERK Multiple-row subquery Subquery Main query returns CLERK MANAGER Multiple-column subquery Subquery Main query returns CLERK7900 MANAGER7698
8
Single-Row Subqueries Return only one row Use single-row comparison operators Practice: Display the employees whose job title is the same as that of employee 7369.
9
Executing Single-Row Subqueries SQL> SELECTename, job 2FROMemp 3WHERE job = 4 5 6 7AND sal > 8 9 10 ( SELECT job FROM emp WHERE empno = 7369 ) ( SELECT sal FROM emp WHERE empno = 7876 ); ENAMEJOB --------------------------------------- MILLERCLERK Display employees whose job title is the same as that of employee 7369, and whose salary is greater than that of employee 7876.
10
Using Group Functions in a Subquery SQL> SELECTename, job, sal 2FROMemp 3WHEREsal = 4 5 ( SELECT MIN (sal) FROM emp); ENAMEJOB SAL -------------------------------- MILLERCLERK800 Display the employee name, job title, and salary of all employees whose salary is equal to the minimum salary.
11
HAVING Clause with Subqueries The Oracle Server executes subqueries first. The Oracle Server returns the results into the HAVING clause of the main query. SQL> SELECTdeptno, MIN(sal) 2FROMemp 3GROUP BYdeptno 4 HAVING MIN(sal) > 5 6 7 ( SELECT MIN(sal) FROM emp WHERE deptno=20); 800 Display all the departments that have a minimum salary greater than that of DEPARTMENT 20.
12
What is Wrong with This Statement? SQL> SELECTempno, ename 2FROMemp 3WHERE sal = 4 5 6 ( SELECT MIN(sal) FROM emp GROUP BY deptno); ERROR: ORA-01427: single-row subquery returns more than one row no rows selected Single-row operator with multiple-row subquery
13
Will This Statement Work? SQL>SELECTename,job 2FROMemp 3WHEREjob = 4(SELECT job 5FROM emp 6WHERE ename=‘SMYTHE’); No rows selected Subquery returns no values
14
Multiple-Row Subqueries Return more than one row Use multiple-row comparison operators
15
Using ANY Operator in Multiple-Row Subqueries SQL> SELECTempno, ename, job 2FROM emp 3WHERE sal < ANY 4 5 6 7AND job <> ‘CLERK’ ; (SELECT sal FROM emp WHERE JOB = ‘CLERK’) 1300, 1100, 800, 950 EMPNONAMEJOB ------------------------------------------- 7654MARTINSALESMAN Display employees whose salary is less than any clerk and who are not clerks.
16
Using “ALL” Operator in Multiple- Row Subqueries SQL> SELECT empno, ename, job 2FROM emp 3WHERE sal > ALL 4 5 6 ( SELECT avg(sal) FROM emp GROUP BY deptno ); 1566.67, 2175, 2916.67 EMPNOENAMEJOB -------------------------------------------- 7839KINGPRESIDENT 7566JONESMANAGER 7902FORDANALYST 7788SCOTTANALYST Display employees whose salary is greater than the average salaries of all the departments.
17
Summary Subqueries are useful when a query is based on unknown values. SELECTselect_list FROMtable WHEREexpr operator ( SELECT select_list FROM table ) ;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.