SQL WORKSHOP::Lecture 6

472 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
472
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Schedule: Timing Topic 25 minutes Lecture 30 minutes Practice 55 minutes Total
  • Lesson Aim In this lesson, you will learn about more advanced features of the SELECT statement. You can write subqueries in the WHERE clause of another SQL statement to obtain values based on an unknown conditional value. This lesson covers single-row subqueries and multiple-row subqueries.
  • Guidelines for Using Subqueries A subquery must be enclosed in parentheses. A subquery must appear on the right side of the comparison operator. Subqueries cannot contain an ORDER BY clause. You can have only one ORDER BY clause for a SELECT statement, and if specified it must be the last clause in the main SELECT statement. Two classes of comparison operators are used in subqueries: single-row operators and multiple-row operators. Class Management Note A subquery can execute multiple times in correlated subqueries, which are not included in this course. Students may ask how many subqueries can be written. The Oracle Server imposes no limit on the number of subqueries. The limit is related to the buffer size that the query uses.
  • Practice Overview In this practice, you will write complex queries using nested SELECT statements. Paper-Based Questions You may want to consider creating the inner query first for these questions. Make sure that it runs and produces the data that you anticipate before coding the outer query.
  • SQL WORKSHOP::Lecture 6

    1. 1. 66 Subqueries
    2. 2. 6-2 Objectives After completing this lesson, you shouldAfter completing this lesson, you should be able to do the following: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. 3. 6-3 Using a Subquery to Solve a Problem ““Who has a salary greater than Jones’?”Who has a salary greater than Jones’?” “Which employees have a salary greater than Jones’ salary?” Main Query ?? “What is Jones’ salary?” ?? Subquery
    4. 4. 6-4 Subqueries • The subquery (inner query) executes once before the main query. • The result of the subquery is used by the main query (outer query). SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
    5. 5. 6-5 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
    6. 6. 6-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. 7. 6-7 Types of Subqueries • Single-row subquery Main query Subquery returnsreturns CLERKCLERK • Multiple-row subquery CLERKCLERK MANAGERMANAGER Main query Subquery returnsreturns • Multiple-column subquery CLERK 7900CLERK 7900 MANAGER 7698MANAGER 7698 Main query Subquery returnsreturns
    8. 8. 6-8 Single-Row Subqueries • 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
    9. 9. 6-9 Executing Single-Row Subqueries CLERK 1100 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 9 FROM emp 10 WHERE empno = 7876);
    10. 10. 6-10 Using Group Functions in a Subquery 800 ENAME JOB SAL ---------- --------- --------- SMITH CLERK 800 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp);
    11. 11. 6-11 HAVING Clause with Subqueries • The Oracle Server executes subqueries first. • The Oracle Server returns results into the HAVING clause of the main query. 800 SQL> SELECT deptno, MIN(sal) 2 FROM emp 3 GROUP BY deptno 4 HAVING MIN(sal) > 5 (SELECT MIN(sal) 6 FROM emp 7 WHERE deptno = 20);
    12. 12. 6-12 What Is Wrong with This Statement? ERROR: ORA-01427: single-row subquery returns more than one row no rows selected SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp 6 GROUP BY deptno); Single-row operator with Single-row operator with multiple-row subquery multiple-row subquery
    13. 13. 6-13 Will This Statement Work? no rows selected Subquery returns no values Subquery returns no values SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROMemp 6 WHERE ename='SMYTHE');
    14. 14. 6-14 Multiple-Row Subqueries • 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
    15. 15. 6-15 Using ANY Operator in Multiple-Row Subqueries 950 800 1100 1300 EMPNO ENAME JOB --------- ---------- --------- 7654 MARTIN SALESMAN 7521 WARD SALESMAN SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4 (SELECT sal 5 FROM emp 6 WHERE job = 'CLERK') 7 AND job <> 'CLERK';
    16. 16. 6-16 Using ALL Operator in Multiple-Row Subqueries 2916.6667 2175 1566.6667 EMPNO ENAME JOB --------- ---------- --------- 7839 KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal > ALL 4 (SELECT avg(sal) 5 FROM emp 6 GROUP BY deptno);
    17. 17. 6-17 Summary Subqueries are useful when a query isSubqueries are useful when a query is based on unknown values.based on unknown values. SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
    18. 18. 6-18 Practice Overview • Creating subqueries to query values based on unknown criteria • Using subqueries to find out what values exist in one set of data and not in another

    ×