Documente Academic
Documente Profesional
Documente Cultură
1
Writing Correlated Subqueries
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
1-3
What Is a Subquery?
A subquery is a SELECT statement embedded in a clause of another SQL statement.
Main Query SELECT . . . FROM . . . WHERE . . . (SELECT . . . FROM . . . WHERE . . .) Subquery
1-4
Subqueries
SELECT FROM WHERE select_list table expr operator (SELECT FROM
select_list table);
The subquery (inner query) executes once before the main query.
The result of the subquery is used by the main query (outer query).
1-5
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
1-6
Correlated Subqueries
Used to affect row-by-row processing, each subquery is executed once for every row of the outer query.
GET candidate row
Correlated Subqueries
SELECT outer1, outer2, ... FROM table1 alias1 WHERE outer1 operator (SELECT inner1 FROM table2 alias2 WHERE alias1.outer2 = alias2.inner1);
1-8
inner query.
The condition is flagged TRUE.
query.
1-10
Copyright Oracle Corporation, 1998. All rights reserved.
1-12
Correlated UPDATE
UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column);
Use a correlated subquery to update rows in one table based on rows from another table.
1-13
Correlated DELETE
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.
1-14
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.
1-15
Copyright Oracle Corporation, 1998. All rights reserved.
Practice Overview
Writing correlated subqueries Using the EXISTS operator
1-16
1
Generating Scripts to Generate Scripts
Objectives
After completing this lesson, you should be able to do the following: Describe the types of problems that are solved by using SQL to generate SQL Write a script that generates a script of drop table statements Write a script that generates a script of insert into statements
1-18
Copyright Oracle Corporation, 1998. All rights reserved.
SQL Script
1-19
Copyright Oracle Corporation, 1998. All rights reserved.
1-20
1-21
SPOOL OFF
SELECT 'INSERT INTO dept VALUES ('|| deptno||','|| ''''||dname||''''||','|| ''''||loc||''''||');' FROM dept;
SPOOL OFF SET HEADING ON ECHO OFF FEEDBACK ON SET PAGESIZE 24
1-23
Result
1-24
Statement 2
1-25
Copyright Oracle Corporation, 1998. All rights reserved.
Summary
You can select virtually anything. Script files often use the data dictionary. You use the SPOOL command to capture output in a file.
1-26
Practice Overview
Writing a script to DESCRIBE and SELECT the data from your tables Writing a script to revoke user privileges
1-27
1
Reporting Using SQL*Plus
Objectives
After completing this lesson, you should be able to do the following: Format output with the SET commands Add header and footer information Aggregate data in the output
1-29
RECSEPCHAR
SPACE UNDERLINE
WRAP
NULL HEADSEP NEWPAGE
1-31
Copyright Oracle Corporation, 1998. All rights reserved.
EMPNO ENAME JOB MGR SAL --------- ---------- --------- --------- --------7839 KING PRESIDENT 5000 __________________________________________________ 7782 CLARK MANAGER 7839 2450 __________________________________________________ 7934 MILLER CLERK 7782 1300 __________________________________________________
1-32
Copyright Oracle Corporation, 1998. All rights reserved.
EMPNO ENAME SAL MGR --------- ---------- --------- --------7839 KING 5000 7698 BLAKE 2850 7839 7782 CLARK 2450 7839 ... 14 rows selected.
1-35
Copyright Oracle Corporation, 1998. All rights reserved.
Print specification:
COL n S[KIP] [n]
TAB n
BOLD FORMAT
1-36
EMPNO ENAME SAL MGR --------- ---------- --------- --------7839 KING 5000 Null 7698 BLAKE 2850 7839 7782 CLARK 2450 7839 14 rows selected.
1-37
ADAMS MILLER
1100 1300
7788 7782
1-38
1-40
1-41
1-42
COUNT
MAXIMUM MINIMUM
1-43
Copyright Oracle Corporation, 1998. All rights reserved.
800
DEPTNO ENAME SAL --------- ---------- --------10 MILLER 1300 ********* --------maximum 1300
800 1100
20
SMITH
800
10 rows selected.
1-46
Copyright Oracle Corporation, 1998. All rights reserved.
1-47
4 3
ENAME HIREDATE SAL ---------- --------- --------SMITH 17-DEC-80 800 14 rows selected.
1-48
Copyright Oracle Corporation, 1998. All rights reserved.
Summary
Use the SET commands to format report output.
Use TTITLE and BTITLE commands to format page header and footer.
Calculate and print summary lines by using the COMPUTE command.
1-49
Practice Overview
Identify various SQL*Plus commands Produce more readable output Aggregate data
1-50
Objectives
After completing this lesson, you should be able to do the following:
1-51
Intersect
A B A B
Minus
1-52
Copyright Oracle Corporation, 1998. All rights reserved.
EMP
EMPNO DEPTNO --------7839 10 7698 30 7782 10 7566 20 7654 30 7499 30 7844 30 7900 30 7521 30 7902 20 1-53 7369 20
7698NAME 28-SEP-81 1250 TITLE 1400 EMPID DEPTID ALLEN SALESMAN --------7698-------------------20-FEB-81 1600 --------300 TURNER SALESMAN 7698SPENCER 08-SEP-81 1500 OPERATOR 0 6087 20 EMP_HISTORY JAMES CLERK 7698VANDYKE 03-DEC-81 950 MANAGER 6185 10 WARD SALESMAN 7698BALFORD 22-FEB-81 1250 CLERK 500 6235 20 FORD ANALYST 7566SCOTT 03-DEC-81 3000 ANALYST 7788 20 Copyright Oracle Corporation, 1998. All rights800 reserved. SMITH CLERK 7902 17-DEC-80 7001 JEWELL ANALYST 30
22-FEB-80
05-MAY-81 10-JUN-81
ENAME JOB DEPTNO ---------- --------- --------ADAMS CLERK 30 ALLEN SALESMAN 30 ALLEN SALESMAN 20 BALFORD CLERK 20 BLAKE MANAGER 30 ... 20 rows selected.
1-54
Copyright Oracle Corporation, 1998. All rights reserved.
1-55
ENAME EMPNO ---------- --------KING 7839 BLAKE 7698 CLARK 7782 CLARK 7782 MARTIN 7654 ... 23 rows selected.
ENAME EMPNO JOB ---------- --------- --------ALLEN 7499 SALESMAN CLARK 7782 MANAGER SCOTT 7788 ANALYST
1-56
Copyright Oracle Corporation, 1998. All rights reserved.
MINUS
Display the names, employee numbers, and job titles for all employees who have left the company.
SQL> 2 3 4 5 SELECT FROM MINUS SELECT FROM name, empid, title emp_history A B
NAME EMPID TITLE ---------- --------- --------BALFORD 6235 CLERK BRIGGS 7225 PAY CLERK JEWELL 7001 ANALYST SPENCER 6087 OPERATOR ... 1-57 6 rows selected. Copyright Oracle Corporation, 1998. All rights reserved.
1-59
Summary
UNION returns all distinct rows. UNION ALL returns all rows including duplicates. INTERSECT returns all rows that both queries share. MINUS returns all distinct rows selected by the first query but not the second. ORDER BY can only appear at the very end of the statement.
1-61
Copyright Oracle Corporation, 1998. All rights reserved.
Practice Overview
In this practice you will write queries using the SET operators. Discovering alternative join methods Writing compound queries as a kind of if statement
1-62