Oracle9i: Advanced SQL
Student Guide • Volume 1
40058GC11
Production 1.1
November 2001
D34074
Copyright © Oracle Corporation, 2000, 2001. All rights reserved.
This documentation contains proprietary information of Oracle Corporation. It is
provided under a license agreement containing restrictions on use and disclosure and
is also protected by copyright law. Reverse engineering of the software is prohibited.
If this documentation is delivered to a U.S. Government Agency of the Department of
Defense, then it is delivered with Restricted Rights and the following legend is
applicable:
Restricted Rights Legend
Use, duplication or disclosure by the Government is subject to restrictions for
commercial computer software and shall be deemed to be Restricted Rights software
under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013,
Rights in Technical Data and Computer Software (October 1988).
This material or any portion of it may not be copied in any form or by any means
without the express prior written permission of Oracle Corporation. Any other copying
is a violation of copyright law and may result in civil and/or criminal penalties.
If this documentation is delivered to a U.S. Government Agency not within the
Department of Defense, then it is delivered with “Restricted Rights,” as defined in
FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987).
The information in this document is subject to change without notice. If you find any
problems in the documentation, please report them in writing to Education Products,
Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA 94065.
Oracle Corporation does not warrant that this document is error-free.
Oracle and all references to Oracle products are trademarks or registered trademarks
of Oracle Corporation.
All other products or company names are used for identification purposes only, and
may be trademarks of their respective owners.
Authors
Priya Nathan
Technical Contributors
and Reviewers
Josephine Turner
Martin Alvarez
Anna Atkinson
Don Bates
Marco Berbeek
Andrew Brannigan
Laszlo Czinkoczki
Michael Gerlach
Sharon Gray
Rosita Hanoman
Mozhe Jalali
Sarah Jones
Charbel Khouri
Christopher Lawless
Diana Lorentz
Nina Minchen
Cuong Nguyen
Daphne Nougier
Patrick Odell
Laura Pezzini
Stacey Procter
Maribel Renau
Bryan Roberts
Helen Robertson
Sunshine Salmon
Casa Sharif
Bernard Soleillant
Craig Spoonemore
Ruediger Steffan
Karla Villasenor
Andree Wheeley
Lachlan Williams
Publisher
Sheryl Domingue
I Introduction
Course Objectives I-2
Course Overview I-3
1 Using SET Operators
Objectives 1-2
The SET Operators 1-3
Tables Used in This Lesson 1-4
The UNION Operator 1-7
Using the UNION Operator 1-8
The UNION ALL Operator 1-10
Using the UNION ALL Operator 1-11
The INTERSECT Operator 1-12
Using the INTERSECT Operator 1-13
The MINUS Operator 1-14
SET Operator Guidelines 1-16
The Oracle Server and SET Operators 1-17
Matching the SELECT Statements 1-18
Controlling the Order of Rows 1-20
Summary 1-21
Practice 1 Overview 1-22
2 Oracle9i Datetime Functions
Objectives 2-2
TIME ZONES 2-3
Oracle9i Datetime Support 2-4
TZ_OFFSET 2-6
CURRENT_DATE 2-8
CURRENT_TIMESTAMP 2-9
LOCALTIMESTAMP 2-10
DBTIMEZONE and SESSIONTIMEZONE 2-11
EXTRACT 2-12
TIMESTAMP Conversion Using FROM_TZ 2-13
STRING To TIMESTAMP Conversion Using TO_TIMESTAMP and
TO_TIMESTAMP_TZ 2-14
Time Interval Conversion with TO_YMINTERVAL 2-15
Summary 2-16
Practice 2 Overview 2-17
Contents
iii
3 Enhancements to the GROUP BY Clause
Objectives 3-2
Review of Group Functions 3-3
Review of the GROUP BY Clause 3-4
Review of the HAVING Clause 3-5
GROUPBYwithROLLUPandCUBEOperators 3-6
ROLLUP Operator 3-7
ROLLUP Operator Example 3-8
CUBE Operator 3-9
CUBE Operator: Example 3-10
GROUPING Function 3-11
GROUPING Function: Example 3-12
GROUPING SETS 3-13
GROUPING SETS: Example 3-15
Composite Columns 3-17
Composite Columns: Example 3-19
Concatenated Groupings 3-21
Concatenated Groupings Example 3-22
Summary 3-23
Practice 3 Overview 3-24
4 Advanced Subqueries
Objectives 4-2
What Is a Subquery? 4-3
Subqueries 4-4
Using a Subquery 4-5
Multiple-Column Subqueries 4-6
Column Comparisons 4-7
Pairwise Comparison Subquery 4-8
Nonpairwise Comparison Subquery 4-9
Using a Subquery in the FROM Clause 4-10
Scalar Subquery Expressions 4-11
Scalar Subqueries: Examples 4-12
Correlated Subqueries 4-14
Using Correlated Subqueries 4-16
Using the EXISTS Operator 4-18
Using the NOT EXISTS Operator 4-20
Correlated UPDATE 4-21
The WITH Clause 4-26
WITH Clause: Example 4-27
Summary 4-29
Practice 4 Overview 4-31
iv
5 Hierarchical Retrieval
Objectives 5-2
Sample Data from the EMPLOYEES Table 5-3
Natural Tree Structure 5-4
Hierarchical Queries 5-5
Walking the Tree 5-6
Walking the Tree: From the Bottom Up 5-8
Walking the Tree: From the Top Down 5-9
Ranking Rows with the LEVEL Pseudocolumn 5-10
Formatting Hierarchical Reports Using LEVEL and LPAD 5-11
Pruning Branches 5-13
Summary 5-14
Practice 5 Overview 5-15
6 Oracle9i Extensions to DML and DDL Statements
Objectives 6-2
Review of the INSERT Statement 6-3
Review of the UPDATE Statement 6-4
Overview of Multitable INSERT Statements 6-5
Types of Multitable INSERT Statements 6-7
Multitable INSERT Statements 6-8
Unconditional INSERT ALL 6-10
Conditional INSERT ALL 6-11
Conditional FIRST INSERT 6-13
Pivoting INSERT 6-15
External Tables 6-18
Creating an External Table 6-19
Example of Creating an External Table 6-20
Querying External Tables 6-23
CREATE INDEX with CREATE TABLE Statement 6-24
Summary 6-25
Practice 6 Overview 6-26
A Practice Solutions
B Table Descriptions and Data
C Writing Advanced Scripts
D Oracle Architectural Components
Index
Additional Practices
Additional Practice Solutions
Additional Practices: Table Descriptions and Data
v