0% found this document useful (0 votes)
15 views

Mastering Oracle SQL 2nd Edition Mishra pdf download

The document provides information about various Oracle SQL ebooks available for download at ebookultra.com, including titles like 'Mastering Oracle SQL' and 'Oracle PL/SQL Programming'. It includes links to download specific editions and mentions additional resources from O'Reilly Media related to Oracle and SQL. The document also outlines the structure and contents of the 'Mastering Oracle SQL, Second Edition' book, authored by Sanjay Mishra and Alan Beaulieu.

Uploaded by

sagotblfkih
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Mastering Oracle SQL 2nd Edition Mishra pdf download

The document provides information about various Oracle SQL ebooks available for download at ebookultra.com, including titles like 'Mastering Oracle SQL' and 'Oracle PL/SQL Programming'. It includes links to download specific editions and mentions additional resources from O'Reilly Media related to Oracle and SQL. The document also outlines the structure and contents of the 'Mastering Oracle SQL, Second Edition' book, authored by Sanjay Mishra and Alan Beaulieu.

Uploaded by

sagotblfkih
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Download the full version and explore a variety of ebooks

or textbooks at https://ebookultra.com

Mastering Oracle SQL 2nd Edition Mishra

_____ Tap the link below to start your download _____

https://ebookultra.com/download/mastering-oracle-sql-2nd-
edition-mishra/

Find ebooks or textbooks at ebookultra.com today!


We have selected some products that you may be interested in
Click the link to download now or visit ebookultra.com
for more options!.

Mastering Oracle SQL 1st Edition Sanjay Mishra

https://ebookultra.com/download/mastering-oracle-sql-1st-edition-
sanjay-mishra/

Oracle SQL Plus the definitive guide 2nd ed Edition


Jonathan Gennick

https://ebookultra.com/download/oracle-sql-plus-the-definitive-
guide-2nd-ed-edition-jonathan-gennick/

Pro Oracle SQL Expert s Voice in Oracle 1st Edition Karen


Morton

https://ebookultra.com/download/pro-oracle-sql-expert-s-voice-in-
oracle-1st-edition-karen-morton/

Mastering Oracle Scheduler in Oracle 11g Databases 1st


Edition Ronald Rood

https://ebookultra.com/download/mastering-oracle-scheduler-in-
oracle-11g-databases-1st-edition-ronald-rood/
Oracle PL SQL Programming Sixth Edition Steven Feuerstein

https://ebookultra.com/download/oracle-pl-sql-programming-sixth-
edition-steven-feuerstein/

Beginning Oracle SQL 3rd Edition Lex De Haan

https://ebookultra.com/download/beginning-oracle-sql-3rd-edition-lex-
de-haan/

Oracle SQL Loader the definitive guide 1st Edition


Jonathan Gennick

https://ebookultra.com/download/oracle-sql-loader-the-definitive-
guide-1st-edition-jonathan-gennick/

Oracle SQL tuning pocket reference 1st ed Edition Mark


Gurry

https://ebookultra.com/download/oracle-sql-tuning-pocket-
reference-1st-ed-edition-mark-gurry/

Exper t Oracle SQL Optimization Deployment and Statistics


1st Edition Tony Hasler (Auth.)

https://ebookultra.com/download/exper-t-oracle-sql-optimization-
deployment-and-statistics-1st-edition-tony-hasler-auth/
Mastering Oracle SQL 2nd Edition Mishra Digital
Instant Download
Author(s): Mishra, Sanjay;Beaulieu, Alan
ISBN(s): 9780596006327, 0596006322
Edition: 2
File Details: PDF, 3.38 MB
Year: 2004
Language: english
Mastering Oracle SQL
Other Oracle and SQL resources from O’Reilly
Related titles Oracle in a Nutshell SQL Tuning
SQL Pocket Guide Oracle PL/SQL Programming
Oracle SQL*Plus: The Defini- Oracle Essentials: Oracle 9i,
tive Guide Oracle9, and Oracle8i
Oracle PL/SQL Language Oracle SQL*Plus Pocket
Pocket Reference Reference
TOAD Pocket Reference for Learning Oracle PL/SQL
Oracle

Oracle Books oracle.oreilly.com is a complete catalog of O’Reilly’s books on


Resource Center Oracle and related technologies, including sample chapters and
code examples.

oreillynet.com is the essential portal for developers interested in


open and emerging technologies, including new platforms, pro-
gramming languages, and operating systems.

Conferences O’Reilly brings diverse innovators together to nurture the ideas


that spark revolutionary industries. We specialize in document-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit con-
ferences.oreilly.com for our upcoming events.

Safari Bookshelf (safari.oreilly.com) is the premier online refer-


ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today with a free trial.
SECOND EDITION

Mastering Oracle SQL

Sanjay Mishra and Alan Beaulieu

Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo


Mastering Oracle SQL, Second Edition
by Sanjay Mishra and Alan Beaulieu

Copyright © 2004, 2002 O’Reilly Media, Inc. All rights reserved.


Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti-
tutional sales department: (800) 998-9938 or corporate@oreilly.com.

Editor: Jonathan Gennick


Production Editor: Matt Hutchinson
Production Services: Octal Publishing, Inc.
Cover Designers: Ellie Volckhausen and Emma Colby
Interior Designer: Melanie Wang

Printing History:
April 2002: First Edition.
July 2004: Second Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Mastering Oracle SQL, the image of lantern flies, and related trade dress are
trademarks of O’Reilly Media, Inc.

Oracle® and all Oracle-based trademarks and logos are trademarks or registered trademarks of Oracle
Corporation in the United States and other countries. O’Reilly Media, Inc., is independent of Oracle
Corporation.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher and authors
assume no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.

This book uses RepKover™, a durable and flexible lay-flat binding.

ISBN-10: 0-596-00632-2
ISBN-13: 978-0-596-00632-7
[M] [7/07]
I dedicate this book to my father.
I wish he were alive to see this book.
—Sanjay Mishra

To my daughters, Michelle and Nicole.


—Alan Beaulieu
Table of Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

1. Introduction to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is SQL? 1
A Brief History of SQL 3
A Simple Database 4
DML Statements 5
So Why Are There 17 More Chapters? 17

2. The WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18


Life Without WHERE 18
WHERE to the Rescue 19
WHERE Clause Evaluation 20
Conditions and Expressions 23
WHERE to Go from Here 30

3. Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
What Is a Join Query? 31
Join Conditions 32
Types of Joins 37
Joins and Subqueries 53
DML Statements on a Join View 53

4. Group Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Aggregate Functions 62
The GROUP BY Clause 66
The HAVING Clause 74
Nested Group Operations 77

vii
5. Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
What Is a Subquery? 80
Noncorrelated Subqueries 81
Correlated Subqueries 88
Inline Views 91
Subquery Case Study: The Top N Performers 106

6. Handling Temporal Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112


Time Zones 112
Temporal Data Types in Oracle 114
Literals of Temporal Types 120
Getting Temporal Data In and Out of a Database 126
Date and Time Formats 134
Manipulating Temporal Data 146

7. Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166


Set Operators 167
Precedence of Set Operators 172
Comparing Two Tables 173
Using NULLs in Compound Queries 176
Rules and Restrictions on Set Operations 178

8. Hierarchical Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181


Representing Hierarchical Information 181
Simple Hierarchy Operations 184
Oracle SQL Extensions 187
Complex Hierarchy Operations 193
Restrictions on Hierarchical Queries 204
Enhancements in Oracle Database 10g 204

9. DECODE and CASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210


DECODE, NULLIF, NVL, and NVL2 210
The Case for CASE 214
DECODE and CASE Examples 217

10. Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230


Partitioning Concepts 230
Partitioning Tables 231
Partitioning Indexes 231

viii | Table of Contents


Partitioning Methods 232
Specifying Partitions 238
Partition Pruning 239

11. PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243


What Is PL/SQL? 243
Procedures, Functions, and Packages 244
Calling Stored Functions from Queries 246
Restrictions on Calling PL/SQL from SQL 251
Stored Functions in DML Statements 254
The SQL Inside Your PL/SQL 256

12. Objects and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258


Object Types 258
Collection Types 267
Collection Instantiation 268
Querying Collections 269
Collection Unnesting 270
Collection Functions 271
Comparing Collections 273
Manipulating Collections 275
Multilevel Collections 276

13. Advanced Group Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279


Multiple Summary Levels 279
Pushing the GROUPING Envelope 301
The GROUPING_ID and GROUP_ID Functions 311

14. Advanced Analytic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320


Analytic SQL Overview 320
Ranking Functions 326
Windowing Functions 339
Reporting Functions 346
Summary 351

15. SQL Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352


Know When to Use Specific Constructs 352
Avoid Unnecessary Parsing 358
Consider Literal SQL for Decision-Support Systems 363

Table of Contents | ix
16. XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
What Is XML? 365
Storing XML Data 366
Generating XML Documents 382
Summary 387

17. Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389


Elementary Regular Expression Syntax 389
Advanced Function Options 401
Advanced Regular Expression Syntax 405

18. Model Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417


Basic Elements of a Model Query 417
Cell References 419
Rules 427
Iterative Models 442
Reference Models 445

Appendix: Oracle’s Old Join Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455

x | Table of Contents
Preface

SQL is the language for accessing a relational database. SQL provides a set of state-
ments for storing and retrieving data to and from a relational database. It has gained
steadily in popularity ever since the first relational database was unleashed upon the
world. Other languages have been put forth, but SQL is now accepted as the stan-
dard language for almost all relational database implementations, including Oracle.
SQL is different from other programming languages because it is nonprocedural.
Unlike programs in other languages, where you specify the sequence of steps to be
performed, a SQL program (more appropriately called a SQL statement) only
expresses the desired result. The responsibility for determining how the data will be
processed to generate the desired result is left to the database management system.
The nonprocedural nature of SQL makes it easier to access data in application
programs.
If you are using an Oracle database, SQL is the interface you use to access the data
stored in your database. SQL allows you to create database structures such as tables
(to store your data), views, and indexes. SQL allows you to insert data into the data-
base, and to retrieve that stored data in a desired format (for example, you might sort
it). Finally, SQL allows you to modify, delete, and otherwise manipulate your stored
data. SQL is the key to everything you do with the database. It’s important to know
how to get the most out of that interface. Mastery over the SQL language is one of
the most vital requirements of a database developer or database administrator.

Why We Wrote This Book


Our motivation for writing this book stems from our own experiences learning how
to use the Oracle database and Oracle’s implementation of the SQL language. Ora-
cle’s SQL documentation consists of a reference manual that doesn’t go into details
about the practical usefulness of the various SQL features that Oracle supports. Nor
does the manual present complex, real-life examples.

xi

This is the Title of the Book, eMatter Edition


Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
When we looked for help with SQL in the computer book market, we found that
there are really two types of SQL books available. Most are the reference type that
describe features and syntax, but that don’t tell you how to apply that knowledge to
real-life problems. The other type of book, very few-in-number, discusses the
application of SQL in a dry and theoretical style without using any particular ven-
dor’s implementation. Since every database vendor implements their own variation
of SQL, we find books based on “standard” SQL to be of limited usefulness.
In writing this book, we decided to write a practical book focused squarely on Ora-
cle’s version of SQL. Oracle is the market-leading database, and it’s also the data-
base on which we’ve honed our SQL expertise. In this book, we not only cover the
most important and useful of Oracle’s SQL features, but we show ways to apply
them to solve specific problems.

What’s New in Oracle SQL?


When we wrote the first edition of this book, Oracle9i had just come out, and we
managed to cover some of the interesting and new features in that release of the data-
base. Now, Oracle Database 10g has just been released, and there are even more new
features to talk about:
• A new, MODEL clause has been added to the SELECT statement, enabling you
to write queries that perform spreadsheet-like calculations against multidimen-
sional arrays created from data you select from the database.
• Oracle has added support for using regular expressions from SQL, and with a
vengeance. Not only can you use regular expressions to select data, but also to
manipulate data in various, useful ways. For example, you can perform regular
expression search-and-replace operations. No other database vendor that we
know of offers such powerful, regular expression functionality.
• XML is everywhere these days, and that hasn’t gone unnoticed in the world of
SQL. The ANSI/ISO folk have created the SQL/XML standard, which defines
mechanisms for selecting relational data and presenting it in XML form. Oracle
supports this standard, which involves several, new SQL functions. Oracle also
now supports XML as a native data type.
These are just the big features, which, of course, we cover in this second edition. In
addition, we cover many small updates to Oracle SQL, such as the multiset union
operators that enable you to perform set operations involving nested table collections.
Finally, we’ve worked carefully together as a team, not only with each other, but also
with our editor, to ensure that all examples in this book are drawn from a single data
set. You’ll be able to download that data set from this book’s catalog page. You can
then use it to follow along with our examples.

xii | Preface

This is the Title of the Book, eMatter Edition


Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Objectives of This Book
The single most important objective of this book is to help you harness the power of
Oracle SQL to the maximum extent possible. You will learn to:
• Understand the features and capabilities of the SQL language, as implemented
by Oracle.
• Use complex SQL features, such as outer joins, correlated subqueries, hierarchi-
cal queries, grouping operations, and analytical queries.
• Use DECODE and CASE to implement conditional logic in your SQL queries.
• Write SQL statements that operate against partitions, objects, and collections,
such as nested tables and variable arrays.
• Use the new SQL features introduced in Oracle Database 10g, such as regular
expressions and interrow calculations.
• Use best-practices to write efficient, maintainable SQL queries.
One topic that is important to us and many of our readers, but which is not explic-
itly discussed in this book is SQL tuning. Tuning tips are sprinkled throughout the
book, but we do not include a chapter on tuning for the following reasons:
• Tuning is a large topic, and reasonable coverage of SQL tuning would easily
double or triple the size of this book.
• There are already many excellent Oracle-specific and general-purpose tuning
books on the market, whereas there are very few books (in our opinion, exactly
one) that thoroughly explore the feature set of Oracle SQL.
• In many ways, mastery of Oracle’s SQL implementation is the most important
tool in your tuning toolkit.
With this book under your belt, you will be less likely to write SQL statements that
perform badly, and you will be able to employ multiple strategies to rework existing
statements.

Audience for This Book


This book is for Oracle developers, database administrators, and anyone who needs
access to data stored in an Oracle database for reporting or ad-hoc analysis. Whether
you are new to the world of databases or a seasoned professional, if you use SQL to
access an Oracle database, this book is for you. Whether you use simple queries to access
data or embed them in PL/SQL or Java programs, SQL is the core of all data access
tasks in your application. Knowing the power and flexibility of SQL will improve
your productivity, allowing you to get more done in less time, and with increased
certainty that the SQL statements you write are indeed correct.

Preface | xiii

This is the Title of the Book, eMatter Edition


Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Exploring the Variety of Random
Documents with Different Content
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
back
back
back
back
back
back
back
back
back
back
back
back
back
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like