Oracle SQL Tricks and Workarounds : Expert Guide to Oracle SQL Excellence
Language: English
Published by Authorhouse, 2011
- Softcover
- New

Seller: GreatBookPrices, Columbia, MD, U.S.A.GreatBookPrices
AbeBooks seller since April 6, 2009
Condition: New
£ 33.04
Quantity: Over 20 available
Add to basketSeller Inventory # 12793689-n
- Title
- Oracle SQL Tricks and Workarounds : Expert Guide to Oracle SQL Excellence
- Author
- Hilkevich, Zahar
- Publisher
- Authorhouse
- Publication year
- 2011
- Condition
- New
- Binding
- Soft cover
- Language
- English
- ISBN 10
- 1463421370
- ISBN 13
- 9781463421373
"Synopsis" may belong to another edition of this title.
Excerpt. © Reprinted by permission. All rights reserved.
Oracle SQL Tricks and Workarounds
By Zahar HilkevichAuthorHouse
Copyright © 2011 Zahar HilkevichAll right reserved.
ISBN: 978-1-4634-2137-3
Contents
Chapter 1: Terms and Definitions..........................................1Chapter 2: First approach to the first problem............................13Chapter 3: Method Substitution............................................23Chapter 4: Introducing Problem/Technique Matrix (PTM).....................50Chapter 5: Extending the PTM..............................................77Chapter 6: Problem Patterns...............................................98Chapter 7: Benefit with the vendor........................................115Chapter 8: Handicap Challenge.............................................154Chapter 9: Solution patterns..............................................172Chapter 10: Substitution Drill............................................189Chapter 11: Writing DELETE and UPDATE statements..........................210Chapter 12: Mastering your SQL skills.....................................230
Chapter One
Terms and Definitions.This book introduces a relatively new approach to mastering one's Oracle SQL skills. This book will teach you how to leverage your existing Oracle SQL knowledge as well as how you can benefit from a variety of SQL tricks and techniques we present thereafter. This is a text book rather than a reference, and it aims to teach you how to become a better SQL specialist. Even though the recommendations found in this book may be applied to a variety of SQL flavors, Oracle SQL is the main subject of this book. Our goal was not to impress you with clever tricks and sophisticated techniques, but rather give you a roadmap to excellence in writing Oracle SQL queries. No doubt, this book presents tricks and classy approaches, which still serve the main goal – to let you master your Oracle SQL skills.
Traditionally, when someone explains a trick or technique, a specific category of problems is targeted; it usually demonstrates how the trick or technique can solve the problem and the goal is considered accomplished. When we try solving a set of problems we often operate with the following concepts: a problem, a solution, and a method (employed by solutions). A method can represent a trick or technique, which we will define in a few paragraphs below. Usually, we are satisfied when a problem gets solved one way or another and there is little need to introduce a new concept within the domain we described. It is easy to understand that the value of a trick or technique in any technology can be measured by the number of different problems the method helps to solve. Throughout their career, a software developer acquires a number of such techniques that signifies his or her professional experience. Our idea is to show a different angle in the way we can acquire professional skills. Instead of only focusing on the number of different techniques we know, we emphasize a number of different problems or types of problems we are familiar with, and also a number of different ways in which we can solve all those problems. In other words, we encourage researching different types of problems and finding numerous solutions for each of them.
One of the main goals of this book is to explore and reveal the potential of many well known and lesser known techniques. Any technique is better understood if it can be widely applied. This book will demonstrate numerous applications of traditional SQL features in situations where those features were not expected to be applicable.
During the work on this book we faced a small challenge with respect to how to refer to a set of alternative solutions to a specific problem. This is how we came across the term WORKAROUND which we defined as follows:
We call the family of distinct solutions for a given problem the workarounds for that problem.
Since the terminology we use in this book may differ from what you have used before we will take a moment to define a few more terms.
We define an SQL technique as a general problem solving strategy that can usually be applied to a fairly broad set of problems.
An SQL trick is a method that's more specific than a technique – it may not apply to as many types of problems but it is usually more clever and obscure than a technique, and can sometimes be used to solve more stubborn problems.
Classifying solutions as tricks vs. techniques is a subjective process (the line can be blurry), but for us this distinction has been proven useful.
Roadmap of the book
The first chapter makes is an introduction into the methodology of producing multiple solutions for a given SQL problem. Generally speaking, any set of numerous solutions begins with a first solution. Some useful tactics on finding the very first solution are presented in Chapter 2. When you found your very first solution, in reality, you already "booked" a whole set of workarounds that can be derived with a help of Method Substitution, described in Chapter 3. Each chapter will present more and more SQL nuances that will help you derive more and more workarounds. With the growing number of found solutions, it becomes challenging to keep your knowledge well organized; Chapter 4 introduces a Problem Technique Matrix (PTM) – a neat model to manage your problems, techniques, and actual workarounds. Chapter 5 extends the PTM concept and explains how it can be best used. Simple statistical analysis of the PTM content leads us to a concept of Problem Patterns that provides a great practical payoff; Chapter 6 describes Problem Patterns in details. Every new Oracle release comes with a handful of very useful new features which equates to more new techniques one should try for finding new solutions – see Chapter 7 for more details on that. New features often offer new and simpler approaches to variety of problems. When developers get used to new techniques, they often forget that in previous versions they survived without them. Chapter 8 presents several handicap challenges, including the one we have just pointed to – "How did we do this before"? When developers start working on a new query, they are supposed to know all related business entities, relationships, and other rules. This is not the case when they have to optimize somebody else's request. Chapter 9 demonstrates possible approaches for such circumstances. Nothing can speed up one's professional progress better than extensive practice. Chapter 10 offers a comprehensive drill based on Method Substitution and other techniques reviewed earlier in the book. Data Manipulation commands are not limited to SELECT statement only. Chapter 11 shows how to apply the knowledge accumulated from Chapters 1 through 10 to DELETE and UPDATE commands. The last chapter of the book suggests further ways of improving SQL practical skills.
Why bother looking for workarounds?
SQL is a declarative language (unlike procedural languages, such as C, Java, Visual Basic, etc) hence it allows multiple equivalent ways to write database requests. Sometimes, two logically identical SQL statements look completely different and only SQL experts can recognize a common pattern. We will not be discussing correct and incorrect ways of writing SQL statements, but rather teaching you how to find different ways to write such statements.
Our goal is to enable our readers to become SQL gurus quickly, by teaching specific sample techniques and tricks, and helping them enhance their approach to SQL problems.
For application developers who stop at the first solution to a SQL problem, gaining proficiency is a slow process requiring a huge number of problems to accumulate the requisite experience.
For those willing to try a more direct path, investing additional time upfront on multiple workarounds for each problem will help build your skills more quickly.
One of the benefits of knowing the workarounds is obvious: when you know multiple approaches the likelihood of picking the most suitable or efficient approach is higher than in the situation when you only know one way. Knowledge of all the ins and outs of a specific problem not only gives you a deep understanding of the problem, but also trains you to see the big picture and become more flexible in your decisions.
The good news is that as we will show in this book, it is not that difficult to learn to find workarounds. Over the years of our development and teaching experience, we have built a framework that simplifies the process of finding the workarounds.
Who Should Read This Book
If you, like the author of this book, value every opportunity to extend your collection of challenging problems, tricks, and techniques, this book is for you. Software application developers, managers, database developers and administrators can all find something new and exciting in this book. By any means, this is not a beginner's manual; however, college students taking advanced database classes may also greatly benefit from this book. Casual database programmers will not find this book very handy and we did not target them when writing this book.
Prerequisites. Why Execution Plan?
We assume that our reader have basic knowledge of Oracle SQL as well as the SQL*Plus environment. If you are new to Oracle, we suggest you read any introductory book on Oracle SQL, and then come back to enjoy our journey to Oracle SQL insights. Throughout the book we will use Oracle's famous built-in schema "SCOTT" with tables "EMP" and "DEPT". We took an effort to rephrase numerous problems we collected for this book in terms of those two tables. This approach should make it easy to try all suggested solutions in this book since the "SCOTT" schema is either already available in your Oracle database or it can easily be created since Oracle supplies the script to build it. We also assume that you are familiar with a concept of a Query Execution Plan.
Execution Plans play a central role in our book. Since we refer to workarounds as a set of distinct solutions to a specific problem, we need to develop criteria to consider two or more solutions distinct.
To illustrate the point let's take a look at the following simple problem.
Problem: Find all employees who were hired in 1981.
We expect the following dataset to be returned as a correct solution:
ENAME HIREDATE
ALLEN 20-FEB-81 WARD 22-FEB-81 JONES 02-APR-81 MARTIN 28-SEP-81 BLAKE 01-MAY-81 CLARK 09-JUN-81 KING 17-NOV-81 TURNER 08-SEP-81 JAMES 03-DEC-81 FORD 03-DEC-81
Let's consider the following alternative solution of this problem and execution plans generated by autotrace option (SET autotrace ON EXPLAIN command):
Solution #1.
SELECT ename, hiredate FROM emp WHERE hiredate BETWEEN TO_DATE('01-JAN-1981', 'DD-MON-YYYY') AND TO_DATE('31-DEC-1981', 'DD-MON-YYYY')
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 11 | 154 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 11 | 154 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("HIREDATE"<=TO_DATE(' 1981-12-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "HIREDATE">=TO_DATE(' 1981-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
Solution #2.
SELECT ename, hiredate FROM emp WHERE hiredate >= TO_DATE('01-JAN-1981', 'DD-MON-YYYY') AND hiredate <= TO_DATE('31-DEC-1981', 'DD-MON-YYYY')
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | 11 | 154 | 3 (0) | 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 11 | 154 | 3 (0) | 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("HIREDATE"<=TO_DATE(' 1981-12-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "HIREDATE">=TO_DATE(' 1981-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
Solution #3.
SELECT ename, hiredate FROM emp WHERE hiredate LIKE '%81'
Comment: one assumption is made here – all employees were hired in 20th century.
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 14 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter(INTERNAL_FUNCTION("HIREDATE") LIKE '%81')
Solution #4.
SELECT ename, hiredate FROM emp WHERE TO_CHAR(hiredate, 'YYYY')='1981'
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 14 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter(TO_CHAR(INTERNAL_FUNCTION("HIREDATE"),'YYYY')='1981')
Solution #5.
SELECT ename, hiredate FROM emp
WHERE INSTR(TO_CHAR(hiredate, 'DD-MON-YYYY'),'-1981')>0
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | 1 | 14 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 14 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter(INSTR(TO_CHAR(INTERNAL_FUNCTION("HIREDATE"),'DD-MON-YYYY') , '-1981')>0)
As you can check, all queries return correct results. It is also obvious that it would be naïve to claim that we have presented five different solutions of the problem, even though they all look different. In our opinion, textual representation of a query cannot be used as proof of a solution's uniqueness. Since Oracle supports in-line views, we can always use any existing solution to derive more by using the following simple technique:
SELECT * FROM () Let's say, to derive Solution #6, we can take Solution #1 and transform it into an inline view: SELECT * FROM (SELECT ename, hiredate FROM emp WHERE hiredate BETWEEN '01-JAN-1981' AND '31-DEC-1981') Apparently, this query does exactly the same as its subquery part, even though it looks somewhat different; hence, we cannot qualify it as a distinct solution. When working on this book, we needed to define an objective criterion for solution distinction, so we decided to employ Oracle's query execution plan. We qualify a workaround as distinct if it causes the database to execute the query differently compared to other solutions. When we come up with a candidate solution, we examine its execution plan against the other solutions found so far. If it is different, we consider what we have found a new, distinct workaround; otherwise, we have only found a workaround equivalent to one of the other workarounds, which we also refer to as a synonymous workaround. Note that based on this definition, the qualification of a workaround is dependent on the database we are using and its configuration. The Oracle database engine may use two different execution plans for the same SQL statement if it is ran on two different servers. This is especially true when you compare execution plans generated by different versions and editions of Oracle server. Using an Execution Plan as an indicator, we have found that among five presented solutions we have no difference in execution plan operation sequence; all execution plans indicate full scan on EMP table. Taking a close look at the execution plan will reveal that all five execution plans have identical plan hash value. This suggests that plan hash value can be used for comparing two or more different workaround candidates. From this point on we will be ultimately using it for workaround classification. SQL*Plus autotrace option has several output formats; one of them is a theoretical execution plan (via explain plan). In older releases of SQL*Plus, autotrace had its own formatting for explain plan, but effective release 10.2, it uses the now-standard DBMS_XPLAN package. (Continues...) Excerpted from Oracle SQL Tricks and Workaroundsby Zahar Hilkevich Copyright © 2011 by Zahar Hilkevich. Excerpted by permission of AuthorHouse. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
"About the title" may belong to another edition of this title.
GreatBookPrices
Columbia, MD, U.S.A.
AbeBooks seller since April 6, 2009
Shipping rates within U.S.A.
| Item | 5 to 14 business days | 8 to 14 business days |
|---|---|---|
| First item | £ 1.95 | £ 1.95 |
Payment methods
Store description
SuperBookDeals.com is your top source for finding new books at the absolute lowest prices, guaranteed ! We offer big discounts - everyday - on millions of titles in virtually any category, from Architecture to Zoology -- and everything in between. Discover great deals and super-savings, on professional books, text book titles, the newest computer guides, or your favorite fiction authors. You'll find it all - at HUGE SAVINGS - at SuperBookDeals. Browse through our complete online product catalog today. Serving customers around the world for years, we help thousands find just the books they're looking for -- at incredibly low, bargain prices.…
Seller's business information
Expert Trading Limited
9220 Rumsey Road, Suite 101
Columbia, MD U.S.A. 21045
Terms of sale
Company Name: GreatBookPrices
Legal Entity: Expert Trading, LLC
Address: 6310 Stevens Forest, suite 200, Columbia MD 21046
Email address: CustomerService@SuperBookDeals.com
Phone number: 410-964-0026
consumer complaints can be addressed to address above
Registration #: 52-1713923
Authorized representative: Danielle Hainsey
Right of withdrawal
If you are a consumer you can withdraw from the contract in accordance with the following. Consumer means any natural person who is acting for purposes which are outside his trade, business, craft or profession.
Information regarding the right of withdrawal
Statutory right to withdraw
You have the right to withdraw from this contract within 14 days without giving any reason.
The withdrawal period will expire after 14 days from the day on which you acquire, or a third party other than the carrier and indicated by you acquires, physical possession of the last good or the last lot or piece.
To exercise the right of withdrawal, electronically fill in and submit a clear statement on our website, under "My Purchases" in "My Account". We will communicate to you an acknowledgement of receipt of such a withdrawal on a durable medium (e.g. by e-mail) without delay.
To meet the withdrawal deadline, it is sufficient for you to send your communication concerning your exercise of the right of withdrawal before the withdrawal period has expired.
Effects of withdrawal
If you withdraw from this contract, we will reimburse to you all payments received from you, including the costs of delivery (except for the supplementary costs arising if you chose a type of delivery other than the least expensive type of standard delivery offered by us).
We may make a deduction from the reimbursement for loss in value of any goods supplied, if the loss is the result of unnecessary handling by you.
We will make the reimbursement without undue delay, and not later than 14 days after the day on which we are informed about your decision to withdraw from this contract.
We will make the reimbursement using the same means of payment as you used for the initial transaction, unless you have expressly agreed otherwise; in any event, you will not incur any fees as a result of such reimbursement.
We may withhold reimbursement until we have received the goods back, or you have supplied evidence of having sent back the goods, whichever is the earliest.
You shall send back the goods or hand them over to GreatBookPrices, Bensenville, Illinois, U.S.A., without undue delay and in any event not later than 14 days from the day on which you communicate your withdrawal from this contract to us. The deadline is met if you send back the goods before the period of 14 days has expired. You will have to bear the direct cost of returning the goods. You are only liable for any diminished value of the goods resulting from the handling other than what is necessary to establish the nature, characteristics and functioning of the goods.
Exceptions to the right of withdrawal
The right of withdrawal does not apply to:
- The delivery of newspapers, journals or magazines with the exception of subscription contracts; and
- The supply of digital content which is not supplied on a tangible medium (e.g. on a CD or DVD) if you accepted when you placed your order that we could start to deliver it, and that you could not withdraw once delivery had started.
Shipping terms
Our warehouses across the globe are fully operational without substantial delays. We are working hard and continue to overcome the daily challenges presented by COVID-19. We appreciate your understanding.
Internal processing of your order will take about 1-2 business days. Please allow an additional 4-14 business days for Media Mail delivery. We have multiple ship-from locations - MD,IL,NJ,UK,IN,NV,TN & GA