About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Effective SQL
61 Specific Ways to Write Better SQL
John L. Viescas
Douglas J. Steele
Ben G. Clothier
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2016955468
Copyright 2017 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
Some of the examples used in this book originally appeared in SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL, Third Edition (Addison-Wesley, 2014). These examples appear with permission from the authors and Pearson Education Inc.
ISBN-13: 978-0-13-457889-7
ISBN-10: 0-13-457889-9
1 16
Editor-in-Chief
Greg Wiegand
Senior Acquisitions Editor
Trina MacDonald
Development Editor
Songlin Qiu
Technical Reviewers
Richard Anthony Broersma Jr.
Craig S. Mullins
Vivek Sharma
Dave Stokes
Morgan Tocker
Managing Editor
Sandra Schroeder
Full-Service Production Manager
Julie B. Nahil
Project Editor
Anna Popick
Copy Editor
Barbara Wood
Indexer
Richard Evans
Proofreader
Anna Popick
Editorial Assistant
Olivia Basegio
Cover Designer
Chuti Prasertsith
Compositor
The CIP Group
Praise for Effective SQL
Given the reputation of the authors, I expected to be impressed. Impressed doesnt cover it, though. I was blown away! Most SQL books tell you how. This one tells you why. Most SQL books separate database design from implementation. This one integrates design considerations into every facet of SQL use. Most SQL books sit on my shelf. This one will live on my desk.
Roger Carlson, Microsoft Access MVP (20062015)
It can be easy to learn the basics of SQL, but it is very difficult to build accurate and efficient SQL, especially for critical systems with complex requirements. But now, with this great new book, you can get up to speed and write effective SQL much more quickly, no matter which DBMS you use.
Craig S. Mullins, Mullins Consulting, Inc., DB2 Gold Consultant and IBM Champion for Analytics
This is a great book. It is written in language that can be understood by a relative beginner and yet contains tips and tricks that will benefit the most hardened workhorse. It will therefore appeal to readers across the whole range of expertise and should be in the library of anybody who is seriously concerned with designing, managing, or programming databases.
Graham Mandeno, database consultant and Microsoft MVP (19962015)
This book is an excellent resource for database designers and developers working with relational and SQL-based databasesits an easy read with great examples that combine theory with practical examples seamlessly. Examples for top relational databases Oracle, DB2, SQL Server, MySQL, and PostgreSQL are included throughout. The book walks the reader through sophisticated techniques to deal with things such as hierarchical data and tally tables, along with explanations of the inner workings and performance implications of SQL using GROUP BY
, EXISTS
, IN
, correlated and non-correlated subqueries, window functions, and joins. The tips you wont find anywhere else, and the fun examples help to make this book stand out from the crowd.
Tim Quinlan, database architect and Oracle Certified DBA
This book is good for those who need to support multiple dialects of SQL. Its divided up into stand-alone items that you just grab and go. I have been doing SQL in various flavors since 1992 and even I picked up a few things.
Tom Moreau, Ph.D., SQL Server MVP (20012012)
This book is a powerful, compact, and easily understandable presentation of how to use SQLit shows the application of SQL to real-world questions in order to teach the construction of queries, and it explains the relationship of how data is stored to how data is queried so that you obtain results successfully and effectively.
Kenneth D. Snell, Ph.D., database consultant and former Microsoft Access MVP
It has been problematic for many that there is no book on going from a novice database administrator to a much more advanced status until now. Effective SQL is a road map, a guide, a Rosetta Stone, and a coach on moving from basic Structured Query Language (SQL) to much more advanced uses to solve real-world problems. Rather than stumble around reinventing the wheel or catching glimpses of the proper ways to use a database, do yourself a favor and buy a copy of this book. Not only will you see many different approaches it would take years to see as a database consultant, but you will get a detailed understanding of why the databases of many vendors do what they do. Save time, effort, and wear and tear on your walls from banging your head against them and get this book.