Join Amazon Prime and ship Two-Day for free and Overnight for $3.99. Already a member? Sign in.
SQL Cookbook and over 300,000 other books are available for Amazon Kindle – Amazon’s new wireless reading device. Learn more

 

or
Sign in to turn on 1-Click ordering.
 
 
More Buying Choices
59 used & new from $11.96

Have one to sell? Sell yours here
 
   
SQL Cookbook (Cookbooks (O'Reilly))
 
 
Start reading SQL Cookbook on your Kindle in under a minute.

Don’t have a Kindle? Get yours here.
 
  

SQL Cookbook (Cookbooks (O'Reilly)) (Paperback)

by Anthony Molinaro (Author)
Key Phrases: case deptno, select adddate, select deptno, Oracle Use, Solution Use, Oracle Database (more...)
4.7 out of 5 stars See all reviews (44 customer reviews)

List Price: $39.95
Price: $26.37 & this item ships for FREE with Super Saver Shipping. Details
You Save: $13.58 (34%)
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
In Stock.
Ships from and sold by Amazon.com. Gift-wrap available.

Want it delivered Tuesday, July 21? Choose One-Day Shipping at checkout. Details
40 new from $21.80 19 used from $11.96
Also Available in: List Price: Our Price: Other Offers:
Kindle Edition (Kindle Book) $17.59
Paperback Order it used!
Like this book? Find similar titles from O'Reilly and Partners in our O'Reilly Bookstore.

Best Value

Buy Learning SQL and get SQL Cookbook (Cookbooks (O'Reilly)) at an additional 5% off Amazon.com's everyday low price.

Learning SQL + SQL Cookbook (Cookbooks (O'Reilly))
Buy Together Today: $48.12

Show availability and shipping details

  • Learning SQL

    In Stock.
    Ships from and sold by Amazon.com.
    Eligible for FREE Super Saver Shipping on orders over $25. Details

  • This item: SQL Cookbook (Cookbooks (O'Reilly))

    In Stock.
    Ships from and sold by Amazon.com.
    This item ships for FREE with Super Saver Shipping. Details


Customers Who Bought This Item Also Bought


Editorial Reviews

Product Description
You know the rudiments of the SQL query language, yet you feel you aren't taking full advantage of SQL's expressive power. You'd like to learn how to do more work with SQL inside the database before pushing data across the network to your applications. You'd like to take your SQL skills to the next level.

Let's face it, SQL is a deceptively simple language to learn, and many database developers never go far beyond the simple statement: SELECT FROM WHERE . But there is "so" much more you can do with the language. In the "SQL Cookbook," experienced SQL developer Anthony Molinaro shares his favorite SQL techniques and features. You'll learn about:

Window functions, arguably the most significant enhancement to SQL in the past decade. If you're not using these, you're missing out

Powerful, database-specific features such as SQL Server's PIVOT and UNPIVOT operators, Oracle's MODEL clause, and PostgreSQL's very useful GENERATE_SERIES function

Pivoting rows into columns, reverse-pivoting columns into rows, using pivoting to facilitate inter-row calculations, and double-pivoting a result set

"Bucketization," and why you should never use that term in Brooklyn.

How to create histograms, summarize data into buckets, perform aggregations over a moving range of values, generate running-totals and subtotals, and other advanced, data warehousing techniques

The technique of "walking a string," which allows you to use SQL to parse through the characters, words, or delimited elements of a string

Written in O'Reilly's popular Problem/Solution/Discussion style, the "SQL Cookbook" is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, weall have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The "SQL Cookbook" moves quickly from problem to solution, saving you time each step of the way.

About the Author
Anthony Molinaro is a SQL developer and database administrator with many years experience in helping developers improve their SQL queries. SQL is particular passion of Anthony's, and he's become known as the go-to guy among his clients when it comes to solving difficult SQL query problems. He's well-read, understands relational theory well, and has nine years of hands-on experience solving tough, SQL problems. Anthony is particularly well-acquainted with new and powerful SQL features such as the windowing function syntax that was added to the most recent SQL standard.


Product Details

  • Paperback: 632 pages
  • Publisher: O'Reilly Media, Inc.; 1 edition (December 15, 2001)
  • Language: English
  • ISBN-10: 0596009763
  • ISBN-13: 978-0596009762
  • Product Dimensions: 9.1 x 7 x 1.4 inches
  • Shipping Weight: 2 pounds (View shipping rates and policies)
  • Average Customer Review: 4.7 out of 5 stars See all reviews (44 customer reviews)
  • Amazon.com Sales Rank: #27,482 in Books (See Bestsellers in Books)

    Popular in these categories: (What's this?)

    #11 in  Books > Computers & Internet > Databases > Data Warehousing
    #15 in  Books > Computers & Internet > Databases > Specific Databases > MySQL & mSQL
    #22 in  Books > Computers & Internet > Databases > Data Mining

Inside This Book (learn more)


Books on Related Topics (learn more)

What Do Customers Ultimately Buy After Viewing This Item?


Tags Customers Associate with This Product

 (What's this?)
Click on a tag to find related items, discussions, and people.
Check the boxes next to the tags you consider relevant or enter your own tags in the field below.
(20)

Your tags: Add your first tag
 
Help others find this product — tag it for Amazon search
No one has tagged this product for Amazon search yet. Why not be the first to suggest a search for which it should appear?

 

Customer Reviews

44 Reviews
5 star:
 (37)
4 star:
 (4)
3 star:    (0)
2 star:
 (2)
1 star:
 (1)
 
 
 
 
 
Average Customer Review
4.7 out of 5 stars (44 customer reviews)
 
 
 
 
Share your thoughts with other customers:
Most Helpful Customer Reviews

 
42 of 42 people found the following review helpful:
4.0 out of 5 stars Excellent book on SQL and the platforms recognized in this book!, January 19, 2006
By Rob Wehrli (Knoxville, TN) - See all my reviews
(REAL NAME)   
This book is just fantastic. One really needs to read the inside cover pages to see what this book doesn't try to do. The relevant elements are:

Non-political (on several SQL-specific fronts)
Non-Pure ANSI (portability versus vendor-specific features)
Non-platform (I couldn't find a mention of a particular platform)

Basically, the book focuses on how to accomplish numerous SQL tasks using SQL and those features found on some of the top RDBMS vendors' products. It is selective in that the focus is constrained to MS SQL (platform obvious, but not mentioned that I could find) Server, DB2, MySQL, PostgresSQL and Oracle. I think that this well represents at least the 80/20 rule of who's running what RDBMS.

The book is a set of recipes for (usually) each of these platforms. Each recipe is a code-demonstrated "how-to" for accomplishing the given task. This is perfect for those who don't want to become DBAs and focus on learning "too" much about SQL, rather, want to use an example in their code to make it do the right thing.

The book is very well written and finished. It is a joy to read.

The one detraction that I can think of is that it would be nice to see all of the examples online at O'Reilly's web site so that we could "play around with them" somewhat without having to enter the database table contents and all of the SQL by hand. It "ain't that hard," but who's got the time to do it? I think that having the code available with the book would make it an excellent training tool for newcomers to SQL and those switching to a different product such as going from Oracle or MS SQL to MySQL or PostgresSQL. However, as it is, the book is a 4.5 stars product. If you're interested solely in MySQL, check out the MySQL Cookbook from O'Reilly.
Comment Comment | Permalink | Was this review helpful to you? Yes No (Report this)



 
29 of 29 people found the following review helpful:
5.0 out of 5 stars Good tutorial on and selection of recipes solving problems with SQL, January 8, 2007
By calvinnme "Texan refugee" (Fredericksburg, Va) - See all my reviews
(TOP 10 REVIEWER)      
Ultimately, the goal of this book is to give you a glimpse of what can be done using SQL outside of what is considered the typical SQL problem domain. This text is unique in that the target audience is wide, incorporating all levels of SQL programmers as well as those who are completely unfamiliar with SQL.

Both complex and simple solutions are provided, and solutions for five different vendors are available when a common solution does not exist. These five databases are DB2 v.8, Oracle Database 10g (with the exception of a handful of recipes, the solutions will work for Oracle8i Database and Oracle9i Database as well), PostgreSQL 8, SQL Server 2005 and MySQL 5. All of the examples are built around a small set of tables containing employee data. This helps the reader get familiar with the example data, so that, having become familiar with the data, you can focus on the technique that each recipe illustrates.

Chapter 1, Retrieving Records, introduces very simple queries. Examples include how to use a WHERE clause to restrict rows from your result set, providing aliases for columns in your result set, using an inline view to reference aliased columns, using simple conditional logic, limiting the number of rows returned by a query, returning random records, and finding NULL values. Most of the examples are very simple, but some of them appear in later more complex recipes, so it's a good idea to read this chapter if you're relatively new to SQL or aren't familiar with any of the examples listed for this chapter.

Chapter 2, Sorting Query Results, introduces recipes for sorting query results. The ORDER BY clause is introduced and is used to sort query results. Examples increase in complexity ranging from simple, single-column ordering, to ordering by substrings, to ordering based on conditional expressions.

Chapter 3, Working with Multiple Tables, introduces recipes for combining data from multiple tables. If you are new to SQL or are a bit rusty on joins, this is a good chapter to read before going on to Chapter 5 and later. Joining tables is what SQL is all about; you must understand joins to be successful. Examples in this chapter include performing both inner and outer joins, identifying Cartesian productions, basic set operations (set difference, union, intersection), and the effects of joins on aggregate functions.

Chapter 4, Inserting, Updating, Deleting, introduces recipes for inserting, updating, and deleting data, respectively. Most of the examples are very straightforward, perhaps even pedestrian. Nevertheless, operations such as inserting rows into one table from another table, the use of correlated subqueries in updates, an understanding of the effects of NULLs, and knowledge of new features such as multi-table inserts and the MERGE command are extremely useful for your toolbox.

Chapter 5, Metadata Queries, introduces recipes for getting at your database metadata. It's often very useful to find the indexes, constraints, and tables in your schema. The simple recipes here allow you to gain information about your schema. Additionally, "dynamic" SQL examples are shown here as well, i.e., SQL generated by SQL.

Chapter 6, Working with Strings, introduces recipes for manipulating strings. SQL is not known for its string parsing capabilities, but with a little creativity, usually involving Cartesian products, along with the vast array of vendor-specific functions, you can accomplish quite a bit. Some of the more interesting examples include counting the occurrences of a character in a string, creating delimited lists from table rows, converting delimited lists and strings into rows, and separating numeric and character data from a string of alphanumeric characters.

Chapter 7, Working with Numbers, introduces recipes for common number crunching. You'll learn how easily window functions solve problems involving moving calculations and aggregations. Examples include creating running totals; finding mean, median, and mode; calculating percentiles; and accounting for NULL while performing aggregations.

Chapter 8, Date Arithmetic, is the first of two chapters dealing with dates. Being able to perform simple date arithmetic is crucial to everyday tasks. Examples include determining the number of business days between two dates, calculating the difference between two dates in different units of time (day, month, year, etc.), and counting occurrences of days in a month.

Chapter 9, Date Manipulation, is the second of the two chapters dealing with dates. In this chapter you will find recipes for some of the most common date operations you will encounter in a typical work day. Examples include returning all days in a year, finding leap years, finding first and last days of a month, creating a calendar, and filling in missing dates for a range of dates.

Chapter 10, Working with Ranges, introduces recipes for identifying values in ranges, and for creating ranges of values. Examples include automatically generating a sequence of rows, filling in missing numeric values for a range of values, locating the beginning and end of a range of values, and locating consecutive values.

Chapter 11, Advanced Searching, introduces recipes that are crucial for everyday development and yet sometimes slip through the cracks. These recipes are not any more difficult than others, yet many developers make very inefficient attempts at solving the problems these recipes solve. Examples from this chapter include finding knight values, paginating through a result set, skipping rows from a table, finding reciprocals, selecting the top n records, and ranking results.

Chapter 12, Reporting and Warehousing, introduces queries typically used in warehousing or generating complex reports. Examples include converting rows into columns and vice versa (cross-tab reports), creating buckets or groups of data, creating histograms, calculating simple and complete subtotals, performing aggregations over a moving window of rows, and grouping rows based on given units of time.

Chapter 13, Hierarchical Queries, introduces hierarchical recipes. Regardless of how your data is modeled, at some point you will be asked to format data such that it represents a tree or parent-child relationship. This chapter provides recipes accomplishing these tasks. Creating tree-structured result sets can be cumbersome with traditional SQL, so vendor-supplied functions are particularly useful in this chapter. Examples include expressing a parent-child relationship, traversing a hierarchy from root to leaf, and rolling up a hierarchy.

Chapter 14, Odds 'n' Ends, is a collection of miscellaneous recipes that didn't fit into any other problem domain, but that nevertheless are interesting and useful. This chapter is different from the rest in that it focuses on vendor-specific solutions only. This is the only chapter of the book where each recipe highlights only one vendor. In some cases, though, you'll be able to easily tweak a solution provided in this chapter to work for a platform not covered in the recipe.

Appendix A, Window Function Refresher, is a window function refresher along with a solid discussion of groups in SQL. Window functions are new to most, so it is appropriate that this appendix serves as a brief tutorial. Additionally, the use of GROUP BY in queries is a source of confusion for many developers. This chapter defines exactly what a SQL group is, and then proceeds to use various queries as proofs to validate that definition. The chapter then goes into the effects of NULLs on groups, aggregates, and partitions. Lastly, you'll find discussion on the more obscure and yet extremely powerful syntax of the window function's OVER clause (i.e., the "framing" or "windowing" clause).

Appendix B, Rozenshtein Revisited, is a tribute to David Rozenshtein, author of "The Essence of SQL". Appendix B focuses on some of the queries presented in "The Essence of SQL", and provides alternative solutions using window functions (which weren't available when The Essence of SQL was written) for those queries.

I highly recommend this book for anyone working with or interested in working with SQL. However, you should already be familiar with the theory behind database management or you will have trouble with this book.
Comment Comment (1) | Permalink | Was this review helpful to you? Yes No (Report this)



 
19 of 19 people found the following review helpful:
5.0 out of 5 stars Mind-expanding, May 9, 2007
It doesn't take long to learn the basics of SQL. Once you start to do multi-table joins and you get to sub-selects and outer joins you can almost convince yourself you are an expert. Trust me. You aren't. I've read at least ten books on using SQL. This is the first one to blow my horizons away and open a completely new landscape.

The "recipies" are well explained and almost entirely practical and useful. This book showed me dozens of things I did not ever think of using SQL for, even though I have the BNF for SQL pretty much in my head after more than 18 years of being a practicing programmer.

Before this book I used to recommend "generic" SQL books to students and newbies because such books apply to virtually all SQL databases and it can be harmful for one to use a specific "dialect" (such as Oracle or SQL Server) that won't port to other SQL databases. I would urge people to learn "generic" SQL. You can always add the features of you specific dialect, but it is hard to give up features you assume will be in all dialects.

This book neatly avoids that problem by providing dialect-specific sections when you can or must code differently for a particular database. Even better, they explain the differences and the pros and cons of those differences. So this book can even be an aid to selecting the "right" database for your application.

I happen to be a fan of Open Source and Free Software and I'm pleased to say that this book covers both MySQL and PostgreSQL. Naturally it provides the "big three" of Oracle, Microsoft SQL Server, and IBM's DB2.

This book really opened my eyes to possibilities in SQL that I really didn't know existed in spite of the fact that I have been using the language (apparently without mastering it) for almost two decades.

I still recommend using a "generic SQL" book to learn the basics, but this should literally be ordered at the same time. It is the best of the "Cookbook" series that I have seen. (And I have used the Java Cookbook, Perl Cookbook, and Python Cookbook).
Comment Comment | Permalink | Was this review helpful to you? Yes No (Report this)


Share your thoughts with other customers: Create your own review
 
 
Ad
 
Most Recent Customer Reviews

5.0 out of 5 stars Other uses for this book
Majority of the cookbooks cater to seasoned programmers so they can quickly lookup a solution without having to reinvent the wheel. Read more
Published 17 days ago by F. Riaz

4.0 out of 5 stars Good SQL reference
This books does what it's supposed to do. Lots of good solutions to SQL problems.
Published 1 month ago by John F. Richards

5.0 out of 5 stars Helped Me (a SQL Newbie) Deliver Cmplex SQL Reports
I was tasked with deliverying 6 key-performance-indicator (KPI) using SQL and Crystal Reports and an Oracle database. Read more
Published 7 months ago by John R. Spencer

5.0 out of 5 stars SQL Tookbox Must-Have
The thing I like about the O'Reilly 'Cookbook' series is that you may not have to use them for months, but when you need them they are there with the answers. Read more
Published 9 months ago by Larry

4.0 out of 5 stars Useful fast reference
This book is great for those times when you say "there must be an easier way to do this". It's concise, readable, and easy to find your way around. Read more
Published 10 months ago by Robert Noble

2.0 out of 5 stars Good book, but shipped falling apart
Content is excellent, with good examples. I was able to solve a prloblem the very first time I used it. Read more
Published 16 months ago by flattenedfrog

5.0 out of 5 stars A great book!
I am a certified DB2 / Oracle DBA and a programmer. Even though, I can get through some of chapters very fast, I certainly have enjoyed reading it so far. Read more
Published 16 months ago by Behzad Pirvali

5.0 out of 5 stars As a reference
I found this book to be very complete. Providing solutions for multiple DBMSs was greately appreciated. I have been in I.T. Read more
Published 16 months ago by Michael Bingle

5.0 out of 5 stars It is real cook book
I am completely satisfied with this book.
Instead of inventing all the time "bicycle" I can use this book in my everyday work and concentrate on more important things in my... Read more
Published 23 months ago by Oleg Savkin

4.0 out of 5 stars MS SQL Server must be 2005
This is a great product but some of the advanced features weren't available in SQL Server 2000. I really liked the windowing and hierarchical queries in this book but those... Read more
Published on June 26, 2007 by Robert A. Meidal

Only search this product's reviews



Customer Discussions

 Beta (What's this?)
New! See all customer communities, and bookmark your communities to keep track of them.
This product's forum (0 discussions)
  Discussion Replies Latest Post
  No discussions yet

Ask questions, Share opinions, Gain insight
Start a new discussion
Topic:
First post:
Prompts for sign-in
  [Cancel]

   


Product Information from the Amapedia Community

Beta (What's this?)


So You'd Like to...


Look for Similar Items by Category


Smooth Operator

Shop for garage door openers

Find garage door products (opener kits, remotes, mini-key-chain controls, and wireless-key entry systems) in the Hardware Store. Opening the garage door shouldn’t be a chore.

Shop all garage door hardware

 

Big Savings in Books

Bargain Books
Find great titles at fantastic prices in our Bargain Books Store.
 

Summer Reading for Kids & Teens

Summer Reading for Kids and Teens
Discover everything from beach reads and board books to teen romance and action-adventure series in Summer Reading for Kids & Teens. And, check off the kids' required reading lists in our Summer School Reading Store.
 

Best Books

Best of the Month
See our editors' picks and more of the best new books on our Best of the Month page.
 
Ad

 

Feedback

If you need help or have a question for Customer Service, contact us.
 Would you like to update product info or give feedback on images?
Is there any other feedback you would like to provide?

Your comments can help make our site better for everyone.


Where's My Stuff?

Shipping & Returns

Need Help?

Your Recent History

  (What's this?)
You have no recently viewed items or searches.

After viewing product detail pages or search results, look here to find an easy way to navigate back to pages you are interested in.

Look to the right column to find helpful suggestions for your shopping session.

Continue shopping: Top Sellers
Free
Free by Chris Anderson
Paranoia
Paranoia by Joseph Finder
My Soul to Lose
My Soul to Lose by Rachel Vincent
Glenn Beck's Common Sense

Conditions of Use | Privacy Notice © 1996-2009, Amazon.com, Inc. or its affiliates