Jump to ratings and reviews
Rate this book

SQL Cookbook

Rate this book
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 SELECT columns FROM table WHERE conditions. 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











Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo "When it comes down to it, we all go to work, we all 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.

633 pages, Paperback

First published December 16, 2005

215 people are currently reading
813 people want to read

About the author

Anthony Molinaro

4 books2 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
140 (34%)
4 stars
161 (39%)
3 stars
91 (22%)
2 stars
11 (2%)
1 star
3 (<1%)
Displaying 1 - 25 of 25 reviews
Profile Image for Michael Pilgrim.
9 reviews1 follower
January 22, 2018
I read this book cover-to-cover rather than using it in the manner intended. The intention is that, when one has a problem to solve, one would find the most similar problem in the table of contents. The repetition of reading multiple similar solutions by reading the entire book helped solidify the concepts. I've read other reviews here that criticize the SQL Cookbook because using the table of contents in the manner intended might lead to a solution that depends on concepts explained in prior solutions. The solutions are not always self-contained. Such criticisms are valid and are part of the reason that I read the book in its entirety. I have since implemented much of the material in my work and have improved a great deal as a result. Highly recommended.
9 reviews1 follower
May 6, 2008
If you NEED to learn the amazingly complex and available functionality in SQL queries, this is the book for you. Simply explained and elegantly detailed, this book is truly a must have for anyone with any level of SQL expertise, from the new beginner to the seasoned veteran.
Profile Image for Surattikorn.
117 reviews5 followers
October 7, 2024
A Must-Have Guide for Solving Everyday SQL Problems (5/5⭐)

I really enjoyed SQL Cookbook by Anthony Molinaro and Robert de Graaf. It’s super helpful for solving everyday SQL problems, especially for developers, data analysts, and database admins. The book gives clear solutions for popular databases like Oracle, MySQL, and PostgreSQL. Even if you're not a SQL expert, this book helps you write better and faster queries.

I found the sections on Recursive Hierarchical Queries, Window Functions, and CTEs especially useful, and I’ve already used them in my projects. Definitely a must-have on your desk for quick reference!
Profile Image for Doug.
50 reviews5 followers
March 6, 2008
You too can write SQL like the Jedi of old.
Profile Image for Fiona Dax.
57 reviews17 followers
January 30, 2022
Perfect , Perfect and Perfect For Non-Experience and Seniors
Profile Image for Gabriel Le Gall.
22 reviews2 followers
July 17, 2021
Overall, one of the best SQL references out there !

I love how the book is structured : problem/solution/discussion, it is such a stimulating way to learn.
I also think some of the recipes are extremely useful (e.g. 13.4, 6.6, 9.10, 14.1, 14.2, 12.14, 12.13 and much more !). I have encountered many situations in which I would have liked to have read this book earlier.

Finally, I think the Appendix B is interesting and challenging although quite redundant (some solutions are almost exactly the same as other ones in the same chapter).

I have found some mistakes here and there in the book (https://www.reddit.com/r/SQL/comments...) but if I had a point to make, it would probably be about the complexity of the solutions proposed. Sometimes, I feel like the solutions are overshooting so much the original problem. For instance :

Appendix B, Question 12 : "You want to find students who take all courses"

Solution proposed :

SELECT sno,sname,age
FROM
(SELECT s.sno,s.sname,s.age,
COUNT(t.cno) OVER (PARTITION BY s.sno) AS cnt,
COUNT(distinct c.title) OVER() AS total,
ROW_NUMBER() OVER(PARTITION BY s.sno ORDER BY c.cno) AS rn
FROM courses c
LEFT JOIN take t ON (c.cno = t.cno)
LEFT JOIN student s ON (t.sno = s.sno)
) x
WHERE cnt = total
AND rn = 1

Seemingly equivalent solution :

SELECT t.sno, COUNT(*) as cnt
FROM [take] t
GROUP BY t.sno
HAVING COUNT(*) = (SELECT COUNT(DISTINCT cno) FROM [take])

I whish the book was prioritizing parsimony a little bit more, but maybe I'm just missing something
Profile Image for Tanya.
449 reviews9 followers
August 24, 2009
Thumbed through this initially and found some new tricks right away. So far it's been easy to find things for specific problems I need to solve.
Profile Image for Chris Nitsas.
44 reviews14 followers
January 10, 2023
This is a chore to get through. I stopped reading around 24% through. I guess it's not for me.

The book is very thorough in describing how to do stuff on different SQL databases, namely: MySQL, PostgreSQL, DB2, Oracle, and SQL Server. The problem is that I don't care about most of the databases! I mainly use MySQL (actually MariaDB).

If you have the patience to fast forward through the vendor-specific sections that you don't care about, the book has many interesting and challenging recipes.

Unfortunately the code in many recipes is hard to read! E.g. the author may create an intermediate (helper) table and name it "x" so you don't have a clue what the table holds. :(

Each chapter contains between 7 and 18 recipes around a similar theme. The chapters are:
1. Retrieving records
2. Sorting query results
3. Working with multiple tables
4. Inserting, updating, and deleting
5. Metadata queries
6. Working with strings
7. Working with numbers
8. Date arithmetic
9. Date manipulation
10. Working with ranges
11. Advanced searching
12. Reporting and reshaping
13. Hierarchical queries
14. Odds 'n' ends
Appendix A: Window function refresher
Appendix B: Common table expressions
1 review
May 8, 2025
SQL Cookbook is my go-to resource for both deep dives into SQL logic and quick memory refreshers. I really like that it includes examples in multiple SQL dialects, which is helpful when working across different systems like PostgreSQL and MySQL. The examples are simple, practical, and easy to follow—perfect for both learning and quick reference. It’s one of the few technical books I keep coming back to time and time again. Highly recommended for anyone working with SQL—whether you're just starting out or looking to sharpen your skills.
Profile Image for Jake Losh.
211 reviews24 followers
March 23, 2020
Rather good. A bit dated in spots, bit the core ideas are still solid. Nice to see some solutions to problems I came up with (as a SQL autodidact) that I thought were"hacky" were actually the canonical way if solving those sorts of problems. I see now clearly some of the strengths and weaknesses of specific SQL flavors now, too. Lastly, I should be using window functions more.
44 reviews17 followers
March 27, 2020
Libro con queries avanzadas, en caso de que no tengas mucho conocimiento de SQL te recomiendo algún libro introductorio como la serie de "dummies" y posteriormente continúes con este o se puede volver confuso. Muestra diferentes funciones y sus aplicaciones en la vida real.

Libro interesante.
Profile Image for Goce Bonev.
20 reviews8 followers
March 27, 2022
Abbriviations for fields and table names used in queries like: emp, empno, deptno, cnt, sal, mgrs, prez, smen and others make reading the examples hard and set a very bad example for writing good readable queries. Other than that it is a great book.
Profile Image for Roohollah Khodabakhshi.
7 reviews
May 5, 2024
While it provides valuable insights overall, some chapters may fall short in clarity or relevance. Despite this, its comprehensive coverage and practical recipes still make it a worthwhile resource for SQL practitioners seeking to enhance their skills.
Profile Image for indy.
189 reviews2 followers
February 14, 2019
I purchased this as an eBook in a Humble Bundle and boy was that a bargain! I'm glad to have this in my dev toolkit; it's already informed and inspired some of my work.
Profile Image for John Doe.
68 reviews10 followers
September 8, 2020
Why would anyone want to read something like this when you can just Google everything? Never mind. 😑
Profile Image for Ferhat Culfaz.
268 reviews18 followers
November 18, 2021
Some useful snippets of information on the various SQL flavours but could be too simple for advanced users.
Profile Image for Adam.
185 reviews10 followers
March 19, 2022
Good cookbook but a not enough on joins. Joins are the backbone of all SQL work and i felt like there was some missed opportunity there to really teach that subject well.
Profile Image for Ashraf Bashir.
226 reviews137 followers
December 28, 2016
The book covers many RDBMs, it is categorized by different topics, each topic contains many recipes. The recipes are well organized from most simple to more complex, so it takes you step by step to learn the details of each topic. Also there is a well documented description of queries. I like it a lot! So why I rated it 3/5 ? Because it is boring! And it has many repetitions of same ideas across recipes, and some recipes will not be understood unless you read first the previous recipe which makes it hard to randomly jump to a recipe. But in general it serves as a very good book to learn about SQL different topics and it use-cases in a "problem-solution" approach, with some amazing tricks which bring your 'Aha!' moments ;) ... Worth reading, especially that it won't take so much of your time; because you do not have to read each solution's description if you understand how the solution works.
Profile Image for Oleksandr Stefanovskyi.
54 reviews2 followers
January 28, 2023
"SQL Cookbook" by Anthony Molinaro is an invaluable resource for developers and database administrators.

The book is well-organized and structured, starting with the simplest and moving on to more complex recipes. It covers the most commonly used RDBMS, which is incredibly helpful for understanding them, not just the one you're currently using. The book is well-written with clear and easy-to-understand descriptions of queries, making it accessible even if you're unfamiliar with specific keyword usage.

I found the Oracle solutions beneficial, and the book is a great reference that taught me a lot of valuable tricks. In addition, the book contains many examples that I will likely use in the future, and I know I'll be coming back to it again and again.
Profile Image for Eddie Welker.
5 reviews
September 13, 2007
great book for those who occasionally use sql, but don't want/need to learn it from head to toe.
16 reviews
Read
February 3, 2016
Focused on the Oracle solutions. Great reference book that will teach you a lot of neat tricks.
Displaying 1 - 25 of 25 reviews

Can't find what you're looking for?

Get help and learn more about the design.