SQL Cookbook by Molinaro and de Graaf β€” book cover showing a symbolic representation of interconnected database query patterns

Pages

581

Published

2020

SQL ✨ New

SQL Cookbook

Problem-Solution Recipes for SQL Across Multiple Database Platforms

Work through 150+ SQL problems with battle-tested solutions that run on Oracle, SQL Server, PostgreSQL, MySQL, and DB2.

SQL Cookbook by Anthony Molinaro and Robert de Graaf is a problem-solution reference covering the full breadth of practical SQL work. Each recipe presents a real task β€” ranking, pivoting, generating series, working with dates, aggregating hierarchies β€” and delivers a tested solution with a clear explanation of why it works. Coverage spans Oracle, SQL Server, PostgreSQL, MySQL, and DB2, so the techniques are immediately applicable regardless of your database platform.

About this book

Most SQL books teach syntax. This one solves problems. SQL Cookbook is organized as a collection of recipes: you have a concrete task, and the book gives you a working solution, explains the mechanics behind it, and shows you where the approach differs across the five major database platforms.

The book covers far more than basic SELECT statements. You will find recipes for string and date manipulation, report generation, hierarchical queries, window functions, pivoting and unpivoting result sets, and generating the kinds of summary data that real reporting and analytics work demands. Each recipe is self-contained, so you can jump straight to the technique you need without reading front to back.

Authors Anthony Molinaro and Robert de Graaf have been writing and tuning SQL across different database engines for decades. Their explanations focus on the reasoning behind each solution β€” why a particular join strategy avoids a Cartesian product, how a window function eliminates a correlated subquery, when a common table expression makes a recursive hierarchy tractable. You come away not just with a working query but with a mental model you can adapt.

Key areas covered include:

  • Retrieving, filtering, and transforming rows and columns with precision
  • Working with strings, numbers, and dates across platform differences
  • Aggregating, grouping, and pivoting result sets for reports
  • Ranking, windowing, and generating running totals and moving averages
  • Writing hierarchical and recursive queries for tree-structured data
  • Inserting, updating, deleting, and merging data safely and efficiently
  • Metadata queries and introspecting database objects at runtime

Whether you keep this book open next to your editor as a reference or work through it systematically to fill gaps in your SQL knowledge, it earns its place on a working developer or analyst's desk. With 581 pages of dense, practical content and coverage across five platforms, it is one of the few SQL books that stays useful long after you have read it once.

🎯 What you'll learn

  • Solve string, number, and date manipulation problems without platform-specific trial and error
  • Write window functions that replace slow correlated subqueries for ranking and running totals
  • Pivot and unpivot result sets to match the shape that reporting tools expect
  • Query hierarchical and recursive data structures using CTEs and vendor-specific extensions
  • Generate and manipulate date ranges, sequences, and calendar data purely in SQL
  • Insert, update, delete, and merge rows safely across multiple platforms
  • Introspect database metadata to write queries that adapt to schema changes

πŸ‘€ Who is this book for?

  • Backend developers who write SQL regularly and want faster, cleaner solutions to common data tasks
  • Data analysts who know basic SELECT but struggle with window functions, pivots, and complex aggregations
  • Database administrators who need cross-platform reference material for Oracle, SQL Server, PostgreSQL, MySQL, or DB2
  • Software engineers moving from ORM-only work to writing raw SQL in production queries
  • BI developers who generate reports from relational databases and need reliable patterns for summarization and ranking

Table of contents

  1. 01

    Retrieving Records

    Covers the foundational SELECT patterns you will actually reach for every day, including filtering rows, handling NULLs, and sorting result sets with precision.

  2. 02

    Sorting Query Results

    Explores ordering strategies beyond a simple ORDER BY, including sorting by substrings, conditional ordering, and dealing with NULL placement across platforms.

  3. 03

    Working with Multiple Tables

    Walks through joins, set operations, and the difference between UNION and UNION ALL, with clear examples of when each produces correct results and when it does not.

  4. 04

    Inserting, Updating, and Deleting

    Provides recipes for every common data modification task, from inserting default values and copying rows between tables to performing conditional updates and safe deletes.

  5. 05

    Metadata Queries

    Shows how to query database catalogs and information schema views to discover table structures, constraints, and index definitions programmatically on each supported platform.

  6. 06

    String Manipulation

    Covers parsing, replacing, padding, and extracting substrings, with side-by-side comparisons of how each platform handles the same string operation.

  7. 07

    Working with Numbers

    Addresses rounding, modular arithmetic, running totals, computing medians, and generating sequences β€” tasks where SQL's numeric behavior surprises even experienced developers.

  8. 08

    Date Arithmetic

    Teaches date addition, subtraction, difference calculation, and interval handling, highlighting where Oracle, SQL Server, PostgreSQL, MySQL, and DB2 diverge significantly.

  9. 09

    Reporting and Warehousing

    Introduces window functions, pivoting, and the aggregation patterns required for business reports, including cumulative sums, moving averages, and top-N-per-group queries.

  10. 10

    Hierarchical Queries

    Demonstrates how to query parent-child and tree-structured data using recursive CTEs and platform-specific syntax such as Oracle's CONNECT BY.

Frequently asked questions

What databases does this book cover?

All solutions are demonstrated on Oracle, SQL Server, PostgreSQL, MySQL, and IBM DB2. Most recipes show the differences between platforms explicitly, so you can adapt the solution to whichever system you use.

What SQL knowledge do I need before reading this book?

You should already know how to write basic SELECT, INSERT, UPDATE, and DELETE statements. The book targets intermediate to advanced users β€” it does not teach SQL from scratch but does explain the reasoning behind every solution.

Is this a reference book or something I read cover to cover?

It works well as both. Each recipe is self-contained, so you can jump to the chapter that matches your current problem. Reading sequentially will also surface techniques you may not have known to search for.

Does the book cover modern SQL features like window functions and CTEs?

Yes. Window functions, common table expressions, and recursive queries receive substantial coverage, with explanation of which platforms support each feature and what workarounds exist where support is limited.

Is this edition current enough to be useful?

Published in 2020, the book reflects the state of each platform at that time. Core SQL techniques are stable, so the vast majority of recipes remain accurate. Always verify syntax against your specific database version for newer features.

You might also like

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.