Leanpub Header

Skip to main content

Build a SQL Database Engine in C++

Through Challenges --- Storage and Query Processing (Parts I & II)

This book is 100% completeLast updated on 2026-07-08

Build a working SQL database engine in C++20 -- from raw pages to a real

query executor -- one compilable, measured challenge at a time. Every

performance claim is a benchmark you run yourself; every unit ends with a

real bug, caught red-handed and fixed. Nothing asserted. Everything

demonstrated.

Minimum price

$13.30

$13.30

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Build a working SQL database engine in C++20 -- from an empty directory to

a query processor that runs real SQL against data on disk -- through a

single relentless method: nothing is asserted; everything is demonstrated.

Every data structure in this book is built, compiled, and run. Every

performance claim is a table printed by a benchmark whose source code is on

the page in front of you. Every design decision is followed by the

measurement that justifies it -- and, where the design has a cost, by the

measurement that exposes that cost too. And once per unit, something fails

in front of you: a real bug, reproduced deterministically, diagnosed from

the evidence, fixed, and locked shut with a regression test.

This is not a survey of database theory. It is a lab manual. You will not

find hand-waving about how B+Trees are "generally logarithmic" -- you will

find the fan-out arithmetic, the page-count math, and a benchmark that

walks a tree of a million keys and prints the real number.

This book takes the engine across two complete parts and eight units:

PART I -- STORAGE

1. Slotted Pages & the Pager -- self-describing 4 KiB pages, records with

stable slot ids, a pager with a free list

2. B+Tree: Insert & Search -- logarithmic lookup, proven against a

million-key tree

3. B+Tree: Delete & Range Scans -- rebalancing, merges, ordered range

queries

4. The Buffer Pool -- a real cache with clock eviction, measured 2.5x

faster with identical logical work

PART II -- FROM BYTES TO A QUERY

5. The Record Layer -- typed rows: a schema-aware codec and a table heap,

reached by key through the index

6. The Catalog -- persistent schemas and named tables: the engine's

self-knowledge

7. The Front End -- a SQL tokenizer and recursive-descent parser, with

compiler-quality caret-pointed errors

8. The Executor -- the milestone: a real executor that runs CREATE TABLE,

INSERT, and SELECT end to end, against a database of hundreds of

thousands of rows

By the last page, the engine answers a SQL query it parsed from text,

against rows it stored on disk, through an index it built and a cache it

manages itself -- and every number in the book came from actually running

that code.

Reference machine: g++ 13.3.0, Ubuntu 24.04, C++20, stdlib + POSIX only.

Every benchmark ships with the exact command that produced it, so you can

run it yourself and get the same structural numbers.

A companion volume, "Build a SQL Database Engine in C++, Book 2: Parts III

& IV," continues the engine into durability (crash recovery via a

write-ahead log), concurrency, and performance -- available separately, and

as a discounted bundle with this book.

Bundle

Bundles that include this book

Author

About the Author

Hatem M.

Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.

His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.

Contents

Table of Contents

Part I — Storage
  • Unit 1: Slotted Pages & the Pager — self-describing 4 KiB pages, records with stable slot ids, a pager with a free list
  • Unit 2: B+Tree: Insert & Search — logarithmic lookup, proven against a million-key tree
  • Unit 3: B+Tree: Delete & Range Scans — rebalancing, merges, ordered range queries
  • Unit 4: The Buffer Pool — a real cache with clock eviction, measured 2.5x faster with identical logical work
Part II — From Bytes to a Query
  • Unit 5: The Record Layer — typed rows: a schema-aware codec and a table heap, reached by key through the index
  • Unit 6: The Catalog — persistent schemas and named tables: the engine’s self-knowledge
  • Unit 7: The Front End — a SQL tokenizer and recursive-descent parser, with compiler-quality caret-pointed errors
  • Unit 8: The Executorthe milestone: a real executor that runs CREATE TABLE, INSERT, and SELECT end to end, against a database of hundreds of thousands of rows

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub