Part I — Foundations
"Before you can write a single query, you need to know what a database actually is, why it exists, and what mathematical idea makes it work. Skip this and you will spend years memorizing syntax you don't understand."
Most people meet databases backwards. They learn SELECT * FROM users on day one, copy a few snippets from Stack Overflow, and never ask the questions that actually matter: Why a database instead of a spreadsheet or a pile of files? What is a "table," really? Why does every serious database in the world speak the same strange language — SQL — that was designed in the 1970s and has outlived every technology meant to replace it?
This first part answers those questions, and in doing so it installs the mental model that the rest of the book builds on. By the end of Part I you will have PostgreSQL running on your own machine, you will have loaded the Mercado practice database that powers every example in this book, and — crucially — you will understand the relational model and the relational algebra that sit underneath every query you will ever write.
What you will learn
- Chapter 1 — What Is a Database? The problem databases solve: structured, shared, persistent, consistent data with safe concurrent access. Why spreadsheets and flat files break down, what a database management system (DBMS) gives you, and a short history from hierarchical to relational to NoSQL — and why relational won.
- Chapter 2 — Setting Up PostgreSQL. Installing PostgreSQL on any platform (including Docker), meeting
psqland a GUI, creating your first database and table, running your firstSELECT, and loading the Mercado dataset you will use for the next 38 chapters. - Chapter 3 — The Relational Model. Relations, tuples, and attributes; domains and data types; primary and foreign keys; the meaning of
NULL(the "billion-dollar mistake"); and the integrity constraints that keep your data honest. - Chapter 4 — Relational Algebra. The handful of operations — selection, projection, join, union, difference, and friends — that every SQL query is secretly translated into. Understanding the algebra is what lets you reason about why a query is correct and why it is fast or slow.
Why this part matters
Two of the book's recurring themes are born here. First, understand the WHY, not just the HOW: relational algebra is not academic decoration — it is the lens that makes the rest of SQL make sense. Second, the relational model is right for the overwhelming majority of data problems, and you cannot judge when to reach for something else until you understand what you would be giving up.
For every learning path
Nobody skips Part I. The 💻 developer, 📊 analyst, 🔬 CS student, and 🏗️ DBA all stand on this foundation. The CS student will linger on the formal treatment of the relational model and algebra (Chapters 3–4); the analyst and developer can move briskly through the theory but should not skip it — the payoff arrives the moment joins, indexes, and query plans start to feel inevitable rather than mysterious.
A note on Mercado
From Chapter 2 onward, every table you query, every row you count, and every schema you critique comes from Mercado — a fictional online marketplace with customers, orders, products, reviews, employees, suppliers, and warehouses. Install it once and it becomes the world you practice in. By the time you finish the book, you will know its 13 tables the way you know the streets of your neighborhood.
Turn the page. We start with the question everything else depends on: what is a database, and why do we need one at all?
Chapters in This Part
- Chapter 1: What Is a Database? Why Spreadsheets Break and Databases Don't
- Chapter 2: Setting Up PostgreSQL, Your First Database, and the Tools You'll Use
- Chapter 3: The Relational Model — Tables, Rows, Columns, Keys, and the Mathematical Foundation of SQL
- Chapter 4: Relational Algebra — The Operations Underneath Every SQL Query