🗄️ Learn SQL — From Zero to Pro

A comprehensive, edge-case-covering, idiomatic SQL curriculum. Each document is self-contained and covers its concept deeply enough that a careful reader can go from beginner to pro SQL developer.

How to Use This Course

  1. Read sequentially for a structured path (01 → 30).
  2. Jump to a chapter as a reference when you hit a concept in the wild.
  3. Run the exercises in chapter 30 against a real database.
  4. Read your database's docs (PostgreSQL, MySQL, SQLite) alongside.

Prerequisites

  • A SQL database (PostgreSQL recommended; SQLite works for most chapters).
  • A client tool (psql, DBeaver, or sqlite3 CLI).
  • Comfort with basic programming concepts.

Curriculum

Part I — Foundations

#TopicWhy It Matters
01Introduction & SetupRelational model, PostgreSQL/SQLite install, psql/sqlite3.
02SELECT Basics & FilteringProjection, WHERE, comparison & logical operators.
03Sorting, Pagination & LIMITORDER BY, LIMIT/OFFSET, keyset pagination.
04JoinsINNER/LEFT/RIGHT/FULL/CROSS, join mechanics.
05Aggregation & GROUP BYCOUNT/SUM/AVG/MIN/MAX, HAVING, grouping quirks.

Part II — Query Composition

#TopicWhy It Matters
06SubqueriesScalar, correlated, EXISTS/IN, semi/anti-joins.
07Common Table ExpressionsWITH, readability, chaining, materialization hints.
08Set OperationsUNION/UNION ALL/INTERSECT/EXCEPT, column matching.
09Window FunctionsOVER, PARTITION BY, frames, ranking, running totals.
10Data Types & NULL HandlingThree-valued logic, COALESCE, NULLIF, type coercion.

Part III — Schema & Data Definition

#TopicWhy It Matters
11Tables, Schemas & DDLCREATE/ALTER/DROP, schemas, temporary tables.
12Constraints & KeysPRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, NOT NULL.
13Indexes & PerformanceB-tree, partial, expression, composite, when indexes hurt.
14INSERT, UPDATE, DELETEDML, RETURNING, upsert, cascading deletes.
15Transactions & IsolationACID, BEGIN/COMMIT/ROLLBACK, isolation levels, deadlocks.

Part IV — Advanced Querying

#TopicWhy It Matters
16Views & Materialized ViewsVirtual tables, refresh strategies, updatable views.
17Date & Time HandlingDATE/TIMESTAMP/INTERVAL, time zones, DST traps.
18JSON & Array ColumnsJSONB, indexing JSON, array operators, unnesting.
19Recursive QueriesRecursive CTEs, tree traversal, graph patterns.
20Full-Text Searchtsvector, ranking, tsquery, trigrams, LIKE vs FTS.

Part V — Programming in the Database

#TopicWhy It Matters
21Triggers & EventsBEFORE/AFTER, statement vs row, audit tables.
22Stored Procedures & FunctionsFUNCTION vs PROCEDURE, PL/pgSQL, volatility.
23Sequences & IdentifiersSERIAL/IDENTITY/SEQUENCE, gaps, currval/nextval.
24Security, Roles & PermissionsGRANT/REVOKE, roles, RLS, least privilege.

Part VI — Production Engineering

#TopicWhy It Matters
25Normalization & Data Modeling1NF–BCNF, denormalization tradeoffs, surrogate keys.
26Query Optimization & EXPLAINEXPLAIN ANALYZE, seq vs index scans, join strategies.
27Advanced SQL PatternsPivots, gaps-and-islands, running medians, histograms.
28Database AdministrationBackups, VACUUM, replication, connection pooling.
29Common Pitfalls & Idiomatic Fixes40+ traps and their fixes.
30Exercises & Project IdeasFrom beginner to pro.

Learning Path Suggestions

If you're new to databases

  1. Read 01–10 in order.
  2. Build a small schema (11–14) and insert real data.
  3. Read 15 (Transactions) before writing any app code.
  4. Do exercises 1–5 in chapter 30.

If you're coming from a NoSQL background

Read 04 (Joins) and 05 (Aggregation) carefully — they're the core differentiator. Read 25 (Normalization) to understand why schemas exist. Skim 10 (NULL) — three-valued logic is a common surprise.

If you're coming from a programming language

Read 06–09 (subqueries, CTEs, set ops, windows) — these are the "control flow" of SQL. Don't skip 10 (NULL) — NULL = NULL is UNKNOWN, not TRUE. Read 26 (EXPLAIN) early — query plans matter more than syntax.

If you're a senior engineer

Skim 01–14. Read 09 (Windows), 15 (Isolation), 18 (JSON), 19 (Recursive), 26 (EXPLAIN) closely. Use 27 (Patterns) and 29 (Pitfalls) as references. Read 28 (Admin) for production readiness.

Companion Resources

Tooling to Install

bash
# PostgreSQL (macOS)
brew install postgresql@16
brew services start postgresql@16
psql postgres

# SQLite
brew install sqlite
sqlite3 practice.db

# Or use Docker
docker run --name pg -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:16
psql -h localhost -U postgres

License

These notes are yours to use, share, and modify.

🗄️