What this is

In my career, I have met a surprisingly large number of very talented engineers who are not comfortable with PostgreSQL and SQL fundamentals.

PgQuest is a concise, interactive introduction to the basics of PostgreSQL. The code and the examples do most of the explaining, so you can learn by reading queries and seeing what PostgreSQL does with them.

It is written for experienced developers who want a practical grounding in SQL and PostgreSQL, without starting with a long textbook or a large application.

Playground

Load any lesson database

Use the same browser SQL editor without following a lesson. Load any database used in the lessons, edit the query, and run it directly in PGlite.

Open

Curriculum

27 lessons · roughly 4 hours

Lesson 01

My first query

Create a small table, seed two rows, SELECT them back, and inspect the plan Postgres uses.

SELECT · EXPLAIN

10 min

0/3 exercises

Lesson 02

Introduction to JOIN

See why splitting data across tables matters, and use JOIN to combine it back together.

JOIN

10 min

0/1 exercises

Lesson 03

Schemas, tables, and types

Explore schemas, list tables, inspect columns, and understand database shape.

SCHEMA · TABLES · TYPES

15 min

0/2 exercises

Lesson 04

An introduction to indexes

Indexes and EXPLAIN plans, comparing a sequential scan with an index scan on the same query.

INDEX · EXPLAIN

15 min

0/2 exercises

Lesson 05

Insert, update, delete, upsert

Change stored rows with INSERT, UPDATE, DELETE, RETURNING, and ON CONFLICT upserts.

INSERT · UPDATE · DELETE · UPSERT

15 min

0/4 exercises

Lesson 06

MVCC and row versions

Read the hidden xmin/xmax columns PostgreSQL keeps on every row, and watch them change across a real BEGIN ... COMMIT.

MVCC · XMIN · XMAX

20 min

0/2 exercises

Lesson 07

Draft

Constraints

Use NOT NULL, UNIQUE, CHECK, primary keys, and foreign keys to protect data.

CONSTRAINTS · KEYS

15 min

0/2 exercises

Lesson 08

Draft

Advanced indexes

Use selectivity, composite indexes, and partial indexes to shape better plans.

SELECTIVITY · COMPOSITE · PARTIAL

20 min

0/2 exercises

Lesson 09

Draft

Aggregation intro

Summarize rows with COUNT, SUM, AVG, MIN, MAX, and GROUP BY.

COUNT · GROUP BY

15 min

0/2 exercises

Lesson 10

Draft

NULL

Understand missing values, IS NULL, three-valued logic, and COALESCE.

NULL · COALESCE

15 min

0/2 exercises

Lesson 11

Types of JOINs

Compare INNER, LEFT, RIGHT, FULL OUTER, and CROSS JOIN on the same two tables.

JOIN · LEFT · RIGHT · FULL · CROSS

20 min

0/2 exercises

Lesson 12

Draft

Relationships

Model one-to-many and many-to-many data with foreign keys and join tables.

FOREIGN KEY · RELATIONSHIPS

20 min

0/2 exercises

Lesson 13

Draft

Sorting and pagination

Sort rows deterministically with ORDER BY, LIMIT, OFFSET, and indexes.

ORDER BY · LIMIT

15 min

0/2 exercises

Lesson 14

Draft

Text search basics

Search text with LIKE, ILIKE, wildcards, and case-insensitive patterns.

LIKE · ILIKE

15 min

0/2 exercises

Lesson 15

Draft

JSON in PostgreSQL

Store jsonb, extract fields, query nested values, and use containment.

JSONB

20 min

0/2 exercises

Lesson 16

Draft

Transactions

Group changes with BEGIN, COMMIT, and ROLLBACK so work is atomic.

BEGIN · COMMIT · ROLLBACK

15 min

0/1 exercises

Lesson 17

Draft

Advanced aggregation

Use HAVING, FILTER, date_trunc, and time-based grouping.

HAVING · FILTER · DATE_TRUNC

20 min

0/2 exercises

Lesson 18

Draft

Vectors and similarity search

Search by meaning: embeddings, L2 distance, cosine similarity, and nearest neighbours, computed by hand with plain arrays.

EMBEDDINGS · DISTANCE · SIMILARITY

20 min

0/2 exercises

Lesson 19

Draft

pgvector

The vectors lesson again, with pgvector: a real vector type, distance operators, and four ways to search many rows: exact, HNSW, IVFFlat, and smaller vectors.

PGVECTOR · HNSW · IVFFLAT · HALFVEC

20 min

0/2 exercises

Lesson 20

Draft

Full-text search

Search words instead of characters with tsvector and tsquery: stemming, search-box syntax, ranking, highlights, and a GIN index.

TSVECTOR · TSQUERY · GIN

20 min

0/2 exercises

Lesson 21

Draft

Fuzzy search with pg_trgm

Find text despite typos with trigram similarity, word_similarity, 'did you mean' suggestions, and trigram indexes.

PG_TRGM · SIMILARITY

20 min

0/2 exercises

Lesson 22

Draft

Text search in Postgres, without Elasticsearch or Algolia

Compare ILIKE, fuzzy, full-text, semantic, and hybrid search on the same documents, and decide when Postgres is enough.

ILIKE · PG_TRGM · TSVECTOR · PGVECTOR

20 min

0/2 exercises

Lesson 23

Implementing a high-concurrency system with Postgres

Model a seat reservation system and use transactions with advisory locks to close a real race condition.

ACID · TRANSACTIONS · ADVISORY LOCKS

25 min

0/2 exercises

Lesson 24

Transaction isolation levels

See what READ COMMITTED, REPEATABLE READ, and SERIALIZABLE actually let concurrent transactions see, with a bug only SERIALIZABLE catches.

ISOLATION LEVELS · WRITE SKEW

20 min

0/2 exercises

Lesson 25

Draft

Locks

See which locks every statement takes, what blocks what, and how NOWAIT, SKIP LOCKED, and lock_timeout keep sessions from waiting forever.

TABLE LOCKS · ROW LOCKS · SKIP LOCKED

25 min

0/2 exercises

Lesson 26

Draft

Finding and fixing slow queries

Profile an app's queries with pg_stat_statements, find why the slowest are slow with EXPLAIN ANALYZE, and fix them with a trigram index and keyset pagination.

PG_STAT_STATEMENTS · EXPLAIN ANALYZE · PG_TRGM

25 min

0/2 exercises

Lesson 27

Draft

Building a job queue with Postgres

Build a job queue on a plain table: hand each job to one of many workers with SKIP LOCKED, survive crashes with leases and retries, and see why exactly-once delivery is impossible but exactly-once processing isn't.

SKIP LOCKED · DELIVERY GUARANTEES · OUTBOX

30 min

0/2 exercises