From the Editor — Vol. I, No. 1

A field guide to writing
PostgreSQL by hand,
one query at a time.

Set against a single eleven-table schema, this is a working handbook of 756 hand-checked queries — from a humble SELECT to a recursive CTE walking an organisation chart. Read it cover to cover, or open the page you need.

I.

Read more than you write.

Every query in this volume is set in proper monospace, indented for the eye, and annotated where a junior reader would stumble.

II.

The schema is not an appendix.

Tables, keys, and relationships sit beside the page. You should never have to leave the chapter to remember a column.

III.

No icons, no glow, no theatre.

Ink on parchment, rules where rules belong, oxblood for emphasis. The query is the picture.

A specimen page

How a single answer is set on the page.

Each entry begins with the question in plain English, followed by the query — typeset, syntax-coloured, and copyable in one motion. Beneath it, a brief note on why this clause and not another, and the tables it touches, linked back to the schema.

  • ·Window functions used where they actually belong.
  • ·CTEs over nested subqueries when a human will read it.
  • ·Time zones handled, not hoped for.
SQL
-- The five top-spending customers in the last quarter,
-- with a running share of total revenue.
SELECT
  c.id,
  c.name,
  SUM(o.total)                                AS spent,
  SUM(SUM(o.total)) OVER (ORDER BY SUM(o.total) DESC
                          ROWS UNBOUNDED PRECEDING)
    / SUM(SUM(o.total)) OVER ()               AS running_share
FROM   customers c
JOIN   orders    o ON o.customer_id = c.id
WHERE  o.placed_at >= date_trunc('quarter', now()) - interval '3 months'
GROUP  BY c.id, c.name
ORDER  BY spent DESC
LIMIT  5;
From — Chapter VI · Window Functions

How to read this book

  1. 01

    Pick a chapter.

    Twelve chapters, arranged from foundations to performance. Independent — open whichever you need.

    Browse chapters
  2. 02

    Read the query.

    Every answer is hand-set with a short note on intent, edge cases, and the alternative you didn't take.

    See a sample
  3. 03

    Check the schema.

    Eleven tables, fully indexed and cross-referenced. Each query lists the tables it touches.

    Open the schema

A query well written is a sentence well thought. The database does not reward cleverness — it rewards clarity.

— The Editor

What readers say

  • I keep this open in a tab. When I need a window function or a CTE I can read, not copy-paste blindly, I come here first.

    M. L. Chen

    Senior data engineer, San Francisco

  • Finally a SQL reference that treats the schema as part of the answer, not an afterthought in a footnote.

    A. R. Okonkwo

    Backend lead, Berlin

  • No noise, no gradients, no dark-mode toggle that breaks your eyes. Just queries, well set, with context. Exactly what I wanted.

    J. Sørensen

    Freelance analyst, Copenhagen

The chapters

Twelve · arranged alphabetically
  1. 01

    Advanced PostgreSQL

    Beyond the standard surface. JSONB, arrays, full-text search, partitions, and the features that make Postgres feel limitless.

    350 queriesRead →
  2. 02

    Basic SELECT

    The foundation. Selecting columns, ordering, limiting, and counting — the vocabulary every query is built from.

    136 queriesRead →
  3. 03

    CTEs & Recursive

    Common table expressions. Named building blocks, recursion, and the readable alternative to nested subqueries.

    15 queriesRead →
  4. 04

    Date & Time Functions

    Time, done properly. Intervals, truncation, extraction, time zones, and the arithmetic that survives DST.

    16 queriesRead →
  5. 05

    GROUP BY & Aggregation

    Folding rows into summaries. GROUP BY, HAVING, ROLLUP, and the aggregate functions that turn data into answers.

    24 queriesRead →
  6. 06

    INSERT, UPDATE, DELETE

    Changing data safely. INSERT, UPDATE, DELETE, UPSERT, RETURNING, and the patterns that keep history intact.

    15 queriesRead →
  7. 07

    JOINs (INNER, LEFT, RIGHT, FULL)

    Combining tables. Inner, left, right, full, self, and the cross — knowing which one to reach for is most of the craft.

    32 queriesRead →
  8. 08

    Performance & Indexes

    Making queries fast. Indexes, EXPLAIN, statistics, partial and expression indexes, and reading a plan without flinching.

    76 queriesRead →
  9. 09

    String Functions

    Text in Postgres. Concatenation, casing, trimming, regex, and the functions that quietly do most of the work.

    19 queriesRead →
  10. 10

    Subqueries & EXISTS

    Queries inside queries. Scalar, correlated, EXISTS, IN, ANY, ALL — and when a CTE would read better.

    21 queriesRead →
  11. 11

    WHERE & Filtering

    Narrowing the result set. Predicates, boolean logic, pattern matches, null handling, and the small operators that change everything.

    30 queriesRead →
  12. 12

    Window Functions

    Looking sideways without collapsing rows. Partitioning, ordering, running totals, ranks, leads, and lags.

    22 queriesRead →

Begin reading

Open the first chapter.
The rest will follow.