Stage 2

Stage 2: Intermediate — querying across tables

Goal: Combine tables with joins, break hard questions into steps with subqueries and CTEs, and write the window-function analytics queries interviewers ask for. Time: about 2 weeks at ~30–60 minutes a day (~13–14 hours including the project) · Level: requires Stage 1


Parts in this stage

Do them in order — each pattern builds on the ones before it, and all of them run against the same shopdb you loaded in Part 1.

Part Topic You will be able to Time
5 Joining tables Join any number of tables and predict the row count ~2 h
6 Subqueries, CTEs and set operations Structure hard questions as steps, and dodge the NOT IN trap ~2 h
7 Window functions Rankings, running totals and neighbour comparisons without collapsing rows ~2 h
8 Analytics patterns Top-N, growth, cohorts, funnels, streaks, medians, pivots, subtotals ~2.5 h
🏁 Project: The customer analytics pack RFM, cohort retention, rankings and a growth trend as one file ~4–5 h

How every part is organised

part-NN-topic/
├── README.md      ← 1. read the lesson, running every query as you go
├── examples/      ← 2. runnable .sql files for the lesson's examples (\i them)
├── exercises.md   ← 3. solve the practice questions yourself
└── solutions/     ← 4. only then compare with the solution .sql files

Every output printed in a lesson comes from a real run against PostgreSQL 17 — if your screen differs, your data drifted: psql -d shopdb -f shopdb.sql resets it.

How to study each part

  1. Predict the row count before every join — that habit alone catches most join bugs.
  2. Type every query; when a result surprises you, shrink the query until it doesn't.
  3. Do the exercises without peeking; the solutions explain the why in their comments.
  4. The patterns in Part 8 are interview staples — practise them until the shape comes without thinking.

Finish line

You are done with Stage 2 when your own analytics.sql matches every number in the project's self-check table. Then head to Stage 3: Advanced.

Search the guide