SQL & PostgreSQL
Learn SQL from first SELECT to window functions, then design schemas, make queries fast with indexes and EXPLAIN, and understand transactions and concurrency. Finishes with the PostgreSQL admin basics every backend developer needs.
What you will be able to do
- Answer real business questions with joins, aggregates, CTEs and window functions
- Design normalised schemas with the right types and constraints
- Read EXPLAIN plans and fix slow queries with the right indexes
- Explain transactions, isolation levels and locking, and avoid common concurrency bugs
- Manage roles, backups, vacuum and monitoring on a PostgreSQL server
Roadmap
Bar length shows each module's share of study time.
- 110h4 topics
SQL basics
Get data out of a single table.
Show topicsNot startedRun Postgres locally or in Docker and navigate it with psql meta-commands.
- Run Postgres in Docker or locally
- Connect with psql
- Use \l, \dt and \d meta-commands
Filter, sort and page through rows with precise conditions.
- Select columns and aliases
- Filter with WHERE, AND, OR, IN
- Sort with ORDER BY
- Page with LIMIT and OFFSET
Handle NULLs correctly and transform strings, numbers and dates in queries.
- Handle NULL with IS NULL and COALESCE
- Write CASE expressions
- Use string and number functions
- Work with dates and intervals
Modify data safely and return the affected rows.
- Insert single and multiple rows
- Update rows safely with WHERE
- Delete rows inside a transaction
- Return affected rows with RETURNING
- 212h 30m4 topics
Joins & aggregation
Combine tables and summarise data.
Show topicsNot startedPick the right join and explain why rows appear or go missing.
- Write inner joins
- Use left and full outer joins
- Join a table to itself
- Explain missing and duplicated rows
Summarise data by group and filter on aggregate results.
- Use COUNT, SUM, AVG, MIN, MAX
- Group rows with GROUP BY
- Filter groups with HAVING
- Avoid common grouping mistakes
Combine and compare result sets.
- Combine results with UNION and UNION ALL
- Compare sets with INTERSECT and EXCEPT
- Deduplicate with DISTINCT
Write twenty reporting queries against a sample schema without looking up syntax.
- Load a sample schema
- Write sales and revenue reports
- Write top-N and per-group queries
- Finish twenty queries without lookups
Module resources:PostgreSQL Exercises ↗ - 314h4 topics
Advanced queries
The features that separate SQL users from SQL experts.
Show topicsNot startedBreak complex queries into readable CTEs and walk hierarchies recursively.
- Write scalar and correlated subqueries
- Use EXISTS and IN subqueries
- Refactor queries into CTEs
- Walk a hierarchy with a recursive CTE
Compute rankings, running totals and period-over-period changes.
- Use OVER and PARTITION BY
- Rank rows with ROW_NUMBER and RANK
- Compute running totals
- Compare periods with LAG and LEAD
- Use frame clauses for moving averages
Store, query and index semi-structured JSONB data.
- Store documents in JSONB
- Query with -> and ->> operators
- Filter with @> containment
- Index JSONB with GIN
- Work with array columns
Use INSERT ... ON CONFLICT and views to simplify application code.
- Upsert with INSERT ... ON CONFLICT
- Create views for common queries
- Create and refresh materialised views
- 411h4 topics
Schema design
Good schemas make every query easier.
Show topicsNot startedTurn requirements into tables with one-to-many and many-to-many relationships.
- Identify entities and attributes
- Model one-to-many relationships
- Model many-to-many with join tables
- Draw an ER diagram
Normalise to 3NF and justify any deliberate denormalisation.
- Apply 1NF, 2NF and 3NF
- Spot update anomalies
- Justify a deliberate denormalisation
Enforce data integrity with primary keys, foreign keys, CHECK and UNIQUE.
- Choose appropriate data types
- Pick natural or surrogate keys
- Add foreign keys with ON DELETE rules
- Enforce rules with CHECK and UNIQUE
Evolve a schema safely with versioned migrations and zero-downtime patterns.
- Set up a migration tool
- Write up and down migrations
- Add columns without downtime
- Backfill data in batches
- 513h4 topics
Indexes & performance
Why queries are slow and how to make them fast.
Show topicsNot startedPredict whether an index can be used for a given WHERE and ORDER BY.
- Explain B-tree structure
- Know when an index can be used
- Match indexes to ORDER BY
- See index-only scans
Spot sequential scans, bad estimates and expensive joins in a plan.
- Run EXPLAIN and EXPLAIN ANALYZE
- Read scan and join node types
- Compare estimated and actual rows
- Find the most expensive plan node
Choose an index type and column order for a real query workload.
- Order columns in composite indexes
- Create partial and expression indexes
- Use GIN for JSONB and full text
- Use BRIN for large time series
Rewrite slow queries and batch application queries to cut round trips.
- Rewrite a slow query and compare plans
- Detect N+1 query patterns
- Batch queries to cut round trips
- Measure before and after
- 68h3 topics
Transactions & concurrency
Correct results when many users write at once.
Show topicsNot startedGroup statements into transactions and use savepoints and rollbacks.
- Use BEGIN, COMMIT and ROLLBACK
- Explain each ACID property
- Use savepoints for partial rollback
Explain read committed, repeatable read and serializable and the anomalies each prevents.
- Explain how MVCC works
- Compare read committed and repeatable read
- Use serializable and handle retries
- Reproduce an anomaly in two sessions
Prevent lost updates and diagnose lock waits and deadlocks.
- Inspect locks in pg_locks
- Prevent lost updates with SELECT FOR UPDATE
- Reproduce and read a deadlock
- Keep transactions short
Module resources:Concurrency control ↗ - 711h5 topics
PostgreSQL admin basics
Keep a database healthy, secure and recoverable.
Show topicsNot startedCreate least-privilege roles for apps and humans.
- Create login and group roles
- Grant least-privilege permissions
- Enable row-level security policies
Take logical backups, restore them, and explain point-in-time recovery.
- Back up with pg_dump
- Restore with pg_restore
- Explain WAL archiving and PITR
- Script a scheduled backup
Explain why vacuum exists and check autovacuum is keeping up.
- Explain dead tuples and MVCC bloat
- Run VACUUM and ANALYZE
- Check autovacuum activity
Find the slowest and most frequent queries on a running server.
- Query pg_stat_activity
- Enable pg_stat_statements
- Find slowest and most frequent queries
Size connections with PgBouncer and tune memory settings sensibly.
- Explain why connections are expensive
- Set up PgBouncer
- Tune shared_buffers and work_mem
Module resources:roadmap.sh PostgreSQL DBA ↗ - 810h2 topics
Capstone project
Design, load, query and tune a real database.
Show topicsNot startedAn ER diagram and DDL for a realistic app schema reviewed against 3NF.
- Gather requirements for the app
- Draw the ER diagram
- Write DDL and review against 3NF
A repo with schema migrations, seed data, analytics queries, indexes justified by EXPLAIN and a backup script.
- Write versioned schema migrations
- Create realistic seed data
- Write analytics queries with CTEs and windows
- Add indexes justified by EXPLAIN
- Write a backup and restore script
- Document the schema in a README