Skip to Content
Skip to content
PROGRAMMING

SQL Tutorials

Focused SQL and PostgreSQL tutorials for diagnosing real query and operational issues—including GROUP BY errors, N+1 query patterns, transaction deadlocks, missing indexes, and proving a pg_dump backup actually restores cleanly.

Every tutorial includes reproducible table DDL, sample rows, and exact query or EXPLAIN ANALYZE output.

SQL
Partial Index for Active Rows Only Level: Advanced

Index only the rows that queries ask for, compare the size with a full index, and enforce a unique rule for active rows.

SQL
Index a JSONB Column With GIN Level: Advanced

Add a GIN index to a JSONB column, filter with the contains operator, and compare it with an expression index and jsonb_path_ops.

SQL
Find Unused and Duplicate Indexes Level: Advanced

Read pg_stat_user_indexes to find indexes that were never scanned, find identical indexes in pg_index, and drop them with DROP INDEX CONCURRENTLY.

SQL
Find and Fix a Deadlock Level: Advanced

Reproduce a deadlock between two money transfers, read the message, and fix it by locking rows in the same order.

SQL
Process a Job Queue With SKIP LOCKED Level: Advanced

Let two workers take different jobs from one table with FOR UPDATE SKIP LOCKED, and see what happens to a job when a worker fails.

SQL
Add a NOT NULL Column to a Big Table Without Downtime Level: Advanced

Add a NOT NULL column to a big table with a constant default, or with a nullable column, a batch backfill, NOT VALID and VALIDATE.

SQL
Walk a Tree With a Recursive CTE Level: Advanced

Walk down an employee tree, walk up to the top, count each manager's team and guard the query against a loop with CYCLE.

SQL
Back Up With pg_dump and Prove the Restore Works Level: Advanced

Take a pg_dump backup in Docker, restore it into a scratch database, compare the tables and see what a damaged backup looks like.