Skip to Content
Skip to content
Tutorials

Programming Tutorials

Standalone programming tutorials for solving one specific problem at a time. Each guide starts with the exact error message, failing behavior, or design constraint, gives you a minimal reproducible script or query, and walks through the fix with real output.

Pick a language below or filter by topic and difficulty when you need a quick, practical reference you can test locally and apply right away.

SQL
Turn Rows Into Columns With FILTER Level: Intermediate

Turn one row per status into one column per status with aggregate FILTER and compare it with CASE. After this, you can build a small pivot table without an extension.

SQL
Running Total With a Window Function Level: Intermediate

Add a running total with SUM and OVER, restart it for each group and avoid the default-frame surprise when dates tie.

SQL
Verify a CTE Rewrite Without Losing Duplicate Rows Level: Intermediate

Compare a query and its CTE rewrite in both directions with EXCEPT ALL, including duplicate and NULL rows that a plain set comparison can miss.

SQL
Store Timestamps With Time Zone Correctly Level: Intermediate

Choose timestamptz for a moment in time and see what the session time zone and AT TIME ZONE change. After this, the same moment shows correctly for every reader.

SQL
Top 3 Rows per Group Level: Intermediate

Rank rows inside each group and keep the first three, then decide what to do with ties and compare the LATERAL version.

SQL
Upsert With INSERT … ON CONFLICT Level: Intermediate

Insert a row or update the one that is already there in a single statement, using the conflict target, DO NOTHING, DO UPDATE and the EXCLUDED row.

SQL
Read EXPLAIN ANALYZE in 10 Minutes Level: Advanced

Run one slow query with EXPLAIN ANALYZE and read it with a five-question checklist: rows touched, filtered rows, estimates, loops and sort spills.

SQL
Find Slow Queries With pg_stat_statements Level: Advanced

Turn on pg_stat_statements in a Docker container and list the statements that cost the most in total. After this, you know which query to tune first, and you do not have to guess.

SQL
Create an Index Without Blocking Writes Level: Advanced

Build an index with CREATE INDEX CONCURRENTLY while a writer is active, and handle the transaction-block error and the invalid index that a failed build leaves.

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.