Overview

Quick Setup

docker run --rm -d --name pg-practice -e POSTGRES_PASSWORD=x -p 55432:5432 postgres:17
docker exec -it pg-practice psql -U postgres
# paste the "Sample Schema" and "Seed Data" blocks from E-commerce Practice Queries

1. Fundamentals

  1. DDL Data Types and Constraints — CREATE/ALTER/DROP, identity vs serial, text vs varchar, numeric, timestamptz, uuid, jsonb, arrays, PK/FK/UNIQUE/CHECK
  2. SELECT Fundamentals — logical processing order, WHERE, ORDER BY, OFFSET vs keyset pagination, DISTINCT ON, NULL / three-valued logic, CASE
  3. Aggregation and GROUP BY — GROUP BY, HAVING, FILTER, GROUPING SETS / ROLLUP / CUBE, string_agg / array_agg, fan-out

2. Combining Data

  1. Joins — inner/outer/cross/self joins, ON vs WHERE, semi/anti joins, NOT IN pitfall, LATERAL
  2. Subqueries and CTEs — correlated subqueries, EXISTS/IN/ANY/ALL, WITH, recursive CTEs, MATERIALIZED
  3. Window Functions — ROW_NUMBER/RANK/DENSE_RANK, LAG/LEAD, running totals, ROWS vs RANGE, top-N, gaps & islands

3. Writing Data and Concurrency

  1. Inserts Updates Upserts and MERGE — RETURNING, ON CONFLICT, UPDATE … FROM, DELETE … USING, MERGE, COPY
  2. Transactions and Locking — BEGIN/SAVEPOINT, isolation levels, FOR UPDATE, SKIP LOCKED job queue, advisory locks

4. Performance and Operations

  1. Indexes and Query Performance — B-tree/partial/expression/INCLUDE/GIN, CONCURRENTLY, EXPLAIN ANALYZE, anti-patterns
  2. Admin and Inspection Queries — pg_stat_activity, killing queries, locks, pg_stat_statements, sizes, vacuum health

5. Postgres Features

  1. JSONB and Arrays — ->, ->>, @>, ?, JSONPath, SQL/JSON (PG 17), GIN indexing, array operators

  2. Views Functions and Triggers — views, materialized views + REFRESH CONCURRENTLY, SQL/PL/pgSQL functions, procedures, triggers

  3. UUID Versions — v1–v8, why v4 hurts B-tree PKs (measured), v7 for keys, uuidv7() (PG 18), v7 function for PG 17

6. Practice

  1. E-commerce Practice Queries — schema, seed data, 20 graded questions (easy → hard) with solutions

Note Map

#NoteGroupKey Interview Topics
1DDL Data Types and ConstraintsFundamentalsserial vs identity, FK ON DELETE, NULL in CHECK/UNIQUE
2SELECT FundamentalsFundamentalsProcessing order, keyset pagination, NULL logic
3Aggregation and GROUP BYFundamentalsWHERE vs HAVING, FILTER, ROLLUP
4JoinsCombiningLEFT JOIN filters, NOT IN vs NOT EXISTS, LATERAL
5Subqueries and CTEsCombiningCorrelated subqueries, recursive org chart
6Window FunctionsCombiningTop-N per group, running totals, LAG
7Inserts Updates Upserts and MERGEWritingUPSERT, MERGE, bulk load
8Transactions and LockingWritingIsolation levels, SKIP LOCKED, lost updates
9Indexes and Query PerformancePerformanceWhy isn’t my index used, EXPLAIN
10Admin and Inspection QueriesPerformanceFind & kill blockers, slow query log
11JSONB and ArraysFeaturesjson vs jsonb, GIN
12Views Functions and TriggersFeaturesMatview refresh, trigger use cases
13UUID VersionsFeaturesUUIDv4 vs v7 as PK, bigint vs UUID
14E-commerce Practice QueriesPracticeAll of the above

Postgres vs Standard SQL Cheat Sheet

FeaturePostgresStandard / Other DBs
Auto IDsGENERATED … AS IDENTITY (or legacy serial)Identity (std), AUTO_INCREMENT (MySQL)
Return Written RowsRETURNING on INSERT/UPDATE/DELETE/MERGEOUTPUT (SQL Server), none in MySQL
UpsertON CONFLICT DO UPDATEMERGE (std), ON DUPLICATE KEY (MySQL)
First Row per GroupDISTINCT ONROW_NUMBER()
Case-insensitive LIKEILIKElower(x) LIKE …
Conditional AggregateFILTER (WHERE …) (std, MySQL lacks it)sum(CASE …)
Castingx::intCAST(x AS int)
NULL-safe EqualityIS [NOT] DISTINCT FROM<=> (MySQL)
Transactional DDLYesNo in MySQL/Oracle
Arrays / JSONBFirst-class types with indexesLimited

Summary

  • Start: Load the practice schema, then work through the notes in order 1 → 13
  • Interview Focus: NULL logic, joins vs anti-joins, window functions, CTEs, UPSERT, isolation & locking, index anti-patterns
  • Real Work Focus: Keyset pagination, RETURNING, SKIP LOCKED, CREATE INDEX CONCURRENTLY, EXPLAIN (ANALYZE, BUFFERS), pg_stat_statements