PostgreSQL Tutorial 120 lessons · 1:49:08+ of video
PostgreSQL Home What Is PostgreSQL
What PostgreSQL Is Running Postgres in Docker psql: Your First Query Databases, Tables and psql
Databases, Schemas and Tables Meet Riverside Books psql Meta-commands Querying Rows
SELECT and WHERE ORDER BY and LIMIT NULL and COALESCE Numbers, Text and Booleans
Numbers: integer, numeric, double Text Types Booleans and Casting Dates and Times
Dates and Times timestamptz and Time Zones Intervals and date_trunc Creating Tables
CREATE TABLE Identity Columns Constraints Foreign Keys and RETURNING
Foreign Keys INSERT ... RETURNING UPDATE and DELETE ... RETURNING Upsert, Generated Columns, ALTER
Upsert with ON CONFLICT Generated Columns ALTER TABLE Joins, LATERAL and FILTER
Joins Refresher LATERAL Joins Aggregates and FILTER ROLLUP and CTEs
GROUPING SETS and ROLLUP CTEs Recursive CTEs Window Functions
Window Functions Top N per Group Running Totals and LAG DISTINCT ON, generate_series, Report
DISTINCT ON generate_series Practice: The Monthly Report Arrays
Arrays Array Operators unnest and array_agg JSONB
JSONB Basics Querying JSONB JSONB Containment Updating and Building JSON
Updating JSONB Building JSON JSONB or Columns? Enums, UUIDs and Ranges
Enums and Domains UUIDs Range Types Indexes and EXPLAIN
How Indexes Work B-tree Indexes EXPLAIN EXPLAIN ANALYZE and Smarter Indexes
EXPLAIN ANALYZE Multicolumn Indexes Partial and Expression Indexes GIN, BRIN and Index Mistakes
GIN Indexes BRIN Indexes Index Mistakes Full-Text Search
Full-Text Search tsvector and tsquery Ranking and Headlines Fast Search, Fuzzy Search, Practice
A Search Column and Index Fuzzy Search with pg_trgm Practice: Book Search Transactions and MVCC
Transactions Savepoints MVCC Isolation, Locks and Job Queues
Isolation Levels Locks and Deadlocks FOR UPDATE SKIP LOCKED Views and Schemas
Views Materialized Views Schemas and search_path Functions and Procedures
SQL Functions PL/pgSQL Procedures Triggers
Triggers An Audit Log Trigger Practice: Stock Trigger Roles, Privileges and Row-Level Security
Roles and Privileges Row-Level Security Practice: Shop Staff Access Postgres from Python
psycopg Basics Parameters and SQL Injection Transactions in Python COPY, Pools and a Python Report
COPY and Bulk Loading Connection Pools Practice: A Python Report Partitioning, VACUUM and Statistics
Partitioning VACUUM and Autovacuum Statistics and ANALYZE Finding and Fixing Slow Queries
pg_stat_statements A Slow Query Workflow Configuration Basics Backups and Replication
Backups with pg_dump Restore and Point-in-Time Recovery Replication NOTIFY, Extensions and pgvector
LISTEN and NOTIFY Extensions pgvector Vector Search in Postgres
Vector Search Vector Indexes Hybrid Search Job Queues, Mistakes and Alternatives
Postgres as a Job Queue Common Mistakes Postgres vs Other Databases Project: Harbor Events, Part 1
Project: Harbor Events, the Schema Project: Load and Check Project: Core Queries Project: Harbor Events, Part 2
Project: Reports Project: Search Project: Speed It Up Project: Harbor Events, Part 3
Project: Safe Bookings Project: Access Rules Project: Python Service Interview Questions and Quiz
Interview Questions, Part 1 Interview Questions, Part 2 Quick Quiz Cheat Sheet, Roadmap and Wrap-up
Cheat Sheet Your Roadmap Wrap-up PostgreSQL · Lesson 36 of 120
Practice: The Monthly Report Put it together.
⏱ 2 min read ▶ Video at 31:21 🎓 Free
▶ starts at 31:21 Let's build the owner's monthly report for the first quarter of twenty twenty five. Orders, revenue, the best selling genre, and growth from the month before.
Orders Revenue Top genre Growth Lines totals each order line by month and genre. Monthly sums them. Distinct on picks the top genre per month. Lag gives growth. Run it. Three months, in one readable query.
query.sql sql Copy
WITH lines AS (
SELECT date_trunc( 'month' , o . order_date ):: date AS month ,
b . genre , i . quantity * b . price AS amount, o . order_id
FROM orders o JOIN order_items i USING (order_id) JOIN books b USING (book_id)
WHERE o . order_date BETWEEN '2025-01-01' AND '2025-03-31' ),
monthly AS (
SELECT month , count ( DISTINCT order_id) AS orders, sum (amount) AS revenue
FROM lines GROUP BY month ),
top AS (
SELECT DISTINCT ON ( month ) month , genre FROM lines
GROUP BY month , genre ORDER BY month , sum (amount) DESC )
SELECT m . month , m . orders , m . revenue , t . genre AS top_genre,
round ( 100 * ( m . revenue / lag ( m . revenue ) OVER ( ORDER BY m . month ) - 1 ), 1 ) AS growth
FROM monthly m JOIN top t USING ( month ) ORDER BY m . month ; Output
month | orders | revenue | top_genre | growth
------------+--------+---------+------------+--------
2025-01-01 | 69 | 2120.24 | Technology |
2025-02-01 | 75 | 2465.13 | Technology | 16.3
2025-03-01 | 61 | 1952.82 | Thriller | -20.8
(3 rows) CTEs to name each step. Date trunc to group by month. Distinct on for the top genre. And lag for growth. Real reports are just these small pieces, put together.
key value CTEs named steps date_trunc months DISTINCT ON top genre lag() growth
Prefer to watch? The full PostgreSQL course is free All lessons in one video with animations, real code and chapters.
Watch free A real report is small steps, joined into one query. Next, Postgres arrays.
Key takeaways Next up:
Arrays — lists in a column