For working with data yourself
6 chapters · 18 lessonsAbout 2 h
Go from reading a database to shaping it. Build the queries day-to-day analysis needs, then create tables, indexes, and views of your own.
This course continues from SQL Introduction
The introductory course taught you to read a database with SQL. Now you shape one.
Every lesson starts from a situation you will meet at work: sales by month, customers who never ordered, month-over-month change. Solving it is what makes the new syntax necessary.
The database is a simulation checked byte for byte against real PostgreSQL 17, down to the query planner. When you add an index, it speeds things up (or does not) exactly as the real thing would.
CASE, dates, subqueries, CTEs, and window functions: the shapes real analysis is made of.
Types, constraints, and foreign keys keep bad data out from the start.
Read a query plan and see for yourself whether an index helped.
Beyond filter, summarize, and join: build the queries you will actually be asked for every day.
Group by month, rank rows, compare with the previous period. The everyday shapes of analysis, written by you.
Constraints, indexes, views. Build tables that cannot break, make them fast, and give the queries you use a name.
6 chapters · 18 lessons
CASE WHEN price >= 200 THEN 'premium' WHEN price >= 50 THEN 'standard' ELSE 'budget' END AS tierCASE status WHEN 'paid' THEN 'to ship' WHEN 'shipped' THEN 'done' ELSE status END AS statussum(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END)COALESCE(coupon_code, 'none')round( sum(CASE … END)::numeric * 100 / count(*), 1)date_trunc('month', order_date)::dateWHERE order_date >= current_date - interval '14 days'WHERE price > ( SELECT avg(price) FROM products)WHERE id IN ( SELECT customer_id FROM ordersWHERE code NOT IN ( SELECT coupon_code FROM orders WHERE coupon_code IS NOT NULL)WITH monthly AS ( SELECT … GROUP BY month) SELECT round(avg(sales), 2) FROM monthly;SELECT month, sales FROM monthly WHERE sales > ( SELECT avg(sales) FROM monthly)UNIONUNION ALLEXCEPTSELECT o.customer_id FROM orders o … UNION SELECT customer_id FROM page_views WHERE product_id = 16 AND customer_id IS NOT NULL;rank() OVER (ORDER BY units DESC)dense_rank() OVER (ORDER BY units DESC)row_number() OVER ( PARTITION BY category ORDER BY sales DESC)SELECT name, category, units, sales, place FROM in_category WHERE place <= 2lag(sales) OVER (ORDER BY month)sum(sales) OVER (ORDER BY month)sales - lag(sales) OVER (ORDER BY month)lead(sales) OVER (ORDER BY month)lag(sales) OVER ( PARTITION BY category ORDER BY month)CREATE TABLE reviews ( id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, … created_at timestamptz NOT NULL DEFAULT now() );INSERT INTO reviews (product_id, customer_id, rating, body) VALUES (9, 1, 5, 'Sharp and bright.');ALTER TABLE reviews ADD COLUMN title text;WHERE NOT EXISTS ( SELECT 1 FROM products p WHERE p.id = r.product_id)ALTER TABLE reviews ADD FOREIGN KEY (product_id) REFERENCES products (id);ADD CHECK (rating >= 1 AND rating <= 5)ADD UNIQUE (product_id, customer_id)DELETE FROM customers WHERE id = 13;CREATE INDEX ON page_views (product_id);EXPLAIN SELECT id, product_id FROM page_views WHERE viewed_on = '2026-08-15';EXPLAIN ANALYZE SELECT id, viewed_on FROM page_views WHERE product_id = 7;WHERE viewed_on::text = '2026-08-15'DROP INDEX orders_coupon_code_idx;CREATE VIEW in_stock AS SELECT id, name, price, stock FROM products WHERE stock > 0;UPDATE in_stock SET stock = 10 WHERE id = 10;Join thousands of developers who've mastered the terminal. Your first lesson awaits.
Get Started →