Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 11 / Stage 3

Changing data (DML)

Goal: Write data like a professional — multi-row and query-fed INSERTs, computed and joined UPDATEs, safe DELETEs, RETURNING receipts, upserts, MERGE, and bulk loads with COPY — plus the two habits that make destructive statements boring. Time: ~2.5 hours

What you will learn

  • INSERT: one row, many rows, and INSERT ... SELECT
  • UPDATE: conditional, computed from old values, and UPDATE ... FROM
  • DELETE (and DELETE ... USING), vs TRUNCATE
  • RETURNING: every write its own receipt
  • Upsert: ON CONFLICT DO NOTHING / DO UPDATE with EXCLUDED
  • MERGE: insert-update-delete in one statement
  • Bulk loading: COPY vs \copy
  • The safety habits: a pre-flight SELECT, and BEGIN, inspect, then COMMIT or ROLLBACK

Everything runs on shopdb — and since this part changes it, remember the reset: psql -d shopdb -f shopdb.sql.

Topics in this part

Practice

SQL workbench — write and run queries here

Press Run and PostgreSQL loads right in your browser (a one-time download of about 16 MB; after that it works offline). It comes seeded with shopdb, and statements share one session per page — like a psql window. Reset data rebuilds shopdb. Your queries never leave this page. Lessons were captured on PostgreSQL 17; the in-browser server is PostgreSQL 19 (via PGlite), so the rare output detail can differ.

Each topic above carries its own exercise and solution — open a topic and scroll to its exercise. All 7 exercises are also listed on the SQL Practice page.

Common mistakes

Mistake What happens Fix
UPDATE/DELETE without pre-flight or BEGIN One typo, whole table changed SELECT the WHERE first; BEGIN, inspect, COMMIT
Ignoring the UPDATE n tag Wrong blast radius unnoticed Expected 2, saw 7? Investigate before COMMIT
SELECT-after-INSERT for the new id Racy and slower RETURNING
Check-then-insert "upserts" Duplicate-key errors under load ON CONFLICT
Row-by-row INSERT loops for files Minutes instead of seconds \copy into staging, then INSERT ... SELECT
TRUNCATE as a casual DELETE Strongest lock, no row triggers, no WHERE ever TRUNCATE only for deliberate full wipes

Quick recap

  • INSERT always takes an explicit column list, and INSERT ... SELECT handles anything query-shaped.
  • UPDATE computes from old values and joins via FROM; read the row-count tag.
  • DELETE takes USING for joins; TRUNCATE wipes wholesale; both bow to the two habits (pre-flight SELECT, BEGIN/inspect/COMMIT).
  • RETURNING makes every write report itself — ids, audit trails, receipts.
  • ON CONFLICT upserts atomically, with EXCLUDED standing for the proposed row; MERGE reconciles whole sets with per-branch logic.
  • COPY/\copy move files in bulk; stage, validate, then merge.

Checklist

Before Part 12, make sure you can:

  • Feed an INSERT from a query with a join in it.
  • Raise a category's prices 10% and prove exactly what changed, in one statement.
  • Demonstrate from memory that BEGIN, damage, ROLLBACK leaves the data intact.
  • Write the stock-merging upsert without looking, EXCLUDED and all.
  • Say when MERGE beats ON CONFLICT, and the concurrency caveat.
  • Load a CSV with \copy into staging and merge it properly.

Read more: the official INSERT, UPDATE, MERGE and COPY references.

Search the guide