SQL for Writing Data: DDL, DML, Transactions & Upserts
Back to Write-Side SQL for Engineers

Lab: Build & Load stg_orders

CREATE, load 2k rows, correct, and prove replay-safety.

61 minutesProject submission
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Assignment Instructions

Brief

Create the staging table, load it from a SELECT, correct it, and prove the load is replay-safe. Work in your Postgres sandbox.

Tasks

  1. CREATE stg_orders with: BIGINT PK, NOT NULL customer + timestamp, non-negative CHECK on amount, IF NOT EXISTS.
  2. INSERT..SELECT 2,000 rows from the provided raw_orders_extract seed (fixture attached) with an explicit column list.
  3. UPDATE the miscoded status='tset' rows to 'test' using SELECT-first discipline (paste both statements).
  4. Replay proof: run your INSERT a second time with ON CONFLICT DO NOTHING and show row count unchanged (SELECT COUNT(*) before/after).
  5. ALTER: add nullable coupon_code TEXT with IF NOT EXISTS.

Acceptance (deterministic checks before review)

  • stg_orders exists with PK + CHECK intact.
  • Row count == seed count after replay (no duplicates).
  • coupon_code column present, all NULL.
  • Both SELECT-first statements pasted alongside each write.

Starter

Fixture raw_orders_extract.csv + skeleton .sql with TODO markers are attached.

Checking your access…