runtime
86 / 88

lesson 86 Warehouse & reporting projects · advanced

Stage, validate & atomically promote a refresh

Validate staged data before replacing a published result.

the idea

Refresh a published table in a transaction: BEGIN, delete the old rows, insert the staged rows, then COMMIT. Readers see either the old snapshot or the new one, never a half-empty table. If anything fails before COMMIT, ROLLBACK leaves the old snapshot intact.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

published_totals2 columns · 15 rows
customer_idtext · unique idtotal_salesdecimal
1810215331.08
1738912361.36
154828191.46
158385309.5
127983651.6
148662681.26
customer_idtext · unique id
18102
total_salesdecimal
15331.08
customer_idtext · unique id
17389
total_salesdecimal
12361.36
customer_idtext · unique id
15482
total_salesdecimal
8191.46
customer_idtext · unique id
15838
total_salesdecimal
5309.5
customer_idtext · unique id
12798
total_salesdecimal
3651.6
customer_idtext · unique id
14866
total_salesdecimal
2681.26

first 6 of 15 rows · your queries use all of them

refresh_stage2 columns · 15 rows
customer_idtexttotal_salesdecimal
1810211485.54
173895876.78
16440975.43
14895925.62
14418897.35
16839840.48
customer_idtext
18102
total_salesdecimal
11485.54
customer_idtext
17389
total_salesdecimal
5876.78
customer_idtext
16440
total_salesdecimal
975.43
customer_idtext
14895
total_salesdecimal
925.62
customer_idtext
14418
total_salesdecimal
897.35
customer_idtext
16839
total_salesdecimal
840.48

first 6 of 15 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Compare published and staged totals. published_totals is what readers see today; refresh_stage holds the next snapshot before you promote it in a transaction.the pattern

    example.sql
    SELECT
      p.customer_id,
      p.total_sales AS published,
      s.total_sales AS staged
    FROM published_totals p
    LEFT JOIN refresh_stage s ON s.customer_id = p.customer_id;
  2. Validate the stage before promotion. Check row count, null keys, and negative totals on refresh_stage so you do not commit a bad refresh.

    step-2.sql
    SELECT
      COUNT(*) AS rows,
      SUM(customer_id IS NULL) AS missing_keys,
      SUM(total_sales < 0) AS negative_totals
    FROM refresh_stage;
  3. Confirm rollback leaves data intact. DELETE inside BEGIN is undone by ROLLBACK—staged_rows should match the count before the failed experiment.

    step-3.sql
    BEGIN;
    DELETE FROM refresh_stage;
    ROLLBACK;
    SELECT COUNT(*) AS staged_rows
    FROM refresh_stage;

common mistake

Deleting and inserting without a transaction leaves an empty or partial table if the insert fails.

challenge 1

Simulate an interrupted promotion: BEGIN, DELETE all published_totals, INSERT the refresh_stage rows, then ROLLBACK.

Return customer_id and total_sales from published_totals (the November snapshot must remain).

what to return

Columns
customer_idtotal_sales in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Stage, validate & atomically promote a refresh — Warehouse & reporting projects · Runtime