Skip to course content
Free SQL course

SQL for Data Analysis and AI

Unit 11.04: Making the notebook rerun cleanly top to bottom

Unit ID: SQL-M11-U05 - Estimated active time: 12-15 minutes Objective: eliminate hidden state so an analysis reproduces exactly.

Out-of-order execution is the reproducibility killer

Notebooks let you run cells in any order. The variables in memory then reflect a sequence nobody recorded

The result looks correct on your screen and cannot be reproduced by anyone, including you tomorrow.

The only real test

Restart the kernel and run all cells, top to bottom. If the output differs - or errors - the notebook was depending on hidden state.

Do this before sharing anything. Every time.

Structure that survives a restart

# Cell 1 - setup, no analysis
import duckdb, pandas as pd
con = duckdb.connect()
con.execute(open("schema.sql").read())
con.execute(open("seed.sql").read())

# Cell 2 - validate the inputs before using them
n_orders = con.execute("SELECT COUNT(*) FROM orders").fetchone()[0]
assert n_orders == 1000, f"expected 1000 orders, got {n_orders}"
print("orders:", n_orders)

# Cell 3 - the analysis
df = con.execute("""
    SELECT status, COUNT(*) AS orders, SUM(order_total) AS revenue
    FROM orders GROUP BY status ORDER BY revenue DESC
""").df()
df

The assert in cell 2 is the important part. If the data changes underneath the notebook, it fails immediately and loudly rather than producing a quietly different answer.

Rules that make notebooks reproducible

  1. One direction. Cells run top to bottom; never rely on having run something above out of order.
  2. No manual edits to data. Anything typed by hand cannot be reproduced. Put it in the query.
  3. Assert your assumptions. Row counts and totals, as above.
  4. Seed any randomness, so sampling repeats.
  5. Restart-and-run-all before sharing. Non-negotiable.

Connecting it to the evidence note

A reproducible notebook is what makes the Module 1 evidence note verifiable. The note says what you found; the notebook lets someone else get the same number. Without reproducibility the note is a claim rather than evidence.

Practice

Your notebook produces ₹26,85,905. A colleague runs it and gets ₹27,01,463. Name the two most likely causes.

Check your answer
  1. Hidden state. You ran a cell defining a status = 'completed' filter, then edited or deleted it.

Your session still held the filtered frame; their fresh run did not. ₹27,01,463 is the unfiltered total.

  1. A changed input. The underlying data moved between the two runs, and nothing asserted the expected

row count.

Both are fixed by the same practice: restart-and-run-all, plus an assert on the population.

Takeaway

Restart and run all before sharing, and assert your row counts. A notebook that cannot reproduce its own number is not evidence.

---