Module 14 · Lesson 3 of 12Detailed study · about 25 min

Choose the right historical boundary: AT and BEFORE

AT includes the specified boundary; BEFORE excludes the referenced statement’s change when reading the retained state before its completion.

Your learning goal

Capture an incident query ID, compare AT with BEFORE, and choose repeatable timestamp boundaries without confusing a query ID with an order ID.

Understand the idea

A concrete example

If DELETE D removes west orders, AT D shows the deletion, while BEFORE D lets you inspect the missing rows. A diagnostic SELECT issued after D has a different ID.

Key ideas in simple English
ConceptWhat it means
STATEMENTA Snowflake query ID, not ORDER_ID; capture it immediately.
BEFORE incidentRead before that statement completes.
AT incidentInclude that statement’s change in the single-writer example.
OFFSETNegative seconds relative to now; not a stable incident marker.

Remember: Use a saved incident identifier or an explicitly typed timestamp, and document transaction and concurrency assumptions.

Explain it step by step · full theory

Learning objectives and prerequisites

Capture an incident query ID, compare AT with BEFORE, and choose repeatable timestamp boundaries without confusing a query ID with an order ID.

Build on Modules 02–04: storage, table types, SELECT, DML and transactions. Module 13 explains why recovery must preserve valid business history. Use the first lesson’s isolated setup; examples name separate demonstration tables so the baseline ORDERS stays unchanged.

A boundary is part of your recovery logic

Start with a single-writer, autocommit exercise so the difference is visible. The bad DELETE removes two of four rows. In that controlled sequence, AT the DELETE ID returns two rows; BEFORE it returns four. Neither query edits the live table.

BEFORE STATEMENT means before completion, not necessarily before start. Another transaction can commit while the incident statement is running. If you require an earlier instant, investigate timestamps and transaction boundaries instead of assuming BEFORE excludes all concurrent work.

Timestamp and offset syntax

A timestamp literal should be explicitly typed, preferably TIMESTAMP_TZ with an offset when communicating an incident across teams. Session time zone and timestamp type are part of interpretation. A fixed timestamp makes a repeatable reference if the data remains eligible.

OFFSET is measured in seconds from the current time. OFFSET => -60 means roughly one minute ago, not one minute before the last DELETE. It can fail if the table did not exist then. Avoid fixed-offset recovery scripts whose target silently moves on reruns.

Capture IDs without losing the incident

Run the DELETE and the immediately following SET together in the same worksheet session. LAST_QUERY_ID refers to session statement history; another statement between them changes what it returns. Save the printed ID in your incident notes. Session variables disappear when the session ends.

For this lesson use a DML query ID. STATEMENT lookup has a 14-day limit even when a table has longer retained history; use an appropriate timestamp for an older incident. A valid query ID does not bypass the table’s own retention window.

What history does not promise

Current access requirements still apply when reading historical data. Time Travel is not a way to bypass a revoked permission or recover arbitrary historical access rules. Historical SELECT uses the current table definition, so schema changes can affect the columns you see.

If a query fails, inspect the exact table identity, creation time, retained period, time zone and the selected statement. CREATE OR REPLACE creates a new object identity; a familiar name alone does not prove you are looking at the old table. Avoid DDL changes during the recovery exercise.

Production decisions and common mistakes

Use a saved incident identifier or an explicitly typed timestamp, and document transaction and concurrency assumptions.

Do not save LAST_QUERY_ID after running several exploratory SELECTs and assume it still identifies the bad DELETE.

Key takeaways

The reference moves with the current time. Save the original statement ID and timestamp, verify their eligibility, and read the historical state before deciding what to repair.

Words used in this lesson
Query ID
Identifier of an executed statement.
Autocommit
A successful standalone DML statement commits automatically unless inside an explicit transaction.
TIMESTAMP_TZ
Timestamp value carrying a time-zone offset.
Boundary
The chosen instant or statement transition for the historical read.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 4

Choose the right historical boundary: AT and BEFORE

Before D
  • 101
  • 102
  • 103
  • 104
Historical / retained
After D
  • 102
  • 104
Changed / needs review

Choose the incident statement

For this single-writer autocommit demonstration, DELETE D removes 101 and 103. Save D immediately; later diagnostic SELECT statements have different IDs.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Choose the incident statement

    For this single-writer autocommit demonstration, DELETE D removes 101 and 103. Save D immediately; later diagnostic SELECT statements have different IDs.

    • Before D: 101; 102; 103; 104
    • After D: 102; 104
  2. BEFORE D excludes D

    BEFORE(STATEMENT => D) reads just before D completes. It can include another transaction committed while D was running; it is not necessarily D’s start-time snapshot.

    • BEFORE D result: 101; 102; 103; 104
    • Boundary: Exclude D’s changes; Other committed work may appear
  3. AT D includes D

    AT(STATEMENT => D) includes the completed statement’s changes in this controlled example, so it cannot recover rows deleted by D.

    • AT D result: 102; 104
    • Choose deliberately: AT is inclusive; BEFORE excludes the incident
  4. Timestamp and offset alternatives

    A typed timestamp is a repeatable instant. A negative offset is measured from each query’s current time, so rerunning the same offset later selects a different instant.

    • Timestamp: Explicit time zone; Within retained history
    • Offset: Seconds before now; Moving recovery point

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Manual Snowflake practice only; SQL has not been executed for this course. Use an authorized warehouse and role with access to L2_OBJECTS_LAB and CREATE SCHEMA/CREATE TABLE as needed. Run in M14_LAB, in one session with one writer, using autocommit unless a transaction is shown. Run CREATE once, never replace an existing production object. Standard Edition with one-day retention is sufficient for the main lab; longer retention and administrative monitoring have separate requirements.

Make an incident and compare boundaries

Definition: A saved query ID anchors the comparison to the specific DELETE.

When to use: Run this new demonstration once after lesson 1.

SQL · example
USE SCHEMA L2_OBJECTS_LAB.M14_LAB;
CREATE TABLE SELECTOR_ORDERS CLONE ORDERS;
DELETE FROM SELECTOR_ORDERS WHERE REGION='west';
SET selector_delete_id = LAST_QUERY_ID();
SELECT $selector_delete_id AS INCIDENT_ID;
SELECT 'BEFORE' AS BOUNDARY, COUNT(*) AS N, SUM(AMOUNT) AS TOTAL
FROM SELECTOR_ORDERS BEFORE(STATEMENT => $selector_delete_id)
UNION ALL
SELECT 'AT', COUNT(*), SUM(AMOUNT)
FROM SELECTOR_ORDERS AT(STATEMENT => $selector_delete_id);

What to expect and why

The snapshot before the DELETE includes both west orders. AT includes their removal. Results below assume no other writes and the unchanged fixture.

Expected result for the supplied input (not live output)
BOUNDARYNTOTAL
BEFORE4500.00
AT2250.00

Timestamp and offset alternatives

Definition: Choose a typed instant that lies after table creation and within available history.

When to use: For an incident whose time you have confirmed in Query History.

SQL · example
-- Template: replace the timestamp with a real eligible point from YOUR run.
-- SELECT * FROM SELECTOR_ORDERS
-- AT(TIMESTAMP => '2026-11-25 10:00:00 +00:00'::TIMESTAMP_TZ);

-- Optional only after this table has existed for over one minute:
-- SELECT * FROM SELECTOR_ORDERS AT(OFFSET => -60);

-- Reproducible result for this run, unlike a moving offset:
SELECT ORDER_ID, AMOUNT
FROM SELECTOR_ORDERS BEFORE(STATEMENT => $selector_delete_id)
ORDER BY ORDER_ID;

What to expect and why

The uncommented query returns the original four orders. The timestamp and offset are teaching templates, not promised recovery points for your account.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Which query excludes the bad DELETE in this single-writer exercise?

Try it yourself

Explain why repeating OFFSET => -3600 tomorrow is not a reproducible incident investigation.

Compare your reasoning

The reference moves with the current time. Save the original statement ID and timestamp, verify their eligibility, and read the historical state before deciding what to repair.

Key takeaway

Use a saved incident identifier or an explicitly typed timestamp, and document transaction and concurrency assumptions.

Connect this lesson to the learning path

Official sources and curriculum

Module 14 · See module scope

By Rohit Ray · Independent educational lesson · Explore this visual lab