CertSafari
    Databricks Certified Data Analyst Associate· Lessons

    Domain 5 · Lesson 20/39

    Delta Lake Time Travel: Validate and Compare Historical Results

    Utilize Delta Lake to audit and view history, validate results, and compare historical results or trends.

    10 min read
    2.56% of exam
    4 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Query an earlier version of a Delta table with VERSION AS OF, TIMESTAMP AS OF or the @ syntax
    • Re-create an earlier result to validate it, and fix bad writes with time travel or RESTORE
    • Compare current and historical versions in one query to measure a trend
    • Explain which retention settings decide how far back time travel can reach

    1.Query a previous version with time travel

    Delta Lake keeps a numbered version for every write to a table. Time travel lets you query any of those versions, either by version number or by timestamp, as recorded in the transaction log. To use it, add a clause after the table name. Version numbers come straight from the version column of DESCRIBE HISTORY.

    Time travel by timestamp and by version numbersql
    SELECT * FROM people10m TIMESTAMP AS OF '2018-10-18T22:15:12.013Z';
    SELECT * FROM people10m VERSION AS OF 123;

    The timestamp can be any expression that is, or can be cast to, a timestamp: a literal string, a date string, current_timestamp() - interval 12 hours, or date_sub(current_date(), 1). Neither the timestamp nor the version can be a subquery, so you cannot compute the version with a nested SELECT. There is also a shorthand that puts the version or timestamp in the table name: @v123 for a version, or @ followed by a timestamp in yyyyMMddHHmmssSSS format.

    The @ shorthand for time travelsql
    -- Timestamp version
    SELECT * FROM people10m@20190101000000000
    -- Version number
    SELECT * FROM people10m@v123

    Checkpoint 1 of 5· Fill the gap

    Complete the query that reads version 123 of the table.

    SELECT * FROM people10m TIMESTAMP AS OF '2018-10-18T22:15:12.013Z';
    SELECT * FROM people10m  ?  AS OF 123;

    Checkpoint 2 of 5· Exam question

    A finance analyst wants to compare last Friday's `inventory_counts` table against the version created by yesterday's overnight OPTIMIZE job, to check whether any rows were unexpectedly added or removed between the two loads. Which approach lets them compare the row-level differences between two specific historical versions of the same table?

    Sources1

    2.Validate results and fix mistakes against an earlier version

    The documentation lists several uses for time travel. Two matter most to an analyst. The first is re-creating analyses, reports or outputs, which helps with debugging or auditing, especially in regulated industries. When a report figure is disputed, rerun its query against the version that existed when the report was produced and see whether you get the same number. The second is fixing mistakes in your data. You can do that by reading the good rows from an earlier version and writing them back.

    Re-insert one user's rows that were deleted by mistake, using yesterday's versionsql
    INSERT INTO my_table
      SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
      WHERE userId = 111

    For incorrect updates, the same pattern works with MERGE INTO my_table USING my_table TIMESTAMP AS OF ... as the source: matched rows are updated back to their earlier values. To roll back the whole table, RESTORE TABLE ... TO VERSION AS OF or TO TIMESTAMP AS OF returns it to an earlier state. RESTORE requires MODIFY permission on the table. It is also a data-changing operation, so it adds a new version and does not erase the history after the target. Downstream jobs may see the restored files as new data and produce duplicates.

    RESTORE by version or by timestampsql
    RESTORE TABLE target_table TO VERSION AS OF <version>;
    RESTORE TABLE target_table TO TIMESTAMP AS OF <timestamp>;

    Checkpoint 3 of 5· Check yourself

    Yesterday's batch deleted rows for userId 111 by mistake. Everything else in the table is correct. Which approach brings back only those rows?

    Sources12

    Because a historical version can be queried like any other table, one statement can compare the present with the past. The documented example counts the new customers added in the last week by subtracting last week's distinct user count from today's.

    Week-over-week growth: current version minus the version from seven days agosql
    SELECT
    (
      SELECT count(distinct userId)
      FROM my_table
    )
    -
    (
      SELECT count(distinct userId)
      FROM my_table TIMESTAMP AS OF date_sub(current_date(), 7)
    ) AS new_customers

    For repeated comparisons, you can save a snapshot as a temporary view, for example CREATE OR REPLACE TEMPORARY VIEW people_10k_v0 AS SELECT * FROM ... VERSION AS OF 0, and then join or aggregate it against the current table. Time travel also gives snapshot isolation for a set of queries on a fast-changing table. If every query in a comparison reads the same version, rows that arrive while the queries run cannot skew the result.

    Checkpoint 4 of 5· Exam question

    A pipeline owner is auditing `customer_events` and finds that `DESCRIBE HISTORY` only returns entries going back 30 days, even though the table has existed for over a year. Why does the history stop at that point by default?

    Sources13

    4.How far back you can travel: retention and VACUUM

    To query an old version you need both its log files and its data files. Two table properties control how long each is kept. logRetentionDuration, 30 days by default, controls how long history is kept. Log files are removed automatically after checkpointing, not by VACUUM. deletedFileRetentionDuration, 7 days by default, is the threshold VACUUM uses when it removes data files that the current version no longer references. Once VACUUM has removed those files, you can no longer query versions older than the retention period. In Databricks Runtime 18.0 and above, and in 12.2 and above for Unity Catalog managed tables, time travel requests older than deletedFileRetentionDuration are blocked.

    The two retention settings behind history and time travel
    PropertyDefaultControlsRemoved by
    logRetentionDurationinterval 30 daysHow long table history is keptAutomatic cleanup after checkpointing
    deletedFileRetentionDurationinterval 7 daysHow long unreferenced data files survive, which limits time travelVACUUM

    To compare 30 days of trends, set delta.deletedFileRetentionDuration to "interval 30 days" so it matches the log retention. Keeping more data files raises storage costs.

    Checkpoint 5 of 5· Match them up

    Match each item to its role in time travel retention

    Tap a term, then the definition that fits it.

    Sources14

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.Because DESCRIBE HISTORY shows 30 days of versions, you can time travel to any of them.Why is that wrong?

      History and time travel have separate thresholds. VACUUM removes unreferenced data files after 7 days by default, and the versions that need those files can then no longer be queried.

      Covered in How far back you can travel: retention and VACUUM

    2. 2.You can compute the version for VERSION AS OF with a subquery, such as the MAX(version) from history.Why is that wrong?

      Neither the version nor the timestamp expression can be a subquery. Read the version number from DESCRIBE HISTORY and pass it in as a literal.

      Covered in Query a previous version with time travel

    Practise it for real

    Use a table's history to measure how many new customers arrived in the last week

    1. 1.Run DESCRIBE HISTORY my_table and note the version and timestamp of the writes over the past seven days.

      Why: Time travel can only reach versions that still appear in history and whose data files have not been vacuumed.

      You should see: One row per write, newest first, with operation, userName and timestamp.

    2. 2.Run SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 7).

      Why: This confirms that last week's snapshot is still available before you build a comparison on it.

      You should see: The table as it was seven days ago. If its files were vacuumed, the query fails.

    3. 3.Run the new_customers query that subtracts count(distinct userId) at TIMESTAMP AS OF date_sub(current_date(), 7) from the current count.

      Why: Comparing two versions in one statement gives the trend directly.

      You should see: A single row with one column, new_customers.

    Stuck? Get a nudge

    If step 2 fails, check deletedFileRetentionDuration and whether VACUUM has run on the table.

    Sources

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Time travel supports querying previous table versions based on timestamp or table version”
      ↩︎ Query a previous version with time travel
      “version is a long value that can be obtained from the output of DESCRIBE HISTORY table_spec.”
      ↩︎ Query a previous version with time travel
      “The timestamp must be in yyyyMMddHHmmssSSS format.”
      ↩︎ Query a previous version with time travel
      “Re-creating analyses, reports, or outputs, such as the output of a machine learning model.”
      ↩︎ Validate results and fix mistakes against an earlier version
      “To restore a table, you must have MODIFY permission for the table.”
      ↩︎ Validate results and fix mistakes against an earlier version
      “Restore is a data-changing operation and might result in duplicate data for downstream workloads.”
      ↩︎ Validate results and fix mistakes against an earlier version
      “To query the number of new customers added over the last week:”
      ↩︎ Compare historical results and trends across versions
      “Providing snapshot isolation for a set of queries for fast changing tables.”
      ↩︎ Compare historical results and trends across versions
      “Data files are deleted when VACUUM runs against a table.”
      ↩︎ How far back you can travel: retention and VACUUM
      “Log files are removed automatically after checkpointing table versions.”
      ↩︎ How far back you can travel: retention and VACUUM
      “time travel queries are blocked if they request a version older than the deletedFileRetentionDuration table property (default 7 days).”
      ↩︎ How far back you can travel: retention and VACUUM
      “Increasing data retention threshold can cause your storage costs to go up, as more data files are maintained.”
      ↩︎ How far back you can travel: retention and VACUUM
      “Time travel and table history are controlled by different retention thresholds.”
      ↩︎ Exam trap 1
      “Neither timestamp_expression nor version can be subqueries.”
      ↩︎ Exam trap 2
      “To fix accidental deletes to a table for the user 111:”
      ↩︎ Checkpoint
      “Use only the past 7 days for time travel operations”
      ↩︎ Prediction
    2. 4.
      “The ability to query table versions older than the retention period is lost after running VACUUM.”
      ↩︎ How far back you can travel: retention and VACUUM
      “Log files are deleted automatically and asynchronously after checkpoint operations and are not governed by VACUUM.”
      ↩︎ Checkpoint

    Spotted a mistake, or was something unclear? Tell us.