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.
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.
-- Timestamp version
SELECT * FROM people10m@20190101000000000
-- Version number
SELECT * FROM people10m@v123Checkpoint 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;Time travel by number uses VERSION AS OF, and the number comes from DESCRIBE HISTORY.
Source: docs.databricks.comCheckpoint 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?
Correct answer: A — Query each version separately with `VERSION AS OF`, then use `EXCEPT` between the two result sets to isolate rows that differ
- A. Correct: selecting each version with `VERSION AS OF` and applying `EXCEPT` returns the actual rows present in one snapshot but not the other, giving a precise row-level comparison between the two loads.
- B. Incorrect: `operationMetrics` gives aggregate counts for what a single commit changed, not a row-level diff between two arbitrary historical versions, so it can only approximate rather than isolate the differing rows.
- C. Incorrect: re-running `OPTIMIZE` compacts and reorders data files for file-layout efficiency; it creates a new version and does not compare or preserve the two versions the analyst is trying to examine.
- D. Incorrect: `ANALYZE TABLE ... COMPUTE STATISTICS` refreshes summary statistics used by the query optimizer for the current table state; it does not retain or expose a comparable snapshot of last week's data.
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.
INSERT INTO my_table
SELECT * FROM my_table TIMESTAMP AS OF date_sub(current_date(), 1)
WHERE userId = 111For 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 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?
Inserting the filtered rows from yesterday's version fixes just the affected user. RESTORE would roll back every change made since yesterday.
“To fix accidental deletes to a table for the user 111:”Source: docs.databricks.com
3.Compare historical results and trends across versions
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.
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_customersFor 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?
Correct answer: A — The `logRetentionDuration` table property defaults to 30 days and controls how long transaction log entries are kept before older ones are cleaned up
- A. Correct: the `delta.logRetentionDuration` table property defaults to 30 days, after which older commit entries in the transaction log become eligible for cleanup, which is why `DESCRIBE HISTORY` no longer shows them.
- B. Incorrect: Delta Lake does not move old commits into a separate audit table; entries are simply retained in the transaction log for the configured duration and then eligible for removal, not relocated.
- C. Incorrect: `DESCRIBE HISTORY` reads directly from the Delta transaction log on the table's storage location, not from a SQL warehouse query-history cache, so warehouse caching settings do not explain the cutoff.
- D. Incorrect: Unity Catalog governs access control and discovery metadata for the table, but the length of Delta's commit history is controlled by the table's own `logRetentionDuration` property, not a fixed Unity Catalog policy.
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.
| Property | Default | Controls | Removed by |
|---|---|---|---|
| logRetentionDuration | interval 30 days | How long table history is kept | Automatic cleanup after checkpointing |
| deletedFileRetentionDuration | interval 7 days | How long unreferenced data files survive, which limits time travel | VACUUM |
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.
History depends on the log files, which are cleaned up after checkpointing. Time travel also needs the data files, which VACUUM removes after deletedFileRetentionDuration.
“Log files are deleted automatically and asynchronously after checkpoint operations and are not governed by VACUUM.”Source: docs.databricks.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
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.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.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.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.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.https://docs.databricks.com/aws/en/tables/historyOfficial docs
“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.
“Restores a Delta table to an earlier state.”
↩︎ Validate results and fix mistakes against an earlier version - 3.https://docs.databricks.com/aws/en/delta/tutorialOfficial docs
“CREATE OR REPLACE TEMPORARY VIEW people_10k_v0 AS”
↩︎ Compare historical results and trends across versions - 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