Sub topic 4
Querying earlier versions by version or timestamp
Delta Lake lets you read a table as it was at an earlier commit. This feature is called time travel. This chapter shows how to pick a version, how to query it in SQL and PySpark, and which errors to expect.
This chapter assumes you can write basic SQL and PySpark. It also assumes you know that every commit to a Delta table creates a numbered version, and that DESCRIBE HISTORY lists those versions. The examples use a table named main.sales.orders.
The working model
A query on a Delta table normally reads the latest version. Time travel changes which version the query reads. Delta looks up the files that belonged to that version in the transaction log and reads only those files. Nothing is copied and nothing in the table changes.
You can name the version in two ways:
- By version number, such as
5. Use this when you have already looked atDESCRIBE HISTORYand know exactly which commit you want. - By timestamp, such as
2026-11-01 00:00:00. Use this when you know when something was correct, but not which commit was current then.
Querying by version in SQL
Add VERSION AS OF after the table name:
SELECT * FROM main.sales.orders VERSION AS OF 5;The result is the table exactly as it was after commit 5. You can use the clause anywhere a table reference is allowed, so filters, joins, and aggregations work as usual:
SELECT status, COUNT(*) AS order_count
FROM main.sales.orders VERSION AS OF 5
WHERE order_date >= '2026-10-01'
GROUP BY status;Querying by timestamp in SQL
Use TIMESTAMP AS OF with a timestamp string:
SELECT * FROM main.sales.orders TIMESTAMP AS OF '2026-11-01 00:00:00';Delta returns the latest version that was committed at or before that timestamp. If commits 7 and 8 happened on 2026-10-31 and commit 9 happened on 2026-11-02, this query reads version 8.
Timestamps are compared against the commit time recorded in the log. Be careful with time zones. A bare string such as '2026-11-01 00:00:00' is interpreted in the session time zone. If your session and your colleagues' sessions differ, the same string can select different versions. Check the current setting with SET spark.sql.session.timeZone;, or confirm the commit times with DESCRIBE HISTORY before you rely on a timestamp.
Querying in PySpark
The DataFrame reader takes the same information as options. Use versionAsOf for a version number and timestampAsOf for a timestamp:
df_v5 = spark.read.option("versionAsOf", 5).table("main.sales.orders")
df_nov = spark.read.option("timestampAsOf", "2026-11-01").table("main.sales.orders")
df_v5.show(5)
print(df_nov.count())A date alone, as in "2026-11-01", means midnight at the start of that day. The returned DataFrame is a normal DataFrame, so you can filter it, join it, or write it somewhere else.
Picking the right version
Use DESCRIBE HISTORY first when you are not sure which version you need:
DESCRIBE HISTORY main.sales.orders LIMIT 10;Read the version, timestamp, and operation columns. If a bad DELETE ran at version 9, then version 8 is the last good state. Querying VERSION AS OF 8 gives you the data from before the delete.
The table below summarizes when each form fits.
| Situation | Better choice |
|---|---|
| You found a specific bad commit in the history | `VERSION AS OF` the commit before it |
| A report was correct at a known time | `TIMESTAMP AS OF` that time |
| A job must be repeatable and exact | `VERSION AS OF`, because a version number never shifts with time zones |
Common errors
Most failures come from history that no longer exists or from values outside the table's range.
- Version not found. You asked for a version that was never created, or whose log entries have been cleaned up. Check the available range with
DESCRIBE HISTORY. - Timestamp before the first commit. A timestamp earlier than the oldest available commit fails, because no version existed then.
- Timestamp after the last commit. Depending on the runtime, a timestamp later than the latest commit can fail with an error that says the timestamp is after the most recent commit. Use a timestamp at or before the latest commit time, or query the table without time travel.
- Missing data files. The log may still list a version, but
VACUUMmay have deleted the data files it needs. The query then fails when it tries to read them. A later chapter covers retention andVACUUMin detail. - Schema differences. A time travel query uses the schema recorded for that version, not the current schema. Columns you added later will be absent, and columns you dropped will reappear.
Exercises
Exercise
A table has commits at version 7 (2026-10-31 09:00), version 8 (2026-10-31 17:00), and version 9 (2026-11-02 08:00). Which version does SELECT * FROM main.sales.orders TIMESTAMP AS OF '2026-11-01 00:00:00' read, assuming the session time zone matches the commit times?
Show answer
Version 8. Delta reads the latest version committed at or before the given timestamp. Version 9 was committed after it, so it is not used.
Exercise
Write PySpark code that loads version 12 of main.sales.orders into a DataFrame named df_old and prints its row count.
Show answer
dfold = spark.read.option("versionAsOf", 12).table("main.sales.orders") print(dfold.count())