Time-Travel & Temporal Tables in SQL
As-of snapshots without replaying everything
System-versioned temporal tables (FOR SYSTEM_TIME AS OF) answer as-of directly where supported. Portable validity ranges (valid_from/valid_to half-open containment: from <= t AND t < to) work everywhere. Event sourcing rebuilds by replay — full history at replay cost. Half-open containment is the portable core; append-only history keeps it trustworthy.
Half-open portable
from <= t AND t < to tiles time without gaps/overlaps — everywhere.
Temporal AS OF
FOR SYSTEM_TIME AS OF queries snapshots directly where supported.
Trigger validity
Writes close/open ranges; history append-only and partitioned.
Replay rarely
Event sourcing rebuilds at replay cost — audit-critical only.
1-- portable validity query:2SELECT * FROM prices WHERE product_id = 7 AND valid_from <= '2024-06-01' AND '2024-06-01' < valid_to;
| product_id | price |
|---|---|
| 7 | 99 |
June-1st falls inside exactly one validity row by half-open containment. Closed ends would double-count boundary instants or gap them.
June-1st price (portable)
Exactly one row contains the instant.
1SELECT * FROM prices WHERE product_id = 7 AND valid_from <= '2024-06-01' AND '2024-06-01' < valid_to;
| product_id | price |
|---|---|
| 7 | 99 |
Native AS OF twin
Engine-kept history, same question.
1-- SELECT * FROM orders FOR SYSTEM_TIME AS OF '2024-06-01';
| rows | source |
|---|---|
| as-of rows | engine history |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.