In two sentences
An as-of join attaches to each event the value that was true at that moment: the price when the order was placed, the exchange rate on the day of the payment, the customer's plan when they churned. It joins on a time range instead of an equal key, and it's what keeps reports and ML features from using information that didn't exist yet.
See it
Price changes for product p1, turned into validity intervals with lead():
| price | valid_from | valid_to |
|---|---|---|
| 10 | 04-01 | 04-05 |
| 12 | 04-05 | 04-09 |
| 11 | 04-09 | NULL (still valid) |
Each order picks the interval its time falls in:
| order_id | order_ts | price |
|---|---|---|
| 4 | 03-30 08:00 | NULL (before the first price) |
| 1 | 04-03 12:00 | 10 |
| 2 | 04-05 00:00 | 12 (the new price starts that instant) |
| 3 | 04-10 08:00 | 11 |
Joining on today's price would give every order 11.
In SQL
Change rows to intervals, then a range join. valid_from is inclusive, valid_to exclusive:
WITH prices AS (
SELECT product_id, price,
changed_at AS valid_from,
lead(changed_at) OVER (PARTITION BY product_id ORDER BY changed_at) AS valid_to
FROM price_changes
)
SELECT o.order_id, o.order_ts, p.price
FROM orders o
LEFT JOIN prices p
ON p.product_id = o.product_id
AND o.order_ts >= p.valid_from
AND (o.order_ts < p.valid_to OR p.valid_to IS NULL);The LEFT JOIN keeps order 4, which has no price yet; an inner join would drop it silently.
The same idea answers "last touch before conversion" (the latest ad click before each purchase): range-join clicks
with click_ts <= purchase_ts, then keep the latest per purchase with row_number().
When to use it
| question | joins on |
|---|---|
| price, rate, tax, plan at the time of an event | an interval table (SCD 2, or intervals built with lead) |
| last touch before a conversion | events before the target, latest kept |
| ML training features | only data available at the label's time (point-in-time correctness) |
| sessions, shifts, campaigns | event time between start and end |
Best practices
- Store intervals, not just change rows, when many queries need them; computing
lead()in every query is slow and easy to get subtly wrong. - Half-open intervals everywhere (
>=start,<end), so an instant belongs to exactly one interval. - Check the intervals don't overlap per key:
lead(valid_from) >= valid_tomust hold, or events match twice. - For ML, compute features as of the label's timestamp, and test it: a feature that predicts suspiciously well usually leaks the future.
- Range joins are expensive: join on the equal key first (product, customer) so each range comparison only runs within a key.
Common mistakes
BETWEENon both ends. An order exactly at 04-05 00:00 matches the 10 and the 12. Half-open intervals.- Joining on the current value. The most common reporting bug with history: last year's orders get this year's prices.
- Inner join to the intervals. Events before the first interval disappear from the result, and from every total built on it.
- Leakage in features. "Number of support tickets" computed over the whole history, for a churn label in March, includes April's tickets. The model looks great and fails in production.
What an interviewer asks next
- "Convert payments to EUR with the rate of the payment day." As-of join on the rate table, with a fallback for weekends (last known rate, so the interval spans the weekend).
- "How do you build a training set without leakage?" For each label, join features with
feature_ts <= label_ts, and keep the latest per feature.
Go further
DuckDB's AS OF join page explains the idea with clear pictures (Trino has no ASOF keyword; the range join above
is the portable version). Feast's page shows why point-in-time joins matter for ML.
Practise it
Point-in-Time Join with a Corrected Price History, then Ad Conversions: Last Click Within 7 Days, Else Last View Within 1, then Leak-Free Training Features for a Churn Model (hard).