shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Range joins and as-of joins

Advanced · 4 min read

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():

pricevalid_fromvalid_to
1004-0104-05
1204-0504-09
1104-09NULL (still valid)

Each order picks the interval its time falls in:

order_idorder_tsprice
403-30 08:00NULL (before the first price)
104-03 12:0010
204-05 00:0012 (the new price starts that instant)
304-10 08:0011

Joining on today's price would give every order 11.

Price intervals on a timeline, each order dropping into the interval valid at its time

In SQL

Change rows to intervals, then a range join. valid_from is inclusive, valid_to exclusive:

SQL
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

questionjoins on
price, rate, tax, plan at the time of an eventan interval table (SCD 2, or intervals built with lead)
last touch before a conversionevents before the target, latest kept
ML training featuresonly data available at the label's time (point-in-time correctness)
sessions, shifts, campaignsevent 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_to must 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

  • BETWEEN on 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).