What is an as-of or nearest-match join, and how would you implement it in SQL for aligning time-series data?

Last updated 13 July 2026

  • An as-of join matches each row to the single closest row in another table by time (usually the latest at or before its timestamp), e.g. aligning trades to quotes.

  • Some engines support it natively (DuckDB ASOF JOIN, ClickHouse ASOF LEFT JOIN). Portably, use a LATERAL subquery with ts <= t.ts ORDER BY ts DESC LIMIT 1 or keep ROW_NUMBER() rank 1.

  • Match an equality key (e.g. symbol) first so the time search stays within one entity for correctness and speed.

  • https://www.snowflake.com/en/blog/time-series-analytics-asof-join-generally-available/
  • https://duckdb.org/docs/current/guides/sql_features/asof_join
  • https://cedardb.com/docs/references/advanced/asof_join/
All Advanced SQL interview questions