σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
SQL skill›Module 2 · Prices over time›Topic 2.3·~50 minIntermediate

As-of and range joins

Skill positionModule 2 of 3 · Topic 3 of 4
2.3 As-of and range joins
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01The idea in plain words
  2. 02Two tables with their own calendars
  3. 03Range joins and overlapping windows
  4. 04What the tasks ask

A back-test must only use what was known on each day. Apple reports in late July, so on 20 July the model sees the April report, not the July one. Prices arrive daily, reports arrive when they arrive, and the two never share a date: you cannot match them with =.

01

The idea in plain words

Where we are. The earlier joins matched equal keys. Here the key is time, and the match is "the latest one not after" or "anything inside this window". The next topic uses the date to build buckets of time, such as weekly and monthly bars.

DefinitionAs-of join

Pairs each row with the latest row of another table whose time is not later than its own. It looks backwards only, so a result can never use information from the future.

02

Two tables with their own calendars

main.pyPython
# The extra tables: one macro row a month, one filing row per quarterly reportprint(con.sql("SELECT date, cpi, unemployment FROM macro WHERE date >= '2026-06-01' ORDER BY date"))print(con.sql("SELECT symbol, filed_at, period_end, net_income FROM filings WHERE symbol = 'AAPL' ORDER BY filed_at DESC LIMIT 3"))
┌────────────┬─────────┬──────────────┐
│    date    │   cpi   │ unemployment │
│    date    │ double  │    double    │
├────────────┼─────────┼──────────────┤
│ 2026-06-01 │ 332.568 │          4.2 │
│ 2026-07-01 │ 332.813 │          4.1 │
│ 2026-08-01 │ 334.131 │          4.1 │
│ 2026-09-01 │ 336.298 │          4.2 │
└────────────┴─────────┴──────────────┘

┌─────────┬────────────┬────────────┬─────────────┐
│ symbol  │  filed_at  │ period_end │ net_income  │
│ varchar │    date    │    date    │    int64    │
├─────────┼────────────┼────────────┼─────────────┤
│ AAPL    │ 2026-07-31 │ 2026-06-27 │ 29789000000 │
│ AAPL    │ 2026-05-01 │ 2026-03-28 │ 29578000000 │
│ AAPL    │ 2026-01-30 │ 2025-12-27 │ 42097000000 │
└─────────┴────────────┴────────────┴─────────────┘

Joining the bars of KO to macro on b.date = m.date finds a match only on the first of a month, a day with a bar roughly one time in twenty. An as-of join gives every day the latest row dated on or before it.

main.pyPython
# Equality matches the 1st of the month only; ASOF matches every dayprint(run(con, "SELECT count(*) FROM bars b JOIN macro m ON b.date = m.date WHERE b.symbol = 'KO' AND b.date >= '2026-05-01'"))print(run(con, "SELECT count(*) FROM bars b ASOF JOIN macro m ON b.date >= m.date WHERE b.symbol = 'KO' AND b.date >= '2026-05-01'"))print(con.sql("""    SELECT b.date, b.close, m.date AS macro_date, m.cpi1    FROM bars b ASOF JOIN macro m ON b.date >= m.date    WHERE b.symbol = 'KO' AND b.date >= '2026-08-19'    ORDER BY b.date"""))
[(3,)]
[(78,)]
┌────────────┬────────┬────────────┬─────────┐
│    date    │ close  │ macro_date │   cpi   │
│    date    │ double │    date    │ double  │
├────────────┼────────┼────────────┼─────────┤
│ 2026-08-19 │  90.35 │ 2026-08-01 │ 334.131 │
│ 2026-08-20 │   90.5 │ 2026-08-01 │ 334.131 │
│ 2026-08-21 │   91.1 │ 2026-08-01 │ 334.131 │
└────────────┴────────┴────────────┴─────────┘
  1. 1b.date >= m.date is the as-of condition: the macro row is the latest one at or before the bar.

Put the equality part of the key first and the time comparison last: ASOF LEFT JOIN filings f ON f.symbol = b.symbol AND b.date >= f.filed_at matches within each symbol, and LEFT keeps the bars from before the first report, with NULL income, instead of dropping them.

main.pyPython
# AAPL's first report was filed on 2009-07-22: before that day the income is NULLprint(con.sql("""    SELECT b.date, b.close, f.filed_at, f.net_income    FROM bars b ASOF LEFT JOIN filings f ON f.symbol = b.symbol AND b.date >= f.filed_at    WHERE b.symbol = 'AAPL' AND b.date BETWEEN '2009-07-20' AND '2009-07-23'    ORDER BY b.date"""))
┌────────────┬─────────┬────────────┬────────────┐
│    date    │  close  │  filed_at  │ net_income │
│    date    │ double  │    date    │   int64    │
├────────────┼─────────┼────────────┼────────────┤
│ 2009-07-20 │ 4.58231 │ NULL       │       NULL │
│ 2009-07-21 │ 4.53886 │ NULL       │       NULL │
│ 2009-07-22 │ 4.69632 │ 2009-07-22 │  164000000 │
│ 2009-07-23 │ 4.72901 │ 2009-07-22 │  164000000 │
└────────────┴─────────┴────────────┴────────────┘
03

Range joins and overlapping windows

A range join matches a row to every row inside a window, for instance the bars from a filing date to three days after it. Group by the filing to total the volume. The danger is overlap: a bar that sits in two windows is paired twice, so either de-duplicate with DISTINCT or ask with EXISTS.

main.pyPython
# XOM bars within 120 days after a filing: the windows overlap, so rows repeatprint(run(con, """    SELECT count(*) AS joined_rows, count(DISTINCT b.date) AS distinct_days1    FROM bars b JOIN filings f ON f.symbol = b.symbol AND b.date BETWEEN f.filed_at AND f.filed_at + INTERVAL 120 DAY    WHERE b.symbol = 'XOM'"""))print(run(con, """    SELECT f.filed_at, count(*) AS bars, sum(b.volume) AS total    FROM filings f JOIN bars b ON b.symbol = f.symbol AND b.date BETWEEN f.filed_at AND f.filed_at + INTERVAL 3 DAY    WHERE f.symbol = 'JPM'    GROUP BY f.filed_at ORDER BY f.filed_at DESC LIMIT 2"""))
[(4268, 3581)]
[(datetime.date(2026, 8, 6), 2, 10386317), (datetime.date(2026, 5, 1), 2, 13473538)]
  1. 1More joined rows than distinct days: some bars were counted twice.
The right timestamp is the one you could have known

The filings table is joined on filed_at, the day the report became public, not on period_end, the day the quarter ended. Using the quarter end would hand the model a result weeks before anyone could have seen it. Pick the date a fact became available, not the date it describes.

04

What the tasks ask

  1. 1
    `bars_with_macro` and `known_income`As-of joins; the second needs an equality on symbol and LEFT to keep early bars.
  2. 2
    `filing_window_volume`A range join with BETWEEN, then GROUP BY the filing date.
  3. 3
    `earnings_windows`A window on both sides of the filing, with each bar returned once.

Key takeaways

  • As-of means the latest one not after ASOF JOIN ... ON b.date >= m.date never looks ahead, and LEFT keeps the rows with no earlier match.
  • Windows overlap A range join can pair one row with several windows; de-duplicate or use EXISTS.
  • Join on availability, not on the period A fact is usable from the day it was published. Next: time buckets.
Quick check
A bar dated 20 July is joined to a company's reports. The April report was filed on 28 April for the quarter ended 31 March, and the July report on 30 July for the quarter ended 30 June. Which report should an as-of join attach to the 20 July bar?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • ASOF JOIN: bars_with_macro25 pts
  • No look-ahead: known_income25 pts
  • A range join: filing_window_volume25 pts
  • Overlapping windows: earnings_windows25 pts
Explorer · lab-7-1
▾ lab-7-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-7-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-7-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes