σμ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 1 · Querying tables›Topic 1.3·~40 minBeginner

NULL values and dates

Skill positionModule 1 of 3 · Topic 3 of 4
1.3 NULL values and dates
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01The idea in plain words
  2. 02Finding and filling gaps
  3. 03Dates are numbers you can add to
  4. 04What the tasks ask

A feed drops one close out of five. Is the average of the week the mean of the four numbers left, or is it unknown? SQL has a value for "unknown", NULL, and it behaves differently from every other value: it is not equal to anything, not even to itself.

01

The idea in plain words

Where we are. Topic 1 selected rows and topic 2 grouped them. Both quietly assumed every value was present. Real feeds have gaps, and a query has to say what to do about them. The next topic joins two tables, where missing matches produce NULLs too.

DefinitionThree-valued logic

A test can be true, false or unknown. Any comparison with NULL is unknown, and WHERE keeps a row only when the test is true, so unknown rows disappear along with false ones.

02

Finding and filling gaps

Real KO bars have no gaps, so make a tiny copy with two of them: a missing close on 19 August and a missing volume on 20 August. The tables printed below show a gap as NULL.

main.pyPython
# A small copy of KO with one close and one volume missingcon.execute("CREATE TABLE gaps AS SELECT symbol, date, open, close, volume FROM bars WHERE symbol = 'KO' AND date >= '2026-08-17'")con.execute("UPDATE gaps SET close = NULL WHERE date = '2026-08-19'")con.execute("UPDATE gaps SET volume = NULL WHERE date = '2026-08-20'")print(con.sql("SELECT date, open, close, volume FROM gaps ORDER BY date"))
┌────────────┬────────┬────────┬──────────┐
│    date    │  open  │ close  │  volume  │
│    date    │ double │ double │  int64   │
├────────────┼────────┼────────┼──────────┤
│ 2026-08-17 │   87.3 │  86.98 │ 12056407 │
│ 2026-08-18 │   88.4 │  88.82 │ 12524517 │
│ 2026-08-19 │  88.59 │   NULL │ 15993861 │
│ 2026-08-20 │  90.18 │   90.5 │     NULL │
│ 2026-08-21 │  90.85 │   91.1 │ 15534719 │
└────────────┴────────┴────────┴──────────┘
main.pyPython
# = NULL is never true; IS NULL finds the gap; aggregates skip itprint(run(con, "SELECT date FROM gaps WHERE close = NULL"))1print(run(con, "SELECT date FROM gaps WHERE close IS NULL"))print(run(con, "SELECT count(*), count(close), round(avg(close), 2) FROM gaps"))2
[]
[(datetime.date(2026, 8, 19),)]
[(5, 4, 89.35)]
  1. 1Unknown is not true, so nothing comes back.
  2. 25 rows, 4 closes: avg averages the four it can see.

coalesce(x, fallback) returns the first value that is not NULL, which fills a gap. nullif(a, b) does the opposite, returning NULL when a equals b; dividing by nullif(open, 0) turns an infinite ratio into an honest NULL. Arithmetic with a NULL gives NULL, so a move is unknown when either price is.

main.pyPython
# coalesce fills the volume; nullif guards the division; NULL in, NULL outprint(con.sql("""    SELECT date, volume, coalesce(volume, 0) AS filled,           round((close - open) / nullif(open, 0), 4) AS move    FROM gaps    ORDER BY date"""))
┌────────────┬──────────┬──────────┬─────────┐
│    date    │  volume  │  filled  │  move   │
│    date    │  int64   │  int64   │ double  │
├────────────┼──────────┼──────────┼─────────┤
│ 2026-08-17 │ 12056407 │ 12056407 │ -0.0037 │
│ 2026-08-18 │ 12524517 │ 12524517 │  0.0048 │
│ 2026-08-19 │ 15993861 │ 15993861 │    NULL │
│ 2026-08-20 │     NULL │        0 │  0.0035 │
│ 2026-08-21 │ 15534719 │ 15534719 │  0.0028 │
└────────────┴──────────┴──────────┴─────────┘
03

Dates are numbers you can add to

A DATE can be shifted with INTERVAL, compared with other dates, and taken apart with functions like year, month, dayname. "The last 5 calendar days up to 21 August" means after 16 August and up to 21 August: subtract the interval from the end date and use a strict > on the left, or you get six days.

main.pyPython
# INTERVAL shifts a date; dayname and date_diff take it apartprint(con.sql("""    SELECT date, dayname(date) AS weekday, date + INTERVAL 7 DAY AS next_week,           date_diff('day', date, DATE '2026-08-31') AS days_to_month_end    FROM gaps    ORDER BY date    LIMIT 3"""))
┌────────────┬───────────┬─────────────────────┬───────────────────┐
│    date    │  weekday  │      next_week      │ days_to_month_end │
│    date    │  varchar  │      timestamp      │       int64       │
├────────────┼───────────┼─────────────────────┼───────────────────┤
│ 2026-08-17 │ Monday    │ 2026-08-24 00:00:00 │                14 │
│ 2026-08-18 │ Tuesday   │ 2026-08-25 00:00:00 │                13 │
│ 2026-08-19 │ Wednesday │ 2026-08-26 00:00:00 │                12 │
└────────────┴───────────┴─────────────────────┴───────────────────┘
NOT IN and NULL

x NOT IN (1, NULL) is never true, because "x differs from the unknown" is itself unknown. A subquery that can return NULL turns NOT IN into an empty result. Use NOT EXISTS, or filter the NULLs out, when the list comes from data.

04

What the tasks ask

  1. 1
    `missing_close_days`IS NULL, never = NULL.
  2. 2
    `volume_filled` and `open_to_close`coalesce to fill; nullif so a zero open gives NULL instead of infinity.
  3. 3
    `trailing_days`INTERVAL arithmetic with the right edge on each side.

Key takeaways

  • NULL is unknown Comparisons with NULL are never true; test with IS NULL and IS NOT NULL.
  • Choose what a gap means Aggregates skip NULLs, arithmetic spreads them, coalesce and nullif let you decide.
  • Mind both ends of a date window A window of N days has one end included and the other excluded. Next: joining tables.
Quick check
A table has 10 rows and 2 of them have a NULL close. What does SELECT count(close) FROM t return?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • IS NULL: missing_close_days25 pts
  • coalesce: volume_filled25 pts
  • nullif and NULL arithmetic: open_to_close25 pts
  • Date arithmetic: trailing_days25 pts
Explorer · lab-3-1
▾ lab-3-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-3-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-3-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