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.
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.
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.
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.
# 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 │ └────────────┴────────┴────────┴──────────┘
# = 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)]
- 1Unknown is not true, so nothing comes back.
- 25 rows, 4 closes:
avgaverages 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.
# 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 │ └────────────┴──────────┴──────────┴─────────┘
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.
# 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 │ └────────────┴───────────┴─────────────────────┴───────────────────┘
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.
What the tasks ask
- 1`missing_close_days`
IS NULL, never= NULL. - 2`volume_filled` and `open_to_close`
coalesceto fill;nullifso a zero open gives NULL instead of infinity. - 3`trailing_days`
INTERVALarithmetic with the right edge on each side.
SELECT count(close) FROM t return?Lab · what's graded
4 tasks · 100 ptsIS NULL:missing_close_days25 ptscoalesce:volume_filled25 ptsnullifand NULL arithmetic:open_to_close25 pts- Date arithmetic:
trailing_days25 pts