Prices live in one table and sectors in another. To total volume by sector, or to line KO up against JNJ day by day, the tables must be joined. A join is the most useful SQL feature and the easiest one to get silently wrong, because a bad join returns plausible numbers.
The idea in plain words
Where we are. Topics 1 to 3 worked on one table. Here a query reads two. The next topic, window functions, brings back the idea of "the previous day" without joining the table to itself.
a JOIN b ON a.key = b.key pairs every row of a with every row of b that has the same key, and keeps only the pairs. A row with no partner disappears. A LEFT JOIN keeps every row of the left table anyway and fills the right-hand columns with NULL where nothing matched.
The column (or columns) the ON clause compares. If a key value repeats in a table, each repeat pairs again and the output gains rows.
Inner and left joins
# The second table: one row per symbol, including one with no barsprint(con.sql("SELECT * FROM sectors ORDER BY symbol"))
┌─────────┬────────────────────────┐ │ symbol │ sector │ │ varchar │ varchar │ ├─────────┼────────────────────────┤ │ AAPL │ Technology │ │ JNJ │ Health Care │ │ JPM │ Financials │ │ KO │ Consumer Staples │ │ TSLA │ Consumer Discretionary │ │ XOM │ Energy │ └─────────┴────────────────────────┘
# JOIN pairs rows that agree on symbol; the sector arrives from the other tableprint(con.sql(""" SELECT s.sector, b.symbol, b.date, b.close FROM bars b JOIN sectors s ON s.symbol = b.symbol1 WHERE b.date = '2026-08-21' ORDER BY s.sector"""))
┌──────────────────┬─────────┬────────────┬────────┐ │ sector │ symbol │ date │ close │ │ varchar │ varchar │ date │ double │ ├──────────────────┼─────────┼────────────┼────────┤ │ Consumer Staples │ KO │ 2026-08-21 │ 91.1 │ │ Energy │ XOM │ 2026-08-21 │ 165.11 │ │ Financials │ JPM │ 2026-08-21 │ 351.58 │ │ Health Care │ JNJ │ 2026-08-21 │ 270.24 │ │ Technology │ AAPL │ 2026-08-21 │ 309.35 │ └──────────────────┴─────────┴────────────┴────────┘
- 1
bandsare aliases, short names for the two tables.
# LEFT JOIN keeps TSLA even though no bar matches; count(b.symbol) sees the NULLsprint(con.sql(""" SELECT s.symbol, s.sector, count(b.symbol) AS bars1 FROM sectors s LEFT JOIN bars b ON b.symbol = s.symbol GROUP BY s.symbol, s.sector ORDER BY bars"""))
┌─────────┬────────────────────────┬───────┐ │ symbol │ sector │ bars │ │ varchar │ varchar │ int64 │ ├─────────┼────────────────────────┼───────┤ │ TSLA │ Consumer Discretionary │ 0 │ │ JPM │ Financials │ 6698 │ │ XOM │ Energy │ 6698 │ │ AAPL │ Technology │ 6699 │ │ KO │ Consumer Staples │ 6699 │ │ JNJ │ Health Care │ 6699 │ └─────────┴────────────────────────┴───────┘
- 1
count(b.symbol)skips NULL, so TSLA counts 0;count(*)would say 1.
To list only the unmatched rows, a LEFT JOIN followed by WHERE b.symbol IS NULL keeps exactly the rows of sectors that found no partner. This pattern is called an anti-join, and it is how you answer "which symbols have no data?".
One trap: a test about the right-hand table, such as b.date >= '2026-08-21', belongs in the ON clause of a LEFT JOIN. Put it in WHERE and every unmatched row, whose date is NULL, fails the test and disappears, so the left join quietly turns into an inner join. The lab task symbols_without_bars(since) is built to catch this.
Joins that multiply rows
# A duplicated key doubles every KO bar, and every sum over themcon.execute("CREATE TABLE dup AS SELECT * FROM sectors UNION ALL SELECT 'KO', 'Beverages'")print(run(con, "SELECT count(*) FROM bars b JOIN sectors s ON s.symbol = b.symbol WHERE b.symbol = 'KO'"))print(run(con, "SELECT count(*) FROM bars b JOIN dup s ON s.symbol = b.symbol WHERE b.symbol = 'KO'"))
[(6699,)] [(13398,)]
The second count is twice the first, and a sum(volume) would be doubled with no error message. After a join, check that the row count is what you expect, and make sure the key is unique on the side you did not mean to multiply.
A table joined to itself
To put two symbols side by side, use bars twice under different aliases and join on the date. Only the days where both have a bar survive, which is usually exactly what a correlation or a spread needs.
# KO against JNJ on the days both tradedprint(con.sql(""" SELECT a.date, a.close AS ko, b.close AS jnj FROM bars a JOIN bars b ON b.date = a.date WHERE a.symbol = 'KO' AND b.symbol = 'JNJ' AND a.date >= '2026-08-18' ORDER BY a.date"""))
┌────────────┬────────┬────────┐ │ date │ ko │ jnj │ │ date │ double │ double │ ├────────────┼────────┼────────┤ │ 2026-08-18 │ 88.82 │ 271.11 │ │ 2026-08-19 │ 90.35 │ 273.41 │ │ 2026-08-20 │ 90.5 │ 267.37 │ │ 2026-08-21 │ 91.1 │ 270.24 │ └────────────┴────────┴────────┘
What the tasks ask
- 1`bars_in_sector` and `sector_volume`An inner join, then (for the second) a
GROUP BYon the sector. A sector with no bars must not appear. - 2`symbols_without_bars`A
LEFT JOINwhoseONholds the date test, and anIS NULLon the right side. - 3`pair_closes`Join
barsto itself on the date, one alias per symbol.
sectors has 6 symbols and 5 of them have bars. How many rows does SELECT * FROM sectors s LEFT JOIN bars b ON b.symbol = s.symbol WHERE b.symbol IS NULL return?Lab · what's graded
4 tasks · 100 pts- Inner
JOIN:bars_in_sector25 pts LEFT JOINanti-join:symbols_without_bars25 pts- Join, then group:
sector_volume25 pts - A self-join:
pair_closes25 pts