"The three busiest days of every stock" takes a subquery in plain SQL, a second subquery to filter it, and a lot of repeated column names. In DuckDB it is one query with one extra clause. DuckDB's SQL keeps everything standard SQL has and adds shortcuts that research queries use all the time.
The idea in plain words
Where we are. Topic 3 joined two tables on time. This one makes single-table analysis shorter: rank within a group, spread a column wide, summarise by month, apply one formula to many columns. The next topic hands results to pandas, Polars and Arrow.
DuckDB's extensions to standard SQL that remove boilerplate without changing what a query means: QUALIFY, PIVOT, GROUP BY ALL, FILTER, COLUMNS, SELECT * EXCLUDE, FROM first, and more. Most other databases do not have them, so a query that relies on them is DuckDB-specific.
Rank inside a group with QUALIFY
QUALIFY is to window functions what HAVING is to aggregates: it filters on their result, so the window and the filter sit in one query with no subquery. Put a tie-breaker in the window's ORDER BY so the answer does not change from run to run.
# The busiest day of every stock, with no subqueryprint(con.sql(""" SELECT symbol, date, volume FROM bars QUALIFY row_number() OVER (PARTITION BY symbol ORDER BY volume DESC, date) = 1 ORDER BY symbol"""))
┌─────────┬────────────┬────────────┐ │ symbol │ date │ volume │ │ varchar │ date │ int64 │ ├─────────┼────────────┼────────────┤ │ AAPL │ 2000-09-29 │ 8846193721 │ │ JNJ │ 2023-08-21 │ 151319484 │ │ JPM │ 2012-05-11 │ 291205984 │ │ KO │ 2009-09-18 │ 179920213 │ │ XOM │ 2010-06-25 │ 189442014 │ └─────────┴────────────┴────────────┘
Wide tables, grouped summaries, one formula for many columns
PIVOT spreads the values of a column into columns of their own, a closing-price table with one column per stock. GROUP BY ALL saves repeating the grouping columns, FILTER counts only some rows inside an aggregate, arg_max(a, b) returns a at the row where b is largest, and COLUMNS('regex') applies one expression to every column whose name matches.
# One column per stocknames, rows = closes_pivot(con, "2026-08-19")print(names)for row in rows: print(row)
['date', 'AAPL', 'JNJ', 'JPM', 'KO', 'XOM'] (datetime.date(2026, 8, 19), 316.83, 273.41, 357.26, 90.35, 164.77) (datetime.date(2026, 8, 20), 311.3, 267.37, 351.55, 90.5, 166.15) (datetime.date(2026, 8, 21), 309.35, 270.24, 351.58, 91.1, 165.11)
# Per month: up days, the date of the highest close, and the day countfor row in month_summary(con, "KO")[-3:]: print(row)
(datetime.date(2026, 6, 1), 9, datetime.date(2026, 6, 10), 21) (datetime.date(2026, 7, 1), 10, datetime.date(2026, 7, 29), 22) (datetime.date(2026, 8, 1), 9, datetime.date(2026, 8, 21), 15)
# One average, written once, applied to four columns; and a few other shortcutsprint({name: round(value, 2) for name, value in price_means(con).items()})print(con.sql("FROM bars SELECT * EXCLUDE (open, high, low) WHERE symbol = 'KO' ORDER BY date DESC LIMIT 2"))1
{'open': 61.84, 'high': 62.41, 'low': 61.28, 'close': 61.86}
┌─────────┬────────────┬────────┬──────────┐
│ symbol │ date │ close │ volume │
│ varchar │ date │ double │ int64 │
├─────────┼────────────┼────────┼──────────┤
│ KO │ 2026-08-21 │ 91.1 │ 15534719 │
│ KO │ 2026-08-20 │ 90.5 │ 14928006 │
└─────────┴────────────┴────────┴──────────┘- 1
FROMcan come first, andEXCLUDEdrops columns without listing the rest.
A query written with QUALIFY, PIVOT or COLUMNS will not run on PostgreSQL or most other engines, and a pivot's column list is decided by the data. If a query must travel, or its columns must be fixed, use the standard forms (a subquery for QUALIFY, conditional aggregates for PIVOT).
What the tasks ask
- 1`busiest_days`
row_number()over each symbol withQUALIFY ... <= n. - 2`closes_pivot` and `month_summary`
PIVOT ... ON symbol; thenFILTER,arg_maxandGROUP BY ALLby month. - 3`price_means`A single
avg(COLUMNS('regex'))for four columns, returned as a dict.
row_number() OVER (PARTITION BY symbol ORDER BY volume DESC)?Lab · what's graded
4 tasks · 100 ptsQUALIFY:busiest_days25 ptsPIVOT:closes_pivot25 ptsFILTER,arg_max,GROUP BY ALL:month_summary25 ptsCOLUMNS:price_means25 pts