Investigations with DuckDB 2.0 and public data
The DuckDB Casebook
Investigations of markets, consumer behavior and public data, showing how to turn imperfect records into defensible answers. Each chapter defines what a row means, makes the comparison that answers its question, separates a change in the world from a change in the records, and tests whether the answer survives.
First draft December 2026. Numbers may still change.
Contents
Before you start
Why one embedded SQL engine is enough for this book, then one small, checked result from a public file and the source tests every later chapter relies on.
- Why DuckDB rocksIn this book one SQL engine with no server counted 4.4 billion taxi rows from file footers, read 85 GB of Wikipedia clickstream on a 16 GB machine and rebuilt a state's liquor ledger on a laptop. This page says what makes that possible, what DuckDB 2.0 added, and where the engine fell short.
- 0Setup and a first checked resultOne small question answered end to end, from a list of public files to a count you can check and rerun; then the installation, extensions and source tests the rebuilding chapters rely on.
Part I — Markets and attention over time
Three long administrative records: taxi trips, product reviews and Wikipedia clicks. Each chapter separates a change in the world from a change in what got recorded.
- 1How New York's ride market changedSeventeen years of TLC trip files show yellow cabs peaking in 2012, app rides passing cabs in 2017, and the cab trips that remain moving out of the small hours and getting longer. Which of those changes are in the world, and which are in the records, is part of the question.
- 2How Amazon's rating distribution changedWithin the same nine categories, weighted the same way every year, the share of 5-star Amazon reviews rose from 2010 to a 2019 peak and then fell in every one, mostly toward 1 star. Ratings did not inflate steadily; they moved to the extremes. Whether product turnover drives this is still open.
- 3Is search still Wikipedia's front door?In seven Wikipedias, arrivals from search engines fell more than arrivals overall between the years to October 2024 and October 2026; in English, by about a fifth. The decline survives checks against Wikimedia's own pageview counts. What caused it, AI summaries included, the clickstream cannot say.
Part II — Products, companies and places
Product names, employer strings and map points become things you can compare only after someone defines them. These chapters make that definition visible and test how much the answer depends on it.
- 4What packaged-food labels let us compareIn 17 of 25 food groups, most US products in Open Food Facts that carry a processing class are ultra-processed. Fewer than half the products have a class and there are no sales weights, so these compare labels, not diets.
- 5Finding comparable store-brand productsA quarter of national-brand products in Open Food Facts have a store-brand product with a near-identical name and ingredient list, and four in five of those pairs pass a hand check. Their nutrition labels still differ more often than not, in both directions, except for simple products such as water and milk.
- 6Political giving by employees of consumer brandsAmong employees who give itemized donations, those at tech and media brands give almost only to Democrats, and only five of 49 companies had a Republican-leaning majority in 2024. The measure describes donors, not workforces, and rests on a reviewed table matching employer strings to companies.
- 7Where the dollar store is closer than the supermarketFor 56% of populated tracts in the lower 48 the nearest dollar store is closer than the nearest listed supermarket chain (35% counting every grocery place), and the share in the poorest fifth of tracts is close to three times that in the richest.
Part III — Choices and policy
Back to the taxi records with harder questions: what a payment screen offered riders, and what a toll changed. The step from a pattern to a mechanism, and from a mechanism to an effect.
- 8What tip payments reveal about the payment screenCard tips in New York's yellow cabs still pile up, to the cent, on a few whole percentages: the likely buttons on the back-seat screen. Read back from 2009 to 2026, the inferred buttons reproduce a published study and shift when TLC adds a fee.
- 9What taxi records can establish about congestion pricingAgainst the Upper East and West Side, trips inside Manhattan's relief zone got about 7% faster in the toll's first weeks and the gain faded within a year; app fares per mile rose. The decline in Uber and Lyft pickups matches a decline a year before the toll, so this design cannot attribute it to the fee.
Part IV — Business workflows
The kind of work an analyst inherits: a wholesaler's invoices, a shop's order book, a stack of annual reports, a product catalog enriched by a model. Each ends with tables someone can run again next month.
- 10A state wholesaler's books, rebuilt from its invoicesIowa is the only wholesaler of spirits in the state, and it publishes every invoice line since 2012. Loading the files naively overstates recent revenue by a fifth; loaded idempotently, they match the state's annual report, and they show sales growing on mix and price while volume fell after 2021.
- 11Cohort retention for an online shop, from one Excel workbookA UK online gift wholesaler's two years of invoices, read straight from Excel: about one new customer in five orders again the next month, returning customers bring in 80% to 91% of revenue, and in 2011 the shop held revenue flat on half as many new customers.
- 12Peer benchmarks from every 10-KBuilt from the SEC's XBRL extracts of every 10-K, an industry benchmark for fiscal 2025 shows the typical semiconductor company earning 2 cents of operating profit per dollar of revenue while the industry as a whole earned 35. One company in seven later changed a prior-year figure.
- 13A catalog that knows what needs reviewDecision models tag jewelry images with a probability for every option. Stored as rows in DuckDB, those probabilities pick out answers worth a second look, but a threshold tuned on half of 28 labelled images missed its error target on the other half. The output is a review queue, not an accuracy claim.
Part V — When the data changes
Every earlier answer depends on a source release. The last chapter reruns a question on the next release and recovers the old answer.
By technique
Another way in: the chapter that best shows each task.
- 0Go from a public file to a checked, rerunnable result
- 1Reconcile files whose schemas changed over the years
- 2Reduce a source too big for memory to reusable counts
- 3Resume a scan of hundreds of files and check its coverage
- 4Flatten nested attributes without multiplying rows
- 5Retrieve, re-rank and validate candidate matches
- 6Keep an auditable dictionary of messy names
- 7Find the nearest facility and test the definition
- 8Infer an unrecorded menu from where outcomes bunch
- 9Build a panel for an event study, with placebos
- 10Load snapshot files idempotently and tie out to a report
- 11Read Excel in place and build cohort tables
- 12Pivot tagged financial data and track revisions
- 13Cache, evaluate and review model outputs
- 14Compare releases and recover an earlier answer
Map Lab
- Map LabThree interactive maps built from Chapter 9's and Chapter 7's pipelines: store access by census tract, a store-count radius tool, and the taxi-zone change around New York's congestion toll by hour.
- Store access by census tractSix metro areas, each tract coloured by how far its nearest dollar store is compared with its nearest supermarket, with every store plotted, chain toggles, an income filter and a click-through panel.
- Stores within a radiusClick anywhere in six metro areas to count dollar stores and supermarkets within 1 to 10 miles, from precomputed H3 hexagon counts, checked against the store points.
- Congestion zone change by hourPercent change in taxi and app pickups and dropoffs by New York taxi zone, the year after the January 2025 congestion toll against the year before, one hour of the day at a time.
- How these maps were builtThe three spatial queries behind the Map Lab, with their results: simplifying tract outlines, counting stores per H3 hexagon, and an exact radius query to check the hexagon shortcut.
Gallery
- When New York rides: 2009 vs 2026Pickups by hour and weekday in April 2009 and April 2026, read in place from TLC's parquet files and shaped with PIVOT.
- Rain and ridesNew York pickups in wet and dry hours, March to June 2026, matched to Central Park's hourly weather report with an ASOF JOIN.
- Chain footprints on a hex gridWhere the 12 US storefront chains with the most places in Overture are, counted into H3 cells straight from Overture's S3 parquet.
- GitHub's event mix, and the day it stopped being comparableEvent types in 58 sampled GH Archive hours from 2015 to 2026, read with read_json over a URL list and probed with VARIANT across a payload change.
- The family tree of OLMo 2 13BEvery Hugging Face model that declares OLMo 2 13B as an ancestor, fetched generation by generation and walked with a recursive CTE.
- Ride-hail overtakes cabsMonthly New York trips by yellow and green cabs and by the four app companies, 2009 to 2026: the apps passed the cabs in February 2017.
- Tip bunching by yearThe share of New York yellow-cab card-coded trips whose tip lands exactly on a percentage where that vendor's tips bunch (the screens' likely defaults), by year and payment vendor, 2009 to 2026, from Chapter 8's read of every TLC trip file.
- One dataset, two layouts120 million yellow-cab trips written as one file, as Hive year/month partitions, and with DuckDB 2.0's PARTITION_PATH and ORDER_BY, then measured by how much of each a query has to read.
- Nutri-Score by food groupHow many US packaged products in each food group Open Food Facts grades D or E on Nutri-Score, from Chapter 4's read of the Open Food Facts product file.