
Chris Parmer
2026-11-16
DuckDB demo
Fast SQL on files and dataframes in Python, with Dash and Plotly
TL;DR
- DuckDB is an analytics database that runs inside your Python process, with no server to set up. It runs SQL over Parquet, CSV, and JSON files and over dataframes, using every core. Version 2.0, a long-term support release, came out on October 21, 2026.
- It suits Dash apps that run SQL over large files: regulatory extracts in pharma, meter and SCADA exports in energy, trade and quote files in finance, and instrument logs in science and engineering.
- With a coding agent you describe the app and name the DuckDB features you want, and the agent writes the SQL. This demo shows what's possible and gives the prompts.
- Our example is Safety Signal Desk, a drug safety app on two years of FDA adverse event reports. DuckDB loads its 32.9 million raw rows in under a minute, and every chart in the app is one SQL query that returns in under a second.

What is DuckDB, and which Dash apps is it for?
A lot of domain data arrives as files: a quarterly extract from a regulator, a folder of meter readings, a day of trades, a bucket of Parquet from the data team. Domain experts want an app on top of that data that their colleagues can filter and question, without a database team or a warehouse bill. Loading every file into memory gets slow as the files grow, and a database server is a project of its own.
DuckDB sits in between. Mark Raasveldt and Hannes MĂŒhleisen started it at CWI, the Dutch national research institute for mathematics and computer science, and first released it in 2019. You pip install duckdb, point SQL at files or DataFrames, and it runs the query on all your cores, streaming the data instead of loading it all. In a Dash app, each callback can run one query and hand the result to Plotly.
We also recommend Polars for loading data in new apps. Our overview of Python packages for data shows where both fit with the rest of the stack.
These demos show you what's possible with DuckDB, and a coding agent writes the SQL.
What's possible
These are the DuckDB features to name in your prompts:
- Read files where they are. read_csv, read_parquet, and read_json read local files, globs like data/*.parquet, and https:// or s3:// URLs. From Parquet they fetch only the columns a query uses.
- Query DataFrames directly. SQL can name a pandas or Polars DataFrame as a table, and .df() and .pl() turn results back into DataFrames for Plotly.
- Persist to a database file. duckdb.connect("file.duckdb") keeps tables between runs, and COPY ... TO 'x.parquet' exports them.
- Window functions and QUALIFY, for running totals, rankings, lags, and keeping the latest row per key.
- PIVOT and UNPIVOT, for turning long tables wide and back.
- ASOF JOIN, for matching each row to the most recent row before it, such as trades to quotes.
- Prepared parameters ($name), so user input never ends up inside the SQL string.
- Extensions: spatial for geometry, fts for full-text search, httpfs for cloud storage, plus Iceberg and Delta tables.
Demo App: Safety Signal Desk
Drug safety teams look for signals: reactions reported more often with one drug than with all the others. The data is FAERS, the FDA Adverse Event Reporting System, which publishes every report it receives as quarterly files. Each quarter is a zip of $-delimited text tables: patients, drugs, reactions, and therapy dates, linked by a case ID.
We used eight quarters, 2024 Q4 to 2026 Q3. Safety Signal Desk has four pages: Signals, Reaction detail, Compare drugs, and a SQL page. It runs on DuckDB 2.0.0 over Parquet files, with Dash and Plotly for the interface. The same shape (load files, rank, drill down, compare, and ask anything in SQL) fits a meter fleet or a trading book just as well.
A report in FAERS means someone suspected a drug, not that the drug caused the reaction, and counts also rise with publicity, so treat everything here as a place to start looking.
Step 1: load two years of FAERS into a DuckDB file
Prompt: Download the FDA FAERS quarterly ASCII zips for 2024Q4 through 2026Q3 from fis.fda.gov. Write build_db.py with DuckDB 2.0: for each quarter, unzip DEMO, DRUG, REAC and THER and load them with read_csv (delimiter '$', no quoting, all columns as text, ignore bad rows) into raw tables in data/faers.duckdb, with a quarter column. Load the Deleted case lists. Then build clean tables: keep the latest version of each case with QUALIFY row_number(), drop deleted cases, parse dates with try_strptime, convert ages to years, and use the active ingredient (prod_ai) as the drug name. Print row counts and timings.
The heart of what the agent wrote is two statements. read_csv reads FDA's odd format without any preprocessing, and QUALIFY keeps one row per case without a subquery:
con.execute(f"""CREATE TABLE raw_demo AS SELECT '{q}' AS quarter, *FROM read_csv('{path}', delim='$', header=true, quote='', all_varchar=true, ignore_errors=true)""")con.execute("""CREATE TABLE cases AS SELECT primaryid, caseid, quarter, sex, age, occp_codFROM raw_demo WHERE caseid NOT IN (SELECT caseid FROM deleted)QUALIFY row_number() OVER (PARTITION BY caseid ORDER BY try_cast(caseversion AS INT) DESC) = 1""")
Loading all eight quarters took 46.5 seconds on two threads. A second script writes the four tables the app needs to Parquet for deployment.
Step 2: the Signals page
Prompt: Build a Dash app, Safety Signal Desk, that opens the Parquet files as DuckDB views. Signals page: pick a drug (active ingredient, with its report count) and a range of quarters. In one SQL query with CTEs, count the reports for every reaction with that drug and overall, and compute the proportional reporting ratio (PRR) and the reporting odds ratio (ROR) with its 95% confidence interval. Pass the drug as a query parameter, never by string formatting. Plot reactions as a log-log scatter of reports against PRR, purple when PRR >= 2, 3+ reports and ROR lower bound > 1. Show the strongest signals in a table, a toggle to hide medication-error terms, and the query time. Use one cursor per callback.
That's the screenshot at the top: every reaction reported with the chosen drug, ranked against all other drugs, with the ones that meet the signal criteria in purple. For semaglutide, 530 reaction terms qualify, and the query runs in a few hundred milliseconds.

For tirzepatide, the strongest signals are about the injection itself, such as injection site coldness. Medication-error terms like "incorrect dose administered" are among the most reported, so the toggle hides them by default.
Here's the query:
WITH c AS (SELECT cid FROM cases WHERE quarter BETWEEN $q0 AND $q1),s AS (SELECT cid FROM suspects SEMI JOIN c USING (cid) WHERE iid = $iid),k AS (SELECT (SELECT count(*) FROM s) AS n_drug, (SELECT count(*) FROM c) AS n_all),a AS (SELECT tid, count(*) AS a FROM reactions SEMI JOIN s USING (cid) GROUP BY tid),p AS (SELECT tid, count(*) AS n_pt FROM reactions SEMI JOIN c USING (cid) GROUP BY tid)SELECT tid, a, (a / n_drug) / ((n_pt - a) / (n_all - n_drug)) AS prrFROM a JOIN p USING (tid) CROSS JOIN k
Step 3: Reaction detail
Prompt: Add a Reaction detail page for one drug and reaction (clicking a dot on the Signals page opens it). Show four charts, each from one DuckDB query: reports per quarter with the cumulative PRR computed with SUM() OVER (ORDER BY quarter) window functions, days from the therapy start date to the event date, ages by sex against all reports for the drug, and who reported it (consumer, physician, pharmacist, or lawyer). Add URL parameters (?drug=...&pt=...&tab=...) so people can share a view.

This page answers the questions a reviewer asks next: is the signal stable over time, how soon after starting the drug does the reaction appear, who is affected, and who reported it. The median time to onset is 39 days. The reporter chart is a reminder that signals need judgment, because the mix of reporters for one reaction can differ a lot from the drug's overall mix.
Step 4: Compare drugs
Prompt: Add a Compare drugs page: pick several drugs (default: semaglutide, tirzepatide, dulaglutide, liraglutide). Run the signal query for each, keep the 25 reactions with the most reports, and use DuckDB's PIVOT on the pandas result to get one row per reaction and one column per drug. Draw a Plotly heatmap of ROR on a log scale centered on 1, with the values in the cells.

Side by side, the four drugs share the gut reactions in different amounts: impaired gastric emptying stands out far less for tirzepatide (ROR 6.8) than for the other three. The page runs four signal queries and a PIVOT.
Step 5: a SQL page for everyone else's questions
Prompt: Add a SQL page with a text box and a Run button. Accept exactly one SELECT statement (check with duckdb.extract_statements), show the first 500 rows in a DataTable and the run time, and show DuckDB's error message if it fails. Before the app serves anything, lock the connection down: allowed_directories set to the data folder, enable_external_access off, then lock_configuration on.

Questions nobody built a chart for still get answers. This one, which countries report tirzepatide and how many of the reports come from consumers, ran in 40 ms.
Other apps you can build with DuckDB
- Energy: a smart meter or SCADA explorer over years of 15-minute Parquet files, with time_bucket and window functions.
- Finance: a transaction cost analysis app that matches each trade to the prevailing quote with ASOF JOIN.
- Pharma: a clinical operations app over trial sites and enrollment exports, with PIVOT for site-by-month tables.
- Science: a sensor log browser that queries a bucket of instrument files with read_parquet('s3://...') globs.
- Engineering: a test bench app that searches lab notes with the fts extension next to the measured signals.
What's new in DuckDB 2.0
DuckDB 2.0.0, released October 21, 2026, is a long-term support release supported until at least October 2027, and 2.0.1 followed on November 20. The main changes in 2.0:
- Quack and CONNECT. Any DuckDB process can serve its databases to others, and CONNECT attaches a remote DuckDB, PostgreSQL, or MySQL database in one statement.
- Asynchronous I/O for Parquet, CSV, and DuckDB files, which speeds up reads from object storage.
- VARIANT for semi-structured data, stored in columns instead of as JSON text.
- Agent mode in the CLI: when a coding agent drives it, the CLI prints compact tables and reports errors as JSON.
- Before you upgrade: database files written by 2.0 can't be read by earlier versions, and the old x -> x + 1 lambda syntax is off by default. The app here uses Parquet, which every version reads.
Running DuckDB on Plotly Cloud and Dash Enterprise
What we learned deploying Safety Signal Desk, which applies the same way on Dash Enterprise:
- Ship Parquet. The app's tables are 30.7 MB as zstd-compressed Parquet, against 66.9 MB as a .duckdb file. Views over Parquet answered as fast as tables in memory (about 0.4 s per signal query) and used 316 MB of memory instead of 577 MB.
- One connection, one cursor per callback. Dash runs callbacks in parallel, so open a single connection at startup and call con.cursor() in each callback. Set threads to what the container has.
- Lock down user SQL. If people can type queries, set allowed_directories, turn off enable_external_access, and set lock_configuration, then accept only SELECT statements.