Topic 2 of 5 · 4 min read
Local analytics with DuckDB
Goal: Query a folder of Parquet files with DuckDB and check the result against a total you already know.
DuckDB is an analytical SQL engine that runs inside your process and reads CSV, Parquet and JSON files where they sit, so you can answer a question about a folder of files without setting up a server or a load job. It is column-oriented and vectorized, which makes it fast on one machine for data that fits on disk, even when it does not fit in memory.
Check yourself · 1 of 3
What does DuckDB let you do that saves a loading step?
Why it matters
Many analytics jobs are one person, one question, a few gigabytes. Loading that into PostgreSQL first is slow and leaves a copy to maintain. With DuckDB you point SQL at the files. It also reads from S3-compatible storage and works well inside a notebook or a CI check.
How to do it
- Start it. From the command line run
duckdb, or in Pythonimport duckdb. An in-memory session needs no file; pass a path to keep tables. - Query files directly. A file or glob in
FROMworks as a table:FROM 'data/events/*.parquet'. Useread_csvorread_jsonwhen you need options. - Inspect before trusting. Run
DESCRIBE SELECT * FROM 'data/events/*.parquet'to see inferred types, andcount(*)to see the row count. - Verify against a known total. Compare your result to a figure you already trust, such as the finance monthly total or the row count in the export log.
- Save results.
COPY (...) TO 'out.parquet'writes a result file for the next step.
Before and after
An assistant writes a script that loads everything into pandas:
import pandas as pd, glob
df = pd.concat(pd.read_parquet(f) for f in glob.glob("data/events/*.parquet"))
print(df.groupby("country")["amount"].sum())
That reads every column of every file into memory. The DuckDB version reads two columns and streams:
import duckdb
rows = duckdb.sql('''
SELECT country, sum(amount) AS revenue
FROM 'data/events/*.parquet'
GROUP BY country
ORDER BY revenue DESC
''').fetchall()
Then check the sum of revenue against the known monthly total before the numbers go in a slide.
What goes wrong
- Silent type inference. CSV columns can be read as the wrong type, for example an id with leading zeros turned into an integer. Declare types for files you do not control.
- Mismatched schemas across files. A glob over files with different columns can fail or fill nulls; use
union_by_name = truedeliberately. - Using it as a shared server. DuckDB is an embedded database. One process writes a database file at a time, so it does not replace a warehouse for many concurrent users.
- Trusting the output unchecked. A wrong glob quietly includes a duplicate export and doubles your totals.
Examples
Daily revenue from exported files
SELECT date_trunc('day', created_at) AS day,
sum(amount) AS revenue
FROM 'exports/orders/2026-11-*.parquet'
GROUP BY day
ORDER BY day;
Sum the revenue column and compare it to the November total in the finance report. If they differ, check for duplicate files in the folder first.
Pin the types of a CSV
SELECT *
FROM read_csv('customers.csv',
columns = {'customer_id': 'VARCHAR', 'signup': 'DATE'});
Without the declaration, an id such as 00123 may be read as the integer 123 and fail to join to the orders table.
Key points
- DuckDB runs inside your process and queries CSV, Parquet and JSON files in place with plain SQL.
- It reads only the columns and row groups a query needs, so it handles files larger than memory.
- Check inferred types with
DESCRIBEand row counts before you build on a result. - Verify a query against a total you already trust; a wrong glob can double-count a duplicate file.
- It is an embedded engine for one user or one job, not a server for many concurrent writers.
Common mistakes
- Loading files into pandas or a server first when DuckDB can query them in place.
- Skipping
DESCRIBEand accepting the types a CSV sniffer guessed. - Reporting a result without comparing it to a known total.
- Treating DuckDB as a multi-user warehouse with concurrent writers.
Check yourself · 2 of 3
A DuckDB query over 'exports/*.parquet' returns revenue exactly double the finance total. What should you check first?
Check yourself · 3 of 3
A customers.csv has ids such as 00123. A DuckDB join to orders finds no matches. What is the likely cause and fix?
Practice items, not counted towards your mastery.
Start here
- DuckDB documentation (opens in a new tab)DocsFree
The engine's own reference for reading files, SQL syntax and functions.
- ClickHouse documentation (opens in a new tab)DocsFree
Official guides for column-oriented storage, ingestion and real-time analytics at scale.
- Apache Parquet documentation (opens in a new tab)DocsFree
The file format overview, with the specification of row groups and encodings.
More for this topic
- DuckDB: Parquet overview (opens in a new tab)DocsFree
Shows how to read, filter and write Parquet files with SQL, including globs and options.