# DuckDB: A Practical Guide to Analytics on Your Own Machine

Someone sends you a 6 GB CSV export and asks for revenue by country by month. A spreadsheet refuses to open it. A quick Python script with pandas eats all your memory and dies. The warehouse team says they can load it for you by Thursday.

Or you type this in a terminal:

```sql
SELECT country, date_trunc('month', order_date) AS month, sum(total) AS revenue
FROM 'orders.csv'
GROUP BY ALL
ORDER BY month, revenue DESC;
```

A few seconds later you have the answer. No server was started, no table was created, no data was loaded. The file name in the `FROM` clause is not a typo.

That is DuckDB. It has become the favourite tool of people who work with data on their own machines, and it changes which jobs need a warehouse at all.

## What DuckDB is

DuckDB is an in-process analytical database. Each part of that phrase matters:

- **In-process** means there is no server. Like SQLite, it is a library that runs inside your program, or a single command-line binary. Nothing listens on a port.
- **Analytical** means it is built for questions over many rows: sums, groups, joins, window functions. It stores data by column and processes it in batches across all your CPU cores.
- **Database** means real SQL with transactions, and an optional single file on disk that holds your tables.

It is open source under the MIT licence. It began as a research project at CWI in Amsterdam, and the name and code are held by a non-profit foundation.

People call it "SQLite for analytics". The comparison is useful:

| | SQLite | DuckDB |
|---|---|---|
| Runs as | A library, no server | A library, no server |
| Stores data | Row by row | Column by column |
| Best at | Many small reads and writes | Scans and aggregations over many rows |
| Typical home | Inside an application | In a notebook, a script or a terminal |
| Uses cores | One per query | All of them |

## Why people choose it

- **It queries files directly.** CSV, Parquet and JSON files can be used as if they were tables, including whole folders of them and files in cloud storage.
- **It is fast on one machine.** Columnar, parallel execution makes a laptop handle data sets that used to need a cluster.
- **It handles data bigger than memory.** When a sort or join does not fit in RAM, DuckDB spills to disk and keeps going.
- **Friendly SQL.** `GROUP BY ALL`, `SELECT * EXCLUDE (...)`, `SUMMARIZE` and similar shortcuts remove a lot of typing.
- **It lives where you work.** There are packages for Python, R, Node.js, Java, Go and Rust, and it reads and writes pandas and Arrow data without copying it.
- **It reaches into other databases.** Extensions let it read PostgreSQL, MySQL and SQLite directly.

## Where DuckDB fits, and where it does not

**A strong choice for:**

- Exploring and cleaning data files on your laptop
- Data science notebooks, in place of or beside pandas
- Transformation steps in a pipeline: read files, reshape, write Parquet
- Analytics embedded in an application or a command-line tool
- Reports on small and medium data that do not justify a warehouse
- Tests for SQL that will later run on a warehouse

**Look elsewhere when:**

- **Many users or services need the same database at once.** DuckDB has no server and no user accounts. A file can be opened by one process that writes, or by several that only read. For a shared database use PostgreSQL, or a warehouse.
- **The workload is transactional.** Many small inserts and updates per second from an application are SQLite's or PostgreSQL's job.
- **The data far exceeds one machine's disk.** That is what Snowflake, BigQuery, Databricks and ClickHouse are for.
- **You need the database to be reachable over the network.** Something has to host it. MotherDuck is a commercial cloud service built on DuckDB, and it is a separate product.

## Using it the right way

Install the command-line client from duckdb.org or a package manager, then:

```bash
duckdb                  # a temporary in-memory database
duckdb analytics.duckdb # a database kept in one file
```

In Python: `pip install duckdb`.

### Query files where they are

```sql
-- One file
SELECT count(*) FROM 'orders.parquet';

-- CSV with detected column types
SELECT * FROM read_csv('orders.csv') LIMIT 10;

-- A folder laid out as year=2026/month=03/...
SELECT year, count(*)
FROM read_parquet('events/*/*/*.parquet', hive_partitioning = true)
WHERE year = 2026
GROUP BY year;
```

For CSV files, check what DuckDB guessed before you trust the numbers:

```sql
DESCRIBE SELECT * FROM 'orders.csv';
SUMMARIZE SELECT * FROM 'orders.csv';
```

`SUMMARIZE` shows each column's type, minimum, maximum, null percentage and approximate distinct count. It is the fastest first look at an unfamiliar file.

### Convert CSV to Parquet early

CSV has to be parsed again on every query. Parquet is columnar, compressed and typed, so later queries read only what they need.

```sql
COPY (SELECT * FROM read_csv('orders.csv'))
TO 'orders.parquet' (FORMAT parquet);
```

To write a partitioned folder:

```sql
COPY (SELECT *, year(order_date) AS year FROM 'orders.parquet')
TO 'orders_by_year' (FORMAT parquet, PARTITION_BY (year));
```

### Keep tables when you will reuse them

```sql
CREATE TABLE orders AS SELECT * FROM 'orders.parquet';
```

Inside a `.duckdb` file, data is stored in DuckDB's own format, which is the fastest to query. Since version 1.0 the file format is backward compatible, so newer releases can read files written by older ones.

### Use the SQL shortcuts

```sql
-- Group by every non-aggregated column
SELECT country, status, count(*) FROM orders GROUP BY ALL;

-- Everything except some columns
SELECT * EXCLUDE (internal_notes, raw_payload) FROM orders;

-- FROM first, handy for a quick look
FROM orders LIMIT 5;
```

### Read from cloud storage

```sql
INSTALL httpfs;
LOAD httpfs;

CREATE SECRET (
  TYPE s3,
  KEY_ID 'your-access-key-id',
  SECRET 'your-secret-key',
  REGION 'us-east-1'
);

SELECT status, count(*)
FROM 's3://my-bucket/orders/*.parquet'
GROUP BY ALL;
```

`CREATE SECRET` needs version 0.10 or later. With Parquet, DuckDB fetches only the columns and row groups a query needs, so a remote query often downloads a small part of each file.

### Join against a live database

```sql
INSTALL postgres;
LOAD postgres;

ATTACH 'dbname=shop host=localhost user=reader' AS pg (TYPE postgres, READ_ONLY);

SELECT c.country, sum(o.total) AS revenue
FROM 'orders.parquet' AS o
JOIN pg.public.customers AS c ON c.id = o.customer_id
GROUP BY ALL;
```

A file on your disk joined to a table in PostgreSQL, in one statement. `ATTACH 'app.db' AS app (TYPE sqlite);` does the same for a SQLite file.

### Work from Python

```python
import duckdb
import pandas as pd

customers = pd.read_csv("customers.csv")

top = duckdb.sql("""
    SELECT c.country, sum(o.total) AS revenue
    FROM 'orders.parquet' AS o
    JOIN customers AS c ON c.id = o.customer_id
    GROUP BY ALL
    ORDER BY revenue DESC
""").df()
```

DuckDB finds the `customers` DataFrame by its variable name and reads it in place. `.df()` returns the result as a DataFrame. To keep tables between runs, use `duckdb.connect("analytics.duckdb")`.

### Set limits on a shared machine

```sql
SET memory_limit = '4GB';
SET threads = 4;
SET temp_directory = '/tmp/duckdb_spill';
```

By default DuckDB uses most of the machine's memory and every core, which is what you want on your own laptop and not on a server you share.

### Know the concurrency rule

One process may open a database file for reading and writing. Alternatively, any number of processes may open it read-only:

```bash
duckdb -readonly analytics.duckdb
```

Inside one process, many threads can read and write together. If several programs need to write, give that job to a single process, or write Parquet files and let everyone read those.

## What it costs, and what it saves

DuckDB is free. There is no licence fee, no credits and no per-query charge. The cost is your own hardware: memory, cores and disk.

That makes it a useful partner to a cloud warehouse, where every query has a price:

- Develop and test SQL locally on a sample exported to Parquet, then run the final version on the warehouse.
- Do small transformations in a pipeline step with DuckDB, without waking a warehouse for a few megabytes.
- Answer one-off questions about an export yourself, without loading it anywhere.

When you read from cloud storage, the storage provider still charges for requests and for data leaving its network. Running DuckDB on a machine in the same cloud region as the bucket avoids most of that.

## Common mistakes

- **Using it as an application's main transactional database.** It will work in a demo and struggle under many small concurrent writes.
- **Opening one file for writing from two programs.** The second one is refused. Plan for a single writer.
- **Inserting row by row.** Load in bulk from files or DataFrames, or use the client library's appender.
- **Trusting detected CSV types blindly.** A postcode column read as a number loses its leading zeros. Check with `DESCRIBE`, and pass `types` or `all_varchar = true` to `read_csv` when needed.
- **Querying the same big CSV again and again.** Convert it to Parquet once.
- **Adding indexes out of habit.** DuckDB keeps minimum and maximum values for each block of a column automatically. Extra indexes help single-row lookups and slow down loads.
- **Forgetting the in-memory default.** Running `duckdb` with no file name means everything vanishes when you quit.
- **Running it unrestricted on a shared server.** Set `memory_limit` and `threads`.

## Quick reference

| Task | Command |
|---|---|
| Open or create a database file | `duckdb analytics.duckdb` |
| List tables | `SHOW TABLES;` or `.tables` |
| Describe a table or query | `DESCRIBE orders;` |
| Profile a table | `SUMMARIZE orders;` |
| Time each statement | `.timer on` |
| See the plan with real timings | `EXPLAIN ANALYZE SELECT ...;` |
| Export a table | `COPY orders TO 'orders.parquet' (FORMAT parquet);` |
| Export everything | `EXPORT DATABASE 'backup_dir' (FORMAT parquet);` |
| Installed extensions | `SELECT extension_name, installed, loaded FROM duckdb_extensions();` |
| Version | `SELECT version();` |

Before you load a file into a warehouse to answer one question, try pointing DuckDB at it. A surprising share of "big data" fits on the machine you already have.

## Exploring DuckDB with CreamDB

One plain limit: a DuckDB database is a file on your own computer, and [CreamDB](https://www.creamdb.com) is a hosted workspace in your browser, so it cannot open that file. For local DuckDB work, the `duckdb` shell or a notebook is the right tool.

CreamDB helps once the data lives somewhere reachable: a PostgreSQL or ClickHouse server, a warehouse such as Snowflake or BigQuery, or a hosted SQLite-style service such as Turso. There you get a SQL editor, result export and an assistant that answers plain-English questions with SQL and a chart.

