The Problem
You’ve spent the last hour cleaning data in DuckDB. Your query is perfect. The results are ready. Now you need to send them to your manager who only opens Excel files.
The traditional workflow:
- Export to CSV from DuckDB
- Open Python/Pandas
- Convert CSV to Excel
- Save the .xlsx file
- Send the file
That’s 5 steps for something that should take 1.
The One-Line Solution
COPY (
SELECT
product_name,
SUM(sales) AS total_sales,
AVG(price) AS avg_price,
COUNT(*) AS orders
FROM orders
WHERE order_date >= '2026-01-01'
GROUP BY product_name
ORDER BY total_sales DESC
) TO 'sales_report.xlsx' (HEADER, MODE_CSV);
That’s it. One SQL statement. No Python. No Pandas. No CSV intermediary.
How It Works
DuckDB’s COPY TO command supports multiple output formats:
-- Export to Excel (.xlsx)
COPY (SELECT * FROM my_table) TO 'output.xlsx' (HEADER);
-- Export to CSV
COPY (SELECT * FROM my_table) TO 'output.csv' (HEADER);
-- Export to Parquet
COPY (SELECT * FROM my_table) TO 'output.parquet';
-- Export to JSON
COPY (SELECT * FROM my_table) TO 'output.json';
Performance Comparison
| Method | Lines of Code | Time | Memory |
|---|---|---|---|
| DuckDB COPY TO | 1 line | ~2s | Minimal |
| Python + Pandas | 5-10 lines | ~8s | High (loads full dataframe) |
| CSV export + manual conversion | 3+ steps | ~15s | Medium |
For a 100MB query result:
- DuckDB COPY: Direct, streaming, ~200MB memory peak
- Pandas to_excel: Loads everything into memory first, ~1GB+ memory peak
When You’ll Use This
- Sending weekly reports to management (Excel format)
- Exporting analysis results for non-technical stakeholders
- Creating data packages for colleagues who don’t use DuckDB
- Quick ad-hoc exports without writing Python scripts
The Takeaway
One rule of thumb: When you need to export data from DuckDB, use COPY TO first. It’s faster, uses less memory, and requires zero additional tools.
The only limitation: Excel export doesn’t support complex data types (arrays, structs). For those, export to Parquet or JSON instead.
Subscribe to DuckDB Lab for weekly practical tips you can use today. 🦆