DuckDB One Trick: Export to Excel in One SQL Line (No Python, No Pandas)

Export DuckDB query results directly to Excel (.xlsx) with a single COPY statement. No Python, no Pandas, no middlemen — just SQL.

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:

  1. Export to CSV from DuckDB
  2. Open Python/Pandas
  3. Convert CSV to Excel
  4. Save the .xlsx file
  5. 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

MethodLines of CodeTimeMemory
DuckDB COPY TO1 line~2sMinimal
Python + Pandas5-10 lines~8sHigh (loads full dataframe)
CSV export + manual conversion3+ steps~15sMedium

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. 🦆

📺 Watch video tutorials → Olap Studio YouTube

Subscribe for more DuckDB & AI automation tutorials

Built with Hugo
Theme Stack designed by Jimmy

⚠️ This site is an independent community project, not affiliated with, endorsed by, or sponsored by the DuckDB Foundation or official DuckDB project.

"DuckDB" is a registered trademark of the DuckDB Foundation. This site uses the name solely for factual description purposes.

All content is for educational and community promotion purposes only and does not constitute any commercial service.