View a markdown version of this page

Download and export query results - Amazon Athena

Download and export query results

You can download a query result as a CSV file from the Athena console. To export a query result to files that other tools can read directly, write it to Amazon S3 with a COPY ... TO statement. Use this method to get files when the workgroup uses managed query results. To make a query result available to other users or other query engines, create a new table with a CREATE TABLE AS SELECT (CTAS) query.

Download results from the console

For workgroups that use managed query results, the console downloads up to 200 MB of each result. If a result is larger, the console downloads the first 200 MB. It then shows the message The download stopped at 200 MB, so the file contains only the rows downloaded before the limit. To get the whole result, use GetQueryResultStream or COPY ... TO.

For workgroups that store results in your own Amazon S3 bucket (a customer managed bucket), the console downloads the full result file.

To download query results in the console
  1. Open the Athena console at https://console.aws.amazon.com/athena/.

  2. In the query editor, run your query.

  3. Choose Download results when the query finishes.

To download results of an earlier query, choose Recent queries, choose the query, and then choose Download results. You can download managed query results for 24 hours after the query runs.

Important

Query result files contain data that users can control. Some spreadsheet programs interpret parts of CSV data as commands (CSV injection). When you open a downloaded file in a spreadsheet program, turn off links and macros.

Write results to files with COPY syntax

A COPY ... TO statement runs a query and writes its result to an Amazon S3 location in the format you choose, such as CSV or Parquet.

Before you run the statement, make sure that the principal that runs the query can write to the Amazon S3 location. If the location uses server-side encryption with AWS KMS keys (SSE-KMS), the principal also needs permission to use the AWS KMS key.

The following COPY statement writes a result to a Parquet file. Replace my_database, my_table, amzn-s3-demo-bucket, and exports with your own values.

COPY (SELECT * FROM my_database.my_table) TO 's3://amzn-s3-demo-bucket/exports/output.parquet' (FORMAT parquet);

The following COPY statement writes a CSV file with a header row. Replace amzn-s3-demo-bucket and exports with your own values.

COPY (SELECT order_id, order_date, amount FROM duckdb_tutorial.orders WHERE order_date >= DATE '2026-11-02') TO 's3://amzn-s3-demo-bucket/exports/orders.csv' (HEADER, DELIMITER ',');

To verify that the file was written, list the location.

aws s3 ls s3://amzn-s3-demo-bucket/exports/

Write results to a table with CTAS

A CTAS query writes its result as the data of a new table. Use CTAS when you want to query the result again from Athena or other engines.

A CTAS query must include a WITH clause that sets the table type and its Amazon S3 location. The following CREATE TABLE AS SELECT query creates a Parquet table from a query. Replace amzn-s3-demo-bucket and tables with your own values.

CREATE TABLE duckdb_tutorial.daily_totals WITH (type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/daily_totals/') AS SELECT order_date, sum(amount) AS total FROM duckdb_tutorial.orders GROUP BY order_date;

The principal that runs the query needs permission to write to the Amazon S3 location and to create the table in the AWS Glue Data Catalog.