View a markdown version of this page

SQL reference - Amazon Athena

SQL reference

DuckDB queries use the SQL dialect used by DuckDB. The topics in this section describe the statements, data types, expressions, functions and operators, and table and file formats that you can use. Each example shows the result that DuckDB returns, or the error message when the statement fails. Where DuckDB works differently from Athena engine version 3, each topic describes the difference.

Along with standard SQL, you can use the SQL additions of open-source DuckDB, such as FROM-first queries, GROUP BY ALL, QUALIFY, ASOF joins, and PIVOT. For these features, see SELECT statement in DuckDB. For lambda functions, TRY, TRY_CAST, the :: operator, and list and struct values, see Expressions and operators in DuckDB. For map values, see MAP type. For the FILTER clause of an aggregate function, see Aggregate function clauses. For SUMMARIZE, see DESCRIBE, SHOW, SUMMARIZE, and EXPLAIN statements in DuckDB.

For information about how DuckDB runs each query, see How queries run. For information about how to name catalogs, databases, and tables, see Work with the AWS Glue Data Catalog. For a walkthrough that creates, loads, and queries tables, see Tutorial: create, load, and query tables. For information about how to move queries from Athena engine version 3, see SQL differences from Athena v3.

How queries run

Each query is a single SQL statement. A request with more than one statement fails. Each query runs on isolated serverless capacity used for that query only, so nothing carries over from one query to the next. Because of this, session statements such as SET, USE, ATTACH, INSTALL, LOAD, PREPARE, BEGIN, and CREATE MACRO are not supported or have no lasting effect. Athena checks each statement before DuckDB runs it, when the query runs rather than when you submit it.

StartQueryExecution accepts a query that has a syntax error and returns a query execution ID; the error appears when the query runs. The one exception is a multi-statement request, which fails at submission.

You cannot change DuckDB settings such as the memory limit, the number of threads, or the session time zone, which is UTC. Without an ORDER BY clause, the order of rows in a result is not guaranteed.

Statement families

The following topics describe the statements that DuckDB supports, with examples:

For information about how to create and drop databases, which statements each table type supports, and how to write query results to Amazon S3 with COPY (SELECT ...) TO, see Work with the AWS Glue Data Catalog. For information about views, see Work with views.

For information about which DuckDB statement to use in place of an Athena engine version 3 statement, see Statement mapping. That section also lists the engine version 3 statements that have no replacement.

For the open-source DuckDB statements that you cannot use, such as SET, USE, ATTACH, transactions, and CREATE MACRO, see Open-source DuckDB functions and features not available in Athena. You also cannot use LOAD DATA.

Data types that each table format can store

For the data types that you can use in queries, see Data types in DuckDB. The types that a table can store depend on its table format:

  • Hive tables cannot store HUGEINT, UBIGINT, INTERVAL, ENUM, BIT, or UNION values. Because sum of an integer column returns a HUGEINT, cast the result, for example CAST(sum(x) AS BIGINT).

  • Iceberg tables cannot store UBIGINT, UHUGEINT, BIGNUM, BIT, INTERVAL, JSON, TUPLE, UNION, or fixed-size ARRAY values. DuckDB widens TINYINT, SMALLINT, UTINYINT, USMALLINT, and UINTEGER to INTEGER or BIGINT, and HUGEINT to DECIMAL(38, 0). A GEOMETRY or VARIANT column needs format version 3.

To write a column of a type that the table cannot store, cast it in the query, for example CAST(n AS BIGINT) AS n. For more information, see CREATE TABLE AS and Column types.

Tutorial: create, load, and query tables

This tutorial creates a database, creates Hive and Apache Iceberg tables in the AWS Glue Data Catalog, loads and changes data, and cleans up. Run each statement as its own query, with the catalog set to AwsDataCatalog. Replace amzn-s3-demo-bucket with your own bucket. This tutorial creates resources that can incur charges, so complete Step 6 to avoid incurring additional charges.

Step 1: Create a database. In DuckDB, a database is a schema.

CREATE SCHEMA duckdb_tutorial

Step 2: Create a Hive table and add rows. Every CREATE TABLE takes its storage settings in a WITH clause. type is required, and location is required outside S3 Tables.

CREATE TABLE duckdb_tutorial.sales_parquet ( order_id INTEGER, customer VARCHAR, region VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/duckdb-tutorial/sales_parquet/' ) INSERT INTO duckdb_tutorial.sales_parquet VALUES (1, 'alice', 'east', 120.50, DATE '2026-10-01'), (2, 'bob', 'west', 75.00, DATE '2026-10-01'), (3, 'carol', 'east', 42.25, DATE '2026-10-02')

INSERT INTO is the only statement that adds rows to an existing Hive table. To change rows, use an Iceberg table, see Step 4.

Step 3: Create a Hive table from a query (CTAS). A CTAS takes the same WITH options as CREATE TABLE.

CREATE TABLE duckdb_tutorial.sales_by_region WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/duckdb-tutorial/sales_by_region/' ) AS SELECT region, count(*) AS order_count, sum(amount) AS revenue FROM duckdb_tutorial.sales_parquet GROUP BY region

Step 4: Create an Iceberg table and change rows. Iceberg tables support row-level changes.

CREATE TABLE duckdb_tutorial.orders ( order_id INTEGER, customer VARCHAR, region VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/duckdb-tutorial/orders/', 'format-version' = '2' ) INSERT INTO duckdb_tutorial.orders SELECT order_id, customer, region, amount, order_date FROM duckdb_tutorial.sales_parquet UPDATE duckdb_tutorial.orders SET amount = 80.00 WHERE order_id = 2 DELETE FROM duckdb_tutorial.orders WHERE order_id = 3

UPDATE and DELETE work on Iceberg tables in the AWS Glue Data Catalog, not on Hive tables. MERGE INTO is not supported in this Preview.

Step 5: Export query results with COPY TO . Use COPY (SELECT ...) TO in place of engine version 3 UNLOAD.

COPY ( SELECT region, order_count, revenue FROM duckdb_tutorial.sales_by_region ORDER BY region ) TO 's3://amzn-s3-demo-bucket/duckdb-tutorial/export/by_region.parquet' (FORMAT parquet)

Step 6: Clean up. To avoid incurring additional charges, delete the resources that you created in this tutorial. DROP TABLE on a Hive or Iceberg table in the AWS Glue Data Catalog removes only the catalog entry; the data files stay in Amazon S3.

DROP TABLE duckdb_tutorial.sales_by_region DROP SCHEMA duckdb_tutorial CASCADE

Then delete the data in Amazon S3 yourself.

aws s3 rm s3://amzn-s3-demo-bucket/duckdb-tutorial/ --recursive

Considerations and limitations

  • Each query must contain exactly one SQL statement, and nothing carries over between queries.

  • You cannot change engine settings, including the session time zone, which is UTC.

  • A syntax error appears when the query runs, not when you submit it.

  • Without ORDER BY, the order of result rows is not guaranteed.

  • You cannot create bucketed, Delta Lake, Hudi, or ORC tables.

  • DROP TABLE keeps the data in Amazon S3 for Hive and Iceberg tables in the AWS Glue Data Catalog, and deletes it for S3 Tables.

  • A write that fails with a transient-error message may already have been applied. Check before you retry it.

  • All catalogs that you query must be in your own account. Cross-account access is not supported in this Preview.