

# Use parameterized and saved queries

With Athena, powered by DuckDB, you can run the same query many times with different values by using *execution parameters*. You can also save a query with a name and run it again later. This topic describes both features and how they differ from Athena engine version 3. It shows how to use them with the AWS Command Line Interface (AWS CLI) and the Athena API.

The examples in this topic use the `duckdb_tutorial.orders` Iceberg table from the tutorial in [Tutorial: create, load, and query tables](duckdb-sql-reference.md#athena-duckdb-sql-tutorial-create-load-and-query-tables), and a workgroup named `my-duckdb-workgroup`. To run the examples, replace these names with your own table and DuckDB workgroup.

## Parameterized queries and prepared statements
<a name="athena-duckdb-parameterized-queries-parameterized-queries-and-prepared-statements"></a>

A *parameterized query* has a question mark (`?`) in place of each value that changes from one run to the next. You supply the values when you start the query, in the `ExecutionParameters` field of `StartQueryExecution`. Athena binds each value into the statement before the statement runs.

Parameterized queries help you in the following ways:
+ You can reuse one query text with different filter values, dates, or limits.
+ You can help prevent SQL injection. Each value must be a single constant, such as `'east'` or `DATE '2026-10-01'`. Athena refuses a value that is a column, a function call, a subquery, or an expression, so a value cannot add SQL to the statement.

With DuckDB, you can run parameterized queries with `ExecutionParameters`. DuckDB does **not** support SQL prepared statements (`PREPARE`, `EXECUTE`, and `DEALLOCATE PREPARE`). Each query runs on its own isolated capacity, so a statement that one query prepares does not exist in the next query. To reuse a query, use execution parameters, a saved query, or both.

**Note**  
JDBC and ODBC applications use "prepared statement" to mean a statement with bound parameters. That is not the same as the SQL `PREPARE` statement.

## Run a query with execution parameters
<a name="athena-duckdb-parameterized-queries-run-a-query-with-execution-parameters"></a>

Put a `?` in the query for each value. When you run the query, give the values in order, one value for each `?`.

### Run a parameterized query with the AWS CLI
<a name="athena-duckdb-parameterized-queries-run-a-parameterized-query-with-the-aws-cli"></a>

To pass the values, use the `--execution-parameters` option, and list the values in the order of the `?` placeholders in the query.

The following AWS CLI command filters orders by a region string and a date.

```bash
aws athena start-query-execution \
  --work-group my-duckdb-workgroup \
  --query-execution-context Catalog=AwsDataCatalog,Database=duckdb_tutorial \
  --query-string "SELECT order_id, customer, amount FROM orders WHERE region = ? AND order_date >= ?" \
  --execution-parameters "'east'" "DATE '2026-10-01'"
```

The command returns a `QueryExecutionId`. To check the state of the query, use `get-query-execution`. To read the rows, use `get-query-results`.

The following AWS CLI command inserts a row with five values.

```bash
aws athena start-query-execution \
  --work-group my-duckdb-workgroup \
  --query-string "INSERT INTO duckdb_tutorial.orders (order_id, customer, region, amount, order_date)
                  VALUES (?, ?, ?, ?, ?)" \
  --execution-parameters "4" "'Paulo Santos'" "'west'" \
      "CAST('18.75' AS DECIMAL(10,2))" "DATE '2026-10-03'"
```

The following AWS CLI command uses one value twice. `$1` and `?1` refer to a value by its position, so you can use the same value more than once.

```bash
aws athena start-query-execution \
  --work-group my-duckdb-workgroup \
  --query-string "SELECT * FROM duckdb_tutorial.orders WHERE amount >= \$1 OR order_id = \$2 OR amount * 2 >= \$1" \
  --execution-parameters "100" "3"
```

### Run a parameterized query with an AWS SDK
<a name="athena-duckdb-parameterized-queries-run-a-parameterized-query-with-an-aws-sdk"></a>

With an AWS software development kit (SDK), call `StartQueryExecution`, and set `ExecutionParameters` to a list of strings. Each string is the SQL text of one value. The following Python code runs a parameterized query with the AWS SDK for Python (Boto3).

```python
import boto3

athena = boto3.client("athena")

response = athena.start_query_execution(
    WorkGroup="my-duckdb-workgroup",
    QueryExecutionContext={"Catalog": "AwsDataCatalog", "Database": "duckdb_tutorial"},
    QueryString="SELECT order_id, amount FROM orders WHERE region = ? AND amount > ?",
    ExecutionParameters=["'east'", "50"],
)
print(response["QueryExecutionId"])
```

## Rules for parameter values
<a name="athena-duckdb-parameterized-queries-rules-for-parameter-values"></a>

Each value is the SQL text of one constant. The following table lists the kinds of values that you can pass.


| Kind of value | Examples | 
| --- | --- | 
| Number | 1, -1.5 | 
| String, in single quotes | 'abc', 'O''Brien' | 
| NULL, TRUE, FALSE | NULL | 
| Typed literal | DATE '2014-07-05', TIMESTAMP '2026-10-01 00:00:00' | 
| Cast of a literal | CAST('2014-07-05' AS DATE), CAST('18.75' AS DECIMAL(10,2)) | 

Athena refuses the following values, and the query fails:
+ A column name, such as `region`
+ A function call, such as `current_date` or `upper('a')`
+ A subquery, such as `(SELECT max(id) FROM t)`
+ An expression, such as `1 + 1`

When you write placeholders and values, follow these rules:
+ **Positional `?` placeholders.** Values go to the `?` placeholders in order. The one exception is a `SELECT` statement with a `WINDOW` clause. The `?` placeholders in the `WINDOW` clause get values first, before the placeholders in the select list, `FROM`, `WHERE`, `GROUP BY`, and `HAVING`. In `SELECT ?, sum(x) OVER w FROM t WINDOW w AS (ROWS ? PRECEDING)`, the first value goes to the window frame, and the second value goes to the select list.
+ **Numbered placeholders.** `$1` and `?1` refer to the first value, `$2` and `?2` to the second, and so on. A numbered placeholder can appear more than once.
+ **Named parameters.** Athena refuses named parameters, such as `$region`.
+ **Number of values.** Give exactly as many values as the highest parameter number in the query. That is the number of `?` placeholders, or the highest `n` among the `$n` and `?n` placeholders. If you give too many or too few values, the query fails.
+ **Quotes.** A `?` inside quotes (`'?'` or `"?"`) is text, not a placeholder. To pass a string, put the single quotes in the value (`'east'`), not around the `?`.
+ **Types.** A value keeps the type that its SQL text gives it. `'2026-10-01'` is a string, and `DATE '2026-10-01'` is a date. When the column type matters, use a typed literal or `CAST`.

## Where you can use parameters
<a name="athena-duckdb-parameterized-queries-where-you-can-use-parameters"></a>

You can use a parameter where a statement takes a constant value, such as in `WHERE`, `HAVING`, `LIMIT`, `OFFSET`, `VALUES`, select-list expressions, and function arguments. You can use parameters in the following statements:
+ `SELECT`
+ `INSERT INTO`
+ `CREATE TABLE AS SELECT`, in the query
+ `UPDATE` and `DELETE` on Apache Iceberg tables
+ `COPY (SELECT ...) TO`, in the `SELECT` query

You cannot use a parameter in the following places:
+ **A whole `ORDER BY`, `GROUP BY`, or `DISTINCT ON` item, such as `ORDER BY ?`.** Bound to `1`, the parameter would mean column 1. Write the column name in the query.
+ **A `CREATE TABLE ... WITH (...)` property, such as `location = ?`.** Athena does not bind table properties. Write the value in the query.
+ **A `COPY ... TO` option, such as `(FORMAT ?)`.** Athena does not bind copy options. Write the value in the query.
+ **An `AT` clause for Iceberg time travel.** Athena does not bind the `AT` clause. Write the snapshot ID or timestamp in the query.
+ **A `CREATE VIEW` statement.** The stored view would keep the values in place of its parameters. Write the values in the view's query, or filter the view with parameters when you query it.
+ **A table, column, or schema name.** Parameters are values, not identifiers. Write the identifier in the query.

A query that contains `?` placeholders but no `ExecutionParameters` fails when it runs. The error message does not mention parameters.

## Use saved queries
<a name="athena-duckdb-parameterized-queries-use-saved-queries"></a>

A *saved query* (named query) stores a query's text with a name, an optional description, a database, and a workgroup. Saved queries work the same way in DuckDB workgroups as in Athena engine version 3 workgroups.
+ In the console, save a query from the query editor with **Save as**. You can find it on the **Saved queries** tab, where you can run, edit, rename, or delete it. For the steps, see [Use saved queries](saved-queries.md).
+ With the API, use `CreateNamedQuery`, `GetNamedQuery`, `BatchGetNamedQuery`, `ListNamedQueries`, `UpdateNamedQuery`, and `DeleteNamedQuery`.

Keep the following in mind when you use saved queries with DuckDB:
+ **A saved query runs on its workgroup's engine.** Athena does not check the SQL dialect when you save a query. A saved query in a DuckDB workgroup must use DuckDB syntax, or it fails when you run it.
+ **Switching a workgroup changes how its saved queries run.** After you switch a workgroup to DuckDB, every saved query in it runs on DuckDB. Before you switch, use the migration agent to translate your queries to the SQL dialect used by DuckDB and validate the translations. For more information, see [Migrate from Athena engine version 3 to DuckDB](duckdb-migration-agent.md).
+ **You cannot move a saved query to another workgroup.** To use an Athena engine version 3 saved query in a DuckDB workgroup, create a new saved query in the DuckDB workgroup with the SQL text for DuckDB.
+ **A saved query can contain parameters.** Save the query with its `?` placeholders. Each time that you run it, supply the values in `ExecutionParameters`.

The following AWS CLI commands save a parameterized query in a DuckDB workgroup, read the saved query text, and run it with values. Replace {{named-query-id}} with the ID that `create-named-query` returns.

```bash
aws athena create-named-query \
  --work-group my-duckdb-workgroup \
  --database duckdb_tutorial \
  --name "orders-by-region-since" \
  --description "Orders for one region since a date" \
  --query-string "SELECT order_id, customer, amount FROM orders WHERE region = ? AND order_date >= ?"

QUERY=$(aws athena get-named-query --named-query-id {{named-query-id}} \
  --query 'NamedQuery.QueryString' --output text)

aws athena start-query-execution \
  --work-group my-duckdb-workgroup \
  --query-execution-context Catalog=AwsDataCatalog,Database=duckdb_tutorial \
  --query-string "$QUERY" \
  --execution-parameters "'west'" "DATE '2026-10-01'"
```

## Differences from Athena engine version 3
<a name="athena-duckdb-parameterized-queries-differences-from-athena-engine-version-3"></a>

The following table compares parameterized and saved queries in Athena engine version 3 and DuckDB.


| Area | Athena engine version 3 | DuckDB | 
| --- | --- | --- | 
| PREPARE, EXECUTE, DEALLOCATE PREPARE | Supported. Prepared statements are stored for each workgroup. | Not supported. Use ExecutionParameters. | 
| Prepared statement API (CreatePreparedStatement and related operations) | Supported | Not supported. CreatePreparedStatement on a DuckDB workgroup returns InvalidRequestException. | 
| ExecutionParameters on StartQueryExecution | Supported | Supported | 
| Statements that take parameters | SELECT, INSERT INTO, CTAS, UNLOAD | SELECT, INSERT INTO, CTAS, UPDATE, DELETE, and the query in COPY (SELECT ...) TO. UNLOAD is not supported. | 
| Numbered placeholders ($1, ?1) | Not supported | Supported, and can repeat | 
| ? as a whole ORDER BY or GROUP BY item | Treated as a value | Refused | 
| CREATE VIEW with parameters | Not applicable | Refused | 
| Parameter order in a SELECT with a WINDOW clause | Order in the query text | WINDOW clause parameters first | 
| Saved queries | Supported | Supported. The query text must use DuckDB syntax. | 

## Troubleshoot parameterized queries
<a name="athena-duckdb-parameterized-queries-troubleshoot-parameterized-queries"></a>

The following sections describe the error messages that a parameterized query can return on a DuckDB workgroup, their causes, and how to fix them.

### `Incorrect number of parameters: expected N but found M`
<a name="athena-duckdb-parameterized-queries-incorrect-number-of-parameters-expected-n-but-found-m"></a>

**Cause:** You gave more or fewer values than the query has parameters.

**Resolution:**

1. Count the `?` placeholders in the query, or find the highest `n` among its `$n` and `?n` placeholders.

1. Pass exactly that many values in `ExecutionParameters`.

### `Execution parameter N must be a single constant value, such as 1, 'abc' or CAST('2014-07-05' AS DATE)`
<a name="athena-duckdb-parameterized-queries-execution-parameter-n-must-be-a-single-constant-value-such-as-1-abc-or-cast-2014-07-05-as-date"></a>

**Cause:** Value `N` is a column, a function call, a subquery, or an expression. A string value without single quotes, or with double quotes, is read as a column name.

**Resolution:**

1. Replace value `N` with a literal or a cast of one, such as `'east'` or `CAST('2026-10-01' AS DATE)`.

### `Named parameters are not supported with execution parameters: $name`
<a name="athena-duckdb-parameterized-queries-named-parameters-are-not-supported-with-execution-parameters-name"></a>

**Cause:** The query uses a named parameter, such as `$region`.

**Resolution:**

1. Replace each named parameter with `?`, or with a numbered placeholder such as `$1` or `?1`.

### `Execution parameters are not supported in CREATE VIEW`
<a name="athena-duckdb-parameterized-queries-execution-parameters-are-not-supported-in-create-view"></a>

**Cause:** The query is a `CREATE VIEW` statement, and you passed `ExecutionParameters`. A view cannot keep the values in place of its parameters.

**Resolution:**

1. Write the values in the view's query, and run `CREATE VIEW` without `ExecutionParameters`.

1. To filter the view by different values, use parameters when you query the view.

### `Something went wrong while Athena was running this query...`
<a name="athena-duckdb-parameterized-queries-something-went-wrong-while-athena-was-running-this-query"></a>

**Cause:** The query has `?` placeholders, but you did not pass `ExecutionParameters`. The error message does not mention parameters.

**Resolution:**

1. Pass one value for each placeholder in `ExecutionParameters`, or remove the placeholders from the query.

### `PREPARE statements aren't supported.`
<a name="athena-duckdb-parameterized-queries-prepare-statements-aren-t-supported"></a>

**Cause:** The query is a `PREPARE`, `EXECUTE`, or `DEALLOCATE PREPARE` statement. DuckDB does not support prepared statements. The `EXECUTE` and `DEALLOCATE PREPARE` messages are `EXECUTE statements aren't supported.` and `DROP PREPARED statements aren't supported.`

**Resolution:**

1. Put `?` placeholders in the query itself, and pass the values in `ExecutionParameters`.

### `Parameter not supported in ORDER BY clause`
<a name="athena-duckdb-parameterized-queries-parameter-not-supported-in-order-by-clause"></a>

**Cause:** A whole `ORDER BY`, `GROUP BY`, or `DISTINCT ON` item is a parameter. Bound to `1`, it would mean column 1.

**Resolution:**

1. Write the column name in the query.

### `Parameter N is not supported in this position`
<a name="athena-duckdb-parameterized-queries-parameter-n-is-not-supported-in-this-position"></a>

**Cause:** Parameter `N` is in a table property, a `COPY` option, or an `AT` clause, which Athena does not bind.

**Resolution:**

1. Write the value in the query.

## Related topics
<a name="athena-duckdb-parameterized-queries-related-topics"></a>

For more information, see the following topics:
+ [Use parameterized queries](querying-with-prepared-statements.md)
+ [Use saved queries](saved-queries.md)
+ [SQL reference](duckdb-sql-reference.md)
+ [StartQueryExecution](https://docs.aws.amazon.com/athena/latest/APIReference/API_StartQueryExecution.html) in the *Amazon Athena API Reference*