Sub topic 6
Making Queries Reusable with Parameters
Assume you know basic SQL and understand Dune’s curated tables. This chapter focuses on query parameters: named values that a user can change without editing the SQL text.
A parameter turns a fixed query into a reusable query. For example, instead of writing one query for Ethereum and another for Arbitrum, write one query with a blockchain parameter.
How parameter substitution works
Dune uses double braces to mark a parameter:
{{blockchain}}When the query runs, Dune replaces the placeholder with the value configured in the query editor or selected by a dashboard viewer. For a text parameter used as a string value, include single quotes around the placeholder:
WHERE blockchain = '{{blockchain}}'The SQL parser receives a normal value after substitution. The parameter itself is configured separately from the query text. You can usually give it a type, a default value, and, for some parameter types, a list of allowed values.
| Parameter | Example value | Typical use |
|---|---|---|
| `blockchain` | `ethereum` | Select a chain |
| `project` | `uniswap` | Select a protocol |
| `days` | `30` | Change a lookback period |
Build a parameterized query
This query groups DEX volume by day and project. The blockchain value can change at run time:
SELECT
block_date,
project,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = '{{blockchain}}'
AND block_date >= current_date - INTERVAL '30' DAY
GROUP BY 1, 2
ORDER BY 1, 3 DESCTo use it:
- Open the query in Dune.
- Create a text parameter named
blockchain. - Set a default such as
ethereum. - Run the query and check that rows are returned.
- Change the parameter to another supported chain and run it again.
The query still reads the same table and calculates the same metric. Only the selected chain changes.
Common errors
Parameter substitution happens before SQL execution, so syntax matters.
- A text parameter usually needs quotes:
='{{blockchain}}'. - Do not add quotes around a numeric expression when the SQL expects a number.
- Use the exact parameter name.
{{Blockchain}}and{{blockchain}}may be treated as different names. - Check that the parameter value matches the table’s values. A chain may be stored as
ethereum, notEthereum. - A parameter cannot fix a wrong table or column. Confirm that the selected table contains the field you filter.
- Test unusual values, such as a chain with no recent trades. An empty result may be valid rather than an SQL failure.
For dashboards, choose safe defaults and clear labels. A viewer should understand whether blockchain means a network name, a chain ID, or a Dune-specific identifier. If several charts use the same parameter, use the same name and meaning across all queries.
Exercise
You have a query with WHERE blockchain = 'ethereum'. What change lets a Dune user choose the chain without editing the query?
Show answer
Replace the fixed value with WHERE blockchain = '{{blockchain}}', then create a text parameter named blockchain and give it a default value.