Sub topic 6
Making queries reusable with parameters
This chapter assumes you can write basic DuneSQL (SELECT, WHERE, GROUP BY), that you know the dex.trades table from earlier chapters, and that you can run and save a query in the Dune editor. By the end you will be able to turn a hard-coded query into a parameterized one. You will also know which parameter type to pick and which mistakes to avoid.
Why parameters exist
A query that hard-codes 'ethereum' answers one question. If you want the same chart for Arbitrum, Base or Polygon, you could copy the query and edit one word. That produces many near-identical queries, and every fix has to be repeated in each copy.
A query parameter is a named placeholder inside the SQL text. Dune shows an input box for it above the editor. When the query runs, Dune replaces the placeholder with the value in the box. One saved query can then feed many charts and dashboards.
Parameters are useful when:
- The same logic applies to several chains, tokens, projects or time windows.
- Dashboard viewers should be able to change an input without editing SQL.
- You want one place to fix a bug.
The placeholder syntax
A parameter is written with double curly braces around its name: {{blockchain}}. Dune finds each {{name}} in the query text and creates an input for it. The name must be a simple identifier, so use lowercase letters, digits and underscores, such as blockchain or start_date.
Here is the DEX volume query from earlier, with the chain turned into a parameter.
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 DESCDune creates a text input named blockchain. Type ethereum into it and run the query. The engine sees WHERE blockchain = 'ethereum'. Type base and it sees WHERE blockchain = 'base'. The quotes around the placeholder are part of your SQL, which matters for the next section.
Substitution is text replacement
The most important thing to understand is that a parameter is substituted into the query text before the query is parsed. Dune does not bind it the way a prepared statement does. It pastes the value in.
That has two consequences.
First, the type of the input decides how the value is pasted, but your SQL decides whether the result is valid. With a text parameter, the value is inserted as typed, so you supply the quotes yourself. If you wrote WHERE blockchain = {{blockchain}} without quotes, the engine would see WHERE blockchain = ethereum. It would then read ethereum as a column name and fail.
Second, a parameter can change the structure of the query, not only a value. This is flexible, because a parameter can hold a column name or a fragment of SQL. It is also risky on a shared dashboard, because whoever edits the input controls part of the SQL text. The risk is bounded, since Dune queries are read-only, but a bad value can still produce confusing errors or expensive scans. Prefer list parameters (below) when you want to restrict the choices.
Parameter types
Dune offers several types. You pick the type from the settings next to the input after the placeholder appears.
| Type | What you enter | How it is pasted | Typical use |
|---|---|---|---|
| Text | Any string | As typed, no quotes added | Chain name, project, token symbol |
| Number | A numeric value | As a bare number | Minimum trade size, number of days |
| Date | A date and time picker | As a date-time string | Start and end of a time range |
| List | A choice from fixed items | As the chosen item's text | Chain picker with a fixed set of options |
Two details cause most confusion:
- A Date parameter is pasted as a string such as
2026-11-01 00:00:00. Wrap it in a cast so DuneSQL treats it as a timestamp:TIMESTAMP '{{start_date}}'. - A List parameter works like a Text parameter with a drop-down. The value is still pasted as text, so you still need quotes around it in the SQL if it stands for a string.
A reusable query with several parameters
Combining a list, a number and a date makes one query serve many dashboards. In the next query, min_usd is a Number parameter, blockchain is a List parameter and start_date is a Date parameter.
SELECT
block_date,
project,
SUM(amount_usd) AS volume_usd,
COUNT(*) AS trades
FROM dex.trades
WHERE blockchain = '{{blockchain}}'
AND block_date >= DATE(TIMESTAMP '{{start_date}}')
AND amount_usd >= {{min_usd}}
GROUP BY 1, 2
ORDER BY 1, 3 DESCPoints to notice:
DATE(TIMESTAMP '{{start_date}}')turns the picker value into adate.block_dateis a date column, so this comparison is type-correct and keeps the partition filter useful for pruning.{{min_usd}}has no quotes, because it is a number. Setting it to0includes all trades. Setting it to10000removes small trades and shows large-trade activity.- Every parameter needs a value before the query can run. An empty Number box produces
amount_usd >=with nothing after it, which is a syntax error.
Parameters must not defeat query efficiency
The earlier chapter on efficient queries explained that filtering on the partition column block_date lets the engine skip data it does not need. Parameters can break this if you are careless.
Compare these two filters:
-- Good: the engine can prune partitions using block_date
WHERE block_date >= DATE(TIMESTAMP '{{start_date}}')
-- Risky: wrapping the column in a function can stop pruning
WHERE date_trunc('month', block_date) >= DATE(TIMESTAMP '{{start_date}}')Keep the partition column bare on the left side of the comparison and put the parameter on the right. Also give date parameters a sensible default, such as a recent start date. If a viewer clears the box or sets a start date in 2015, the query scans the entire history and spends many credits. A narrow default protects both the viewer and your credit balance.
Using a parameter for the time window
Instead of a date picker, many dashboards use a Number parameter for "last N days". This is simpler for viewers, and the value is still pasted as a bare literal.
SELECT
block_date,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = '{{blockchain}}'
AND block_date >= current_date - INTERVAL '{{days}}' DAY
GROUP BY 1
ORDER BY 1Here {{days}} sits inside quotes because DuneSQL interval literals take the form INTERVAL '30' DAY. The parameter type is Text or Number, and the pasted result is INTERVAL '30' DAY. If you left the quotes out you would get INTERVAL 30 DAY, which DuneSQL rejects.
How parameters flow into dashboards
A parameter set in a query shows up in two places:
- On the query page, where you can change the value and re-run.
- On every dashboard that contains a visualization of that query.
On a dashboard, Dune collects parameters with the same name from the different queries and shows them as a single widget at the top. If three charts each use {{blockchain}}, the viewer changes the chain once and all three charts update. This works only when the names match exactly, so blockchain and chain would give two separate inputs. Choose a naming convention for your team and keep to it.
Each distinct combination of parameter values is a separate execution. Changing the chain on a dashboard runs the queries again with the new values, which uses credits for the viewer's session unless the result is already cached. Dashboards with many parameterized charts and wide default ranges are therefore more expensive than static ones.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a column such as `ethereum` does not exist | Text parameter used without quotes | Write `'{{blockchain}}'` |
| Syntax error near `>=` | Empty Number parameter | Enter a value or set a default |
| Type mismatch comparing `date` and `timestamp` | Date parameter pasted as a string and not cast | Use `DATE(TIMESTAMP '{{start_date}}')` |
| Query returns no rows | Value does not match stored data, such as `Ethereum` versus `ethereum` | Check exact values with `SELECT DISTINCT blockchain FROM dex.trades` |
| Query is slow or expensive | Very early start date or a function on the partition column | Set a narrow default, keep `block_date` bare |
| Two inputs appear for one idea | Parameter names differ between queries | Use identical names |
The "no rows" case deserves a habit. String comparisons in DuneSQL are case-sensitive, so an input of Base will not match base. Using a List parameter whose items are the exact stored values removes this whole class of error.
Design guidelines
- Name parameters for meaning, such as
start_dateandmin_usd, notp1. - Parameterize only what viewers genuinely need to change. Each extra input is another way to produce an empty or slow result.
- Set defaults that return quickly and show something meaningful.
- Prefer List parameters when the valid values are known.
- Document each parameter in the query description, including units. A
min_usdinput is clear, whileminis not.
Exercise
A colleague writes WHERE blockchain = {{blockchain}} and sets the Text parameter to base. The query fails. What is the cause, and what two changes would make the query both correct and safer to share on a dashboard?
Show answer
Dune pastes the value as plain text, so the engine sees WHERE blockchain = base and treats base as a column name. Fix it by quoting the placeholder: WHERE blockchain = '{{blockchain}}'. To make it safer, change the parameter to a List type whose items are the exact stored chain names, so viewers cannot type a misspelled or wrongly cased value that returns no rows.
Exercise
You add AND block_date >= {{start_date}} where start_date is a Date parameter. The query fails with a type or syntax error. Rewrite the filter so it runs and still allows partition pruning.
Show answer
Use AND block_date >= DATE(TIMESTAMP '{{start_date}}'). The Date parameter is pasted as a string like 2026-11-01 00:00:00, so it needs quotes and a cast to a timestamp. Wrapping it in DATE(...) gives a date that compares directly with the block_date column, which is left bare so the engine can skip irrelevant partitions.