Sub topic 3
DuneSQL basics: types, addresses and date handling
This chapter assumes you can write basic SQL (SELECT, WHERE, GROUP BY, JOIN). It also assumes you know what an address, an ERC-20 token and a block timestamp are. You should have run a query in the Dune editor. Three things trip up people who know SQL but are new to Dune: blockchain-specific types, address literals, and date filters that decide both cost and correctness.
What DuneSQL is
DuneSQL is Dune's query dialect. It runs on the Trino engine, so most Trino syntax and functions work. If you know PostgreSQL, expect some differences:
- Intervals are written
INTERVAL '7' DAY, with the number in quotes and the unit outside. - Casts use
CAST(x AS type). The::shorthand is not available. - Double quotes mark identifiers. Single quotes mark strings.
Dune adds blockchain-specific types on top of Trino: varbinary is used for addresses and hashes, and uint256 and int256 hold on-chain integers.
The types you meet most often
| Type | Typical use | Example column |
|---|---|---|
| `varbinary` | Addresses, transaction hashes, raw bytes | `contract_address`, `tx_hash`, `"to"` |
| `uint256` | Unsigned integers up to 2^256 - 1, such as token amounts | `value` in `erc20_ethereum.evt_Transfer` |
| `double` | Approximate decimal numbers | `amount_usd` |
| `date` | Calendar day, no time | `block_date`, `evt_block_date` |
| `timestamp` | Moment in time, in UTC | `evt_block_time` |
| `varchar` | Text | `project`, `blockchain` |
You can check the type of any expression with typeof:
SELECT
typeof(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) AS address_literal,
typeof(1) AS whole_number,
typeof(1e6) AS scientific,
typeof(1.5) AS fixed_point,
typeof(current_date) AS todayThe address literal is varbinary, 1 is an integer, 1e6 is a double and 1.5 is a decimal. typeof is a quick way to find out why a comparison or a join fails.
Addresses are bytes, not strings
An Ethereum address is 20 bytes. Dune stores it as varbinary, and the editor shows it as a hex string. A bare hexadecimal literal that starts with 0x and has no quotes is a varbinary value:
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48Three consequences follow.
- Quotes change the type.
'0xA0b8...'is avarchar. Comparing it with avarbinarycolumn raises a type-mismatch error in Trino. This is the most common first error. - Case does not matter. Bytes have no upper or lower case. The mixed-case checksum form and the all-lowercase form are the same value. You don't need
lower()on address columns. If you calllower()on one, the query fails or does the wrong thing. - Length matters. A hash or address with a missing or extra byte compares as unequal without raising an error. A
WHEREclause that silently returns zero rows often has a mistyped literal.
To convert text you hold as a string, for example one pasted into a parameter, use from_hex. It expects the hex digits without the 0x prefix:
SELECT from_hex('A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') AS usdc_addressGoing the other way, CAST(addr AS varchar) gives the 0x-prefixed hex string. This is useful when you build links or labels.
Reserved words as column names
The decoded Transfer table has columns named from and to. Both are SQL keywords, so quote them:
SELECT
"to" AS receiver,
COUNT(*) AS transfers,
SUM(CAST(value AS double) / 1e6) AS usdc_received
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND evt_block_date >= current_date - INTERVAL '7' DAY
GROUP BY 1
ORDER BY 3 DESC
LIMIT 10This returns the ten addresses that received the most USDC over the last seven days. The receiver column is varbinary, so the editor shows it as hex.
Large integers and token decimals
Smart contracts have no decimal numbers. A token amount is a whole number of the token's smallest unit. USDC uses 6 decimals, so a stored value of 2500000 means 2.5 USDC. Most other tokens, including WETH, use 18 decimals. Your query has to divide by 10^decimals.
The value column is uint256. Values reach 2^256, so they don't fit into a 64-bit integer, and engines limit arithmetic on them. The usual approach is to cast to double before dividing or summing:
SUM(CAST(value AS double) / 1e6) AS usdc_transferredA double carries about 15 to 16 significant digits. That is plenty for a chart of daily volume. It is not enough for exact balances of 18-decimal tokens when an audit requires the last wei. Keep that limit in mind when you reconcile numbers later in the tutorial.
Dates and timestamps
Dune stores time in UTC. Decoded tables carry two time columns:
evt_block_timeis atimestampthat holds the exact moment of the block.evt_block_dateis adatethat holds the calendar day.
Curated tables such as dex.trades use block_time and block_date in the same way. Raw tables use similar names.
The date column is the partition column, so filtering on it lets the engine skip whole days of data. A filter on evt_block_time alone also works for correctness. It is slower, because the engine reads more data before it can discard rows. For cost and speed, always include the date column in the WHERE clause, as the dex.trades and Transfer queries in this tutorial do. The next chapters return to this when they cover query efficiency.
Literals and relative dates
SELECT
DATE '2026-01-15' AS fixed_day,
TIMESTAMP '2026-01-15 13:30:00' AS fixed_moment,
current_date AS today,
current_date - INTERVAL '30' DAY AS thirty_days_ago,
date_add('day', -30, current_date) AS same_thingUse DATE '...' for a date and TIMESTAMP '...' for a moment. A plain string such as '2026-01-15' is a varchar and might not compare against a date column the way you expect. Use an explicit literal.
Bucketing time
date_trunc rounds a time down to the start of a unit. Use it to group events by day, week or month:
SELECT
date_trunc('week', block_date) AS week_start,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = 'ethereum'
AND block_date >= DATE '2026-01-01'
AND block_date < DATE '2026-04-01'
GROUP BY 1
ORDER BY 1Weeks begin on Monday. The first and last buckets can be partial weeks when your range does not line up with Mondays. A partial bucket looks like a sudden drop on a chart even though nothing happened on-chain.
Choosing range boundaries
Prefer half-open ranges: >= start and < end. They never double count a boundary row, and they stay correct when the column is a timestamp. With a timestamp, <= DATE '2026-03-31' would drop most of March 31, because the date converts to midnight at the start of that day.
Rolling windows such as current_date - INTERVAL '30' DAY change every time the query runs. Results differ from day to day. They also include a partial current day, so the most recent bar is usually lower than its neighbours. Either exclude today with < current_date, or label the last bar as incomplete.
Differences between dates
To measure elapsed time, use date_diff(unit, earlier, later):
SELECT date_diff('day', DATE '2026-01-01', DATE '2026-03-01') AS days_betweenThis returns 59. The order of arguments is earlier first, so swapping them gives a negative number.
Common errors in this chapter
| Symptom | Likely cause | Fix |
|---|---|---|
| Type mismatch comparing a column to an address | Address written in quotes (`varchar`) | Remove the quotes, or use `from_hex` |
| Syntax error near `from` or `to` | Reserved word used as a column name | Write `"from"` and `"to"` |
| Amounts are a million or a trillion times too large | Division by decimals missing or wrong | Divide by `1e6`, `1e18` and so on |
| Query is slow or expensive | Filter only on the time column or no time filter | Add a filter on the date column |
| Last chart bar looks low | Current day is incomplete | Use `< current_date` or mark the bar |
| Zero rows returned | Mistyped address, wrong chain table or wrong blockchain value | Check the literal and run `typeof` |
Exercise
A teammate writes WHERE contract_address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' and gets a type error. What is wrong, and how do you fix it two different ways?
Show answer
The quotes make the literal a varchar, while contractaddress is a varbinary. Fix 1: remove the quotes so the literal is a varbinary. Fix 2: keep the text and convert it with fromhex('A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'), which takes the hex digits without the 0x prefix.
Exercise
A query sums CAST(value AS double) / 1e6 for a token that actually has 18 decimals. By what factor is the result wrong, and in which direction?
Show answer
The result is too large by a factor of 10^12 (1e18 / 1e6), because the sum should have been divided by 1e18.
Exercise
Write the date filter for the calendar month of February 2026 on a table with a date column named block_date, using a half-open range.
Show answer
blockdate >= DATE '2026-02-01' AND blockdate < DATE '2026-03-01'