Sub topic 3
DuneSQL basics: types, addresses and date handling
This chapter assumes basic SQL: SELECT, WHERE, GROUP BY, and JOIN. It also assumes that you know what blockchain addresses, blocks, transactions, and ERC-20 tokens are. The examples use DuneSQL against Dune's decoded and curated tables.
Read the types before writing the query
A Dune table contains columns with different data types. The type determines which operations are valid and how values are displayed.
| Type | Typical blockchain use | Example |
|---|---|---|
| `varchar` | Text such as protocol names | `'ethereum'` |
| `bigint` | Integer counts or raw token amounts | `1234567` |
| `double` | Approximate numeric values such as USD prices | `12.5` |
| `decimal` | Exact numeric calculations | `CAST(value AS DECIMAL(38, 0))` |
| `date` | Calendar dates used for partition filters | `DATE '2026-11-01'` |
| `timestamp` | A precise event time | `evt_block_time` |
| `varbinary` | Addresses and transaction hashes | `0xA0b8...6eB48` |
Blockchain data often stores quantities as integers. An ERC-20 token's value column usually contains the smallest unit, not the human-readable amount. Convert it only after checking the token's decimals.
For example, USDC uses six decimal places. A raw value of 1250000 represents 1.25 USDC:
SELECT
CAST(value AS DOUBLE) / 1e6 AS usdc_amount
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
LIMIT 10CAST changes a value to another type. Use it deliberately. Converting a large integer to double makes arithmetic convenient, but floating-point values are approximate. For balances or accounting-grade totals, use an appropriate decimal type instead.
Work with addresses
Dune commonly represents Ethereum addresses as varbinary. Use a hexadecimal literal such as 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 when comparing an address column. Do not quote that literal as text unless the column itself is varchar.
Addresses are case-insensitive at the protocol level. A checksum address uses mixed case to help detect typing errors, but the underlying address is the same. Prefer the checksum form copied from a trusted source.
SELECT
"from",
"to",
value
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND "to" = 0x000000000000000000000000000000000000dEaD
LIMIT 20Column names such as "from" can conflict with SQL keywords. Double quotes make the column reference explicit.
Filter and group dates
Dune tables often provide both an event timestamp and a date partition. Use the date column for efficient filtering when it exists.
evt_block_timecontains the event's timestamp.evt_block_datecontains its calendar date.block_dateis commonly available in curated tables such asdex.trades.current_datereturns the current calendar date.
Subtract an interval to select a recent period:
SELECT
date_trunc('day', evt_block_time) AS day,
SUM(CAST(value AS DOUBLE) / 1e6) AS usdc_transferred
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND evt_block_date >= current_date - INTERVAL '7' DAY
GROUP BY 1
ORDER BY 1date_trunc('day', ...) removes the time portion for grouping. The result is a timestamp at the start of each day. If you need a calendar date, cast it with CAST(... AS DATE).
How a query handles time
Exercise
Exercise
A token has 18 decimals and its raw transfer value is stored in value. Which expression converts it to human-readable units, and which column should you use for a seven-day partition filter?
Show answer
Use CAST(value AS DOUBLE) / 1e18, and filter the table's date partition column, such as evtblockdate, with current_date - INTERVAL '7' DAY.