Ethereum SQL Tutorial: Blocks, Tokens and Traces (2026)
Vincent Charles
November 23, 2026 ยท 19 min read

To query Ethereum data with SQL, you write standard SQL against four kinds of tables: blocks, transactions, event logs (decoded into tables like ERC-20 transfers) and traces. On Dune they are ethereum.blocks, ethereum.transactions, erc20_ethereum.evt_Transfer and ethereum.traces. The SQL is the one you already know. What trips people up is the data model: wei, failed transactions that still sit in the table, nonces, raw token amounts, the zero address and internal calls that never show up as transactions.
This Ethereum SQL tutorial walks through seven Ethereum SQL queries, one layer of the chain at a time, from block base fees to internal ETH flows. Each one is a real exercise from the WizardCamp Ethereum path, with the actual question, the reference solution and the real result on a documented sample of mainnet data. Every query also hides one Ethereum specific trap, and that trap is the part worth remembering.
TL;DR:
- Four layers, four tables: blocks (
ethereum.blocks), transactions (ethereum.transactions), events (erc20_ethereum.evt_Transferand other decoded tables) and traces (ethereum.traces).- The 7 queries: base fee change per block, ETH sent per sender, nonce gaps, ERC-20 volume by token, mints and burns, internal ETH receivers, and direct vs internal ETH per transaction.
- The traps: failed transactions keep their
value, most transactions move zero ETH, token amounts are raw integers, mints come from the zero address, and delegatecalls report ETH they never move.- All 7 count toward a certificate: they belong to the parts of the Ethereum path required for WizardCamp's Ethereum Blockchain Foundations certificate.
- Our data: 26 of 330 live data roles (8%) on the Unchain Data job board name Ethereum or the EVM, almost twice as many as name Solana (14), and 16 of those 26 also ask for SQL.
How do you query Ethereum data with SQL?
The easiest way to query Ethereum data with SQL is Dune: you write SQL in the browser against Ethereum tables that are already indexed and decoded, and the free tier is enough to learn. BigQuery, Allium and Flipside also offer SQL access, but Dune adds a large library of public Ethereum queries you can read and fork.
Dune's SQL engine, DuneSQL, is a fork of Trino. If you have done our Dune SQL tutorial, you know the syntax and the first ten patterns. This guide goes one level deeper into how Ethereum itself stores data, with seven exercises that tutorial did not cover.
Which Dune tables hold Ethereum data?
Dune holds Ethereum data in four tables: ethereum.blocks, ethereum.transactions, erc20_ethereum.evt_Transfer and ethereum.traces, with one row per block, transaction, ERC-20 Transfer event and internal call respectively. Those four cover base fees, ETH value, token transfers and traces. For cleaned, cross-chain transfers, use the Spellbook table tokens.transfers.
| Layer | Dune table | One row per | Use it for |
|---|---|---|---|
| Blocks | ethereum.blocks | Block | Base fee, gas used, block time |
| Transactions | ethereum.transactions | Transaction | Sender, recipient, ETH value, nonce, gas, success flag |
| Events | erc20_ethereum.evt_Transfer | ERC-20 Transfer event | Token transfers, mints, burns, raw amounts |
| Traces | ethereum.traces | Call inside a transaction | Internal ETH transfers, contract creations, call types |
| Cleaned transfers | tokens.transfers | Transfer, already decoded and adjusted | Cross-chain transfers, amounts already divided by decimals |
Logs are the raw events contracts emit. Dune decodes the most common ones into readable tables, and erc20_ethereum.evt_Transfer is the one you will use most. Traces are the full record of every call made while a transaction executes, including calls from one contract to another that the transactions table never shows.
How to use this tutorial
Run each query in WizardCamp, where it is graded instantly against the reference answer. WizardCamp runs on small, documented samples of real Dune tables in your browser, mostly mainnet blocks 18,994,254 to 19,008,578, on January 13 to 15, 2024. The numbers below are real, but they describe the sample, not the whole chain.
A few naming differences matter when you move a query to Dune:
- Reserved words are quoted on Dune. The sandbox uses
from_addressandto_address. On Dune, the columns are"from"and"to", in double quotes. - Hash vs tx_hash. The sandbox renames
hashtotx_hash. On Dune,ethereum.transactionsuseshash, andethereum.tracesusestx_hash. - Block numbers. The sandbox calls it
block_numberin the blocks table. Onethereum.blocksthe column isnumber. - Big integers. On Dune,
valueis auint256. The sandbox usesHUGEINT, so where you seeCAST(... AS HUGEINT)below, the Dune equivalent is a cast touint256. - Addresses and hashes are binary. On Dune they are
varbinaryand display as0x.... The sandbox wraps them inlower(hex(...))so results compare as plain lowercase text.
Query 1: Track the base fee from block to block
From WizardCamp's Ethereum Blockchain Fundamentals module ("Base Fee Change Between Consecutive Blocks", Ethereum path).
SQL concept: LAG in a CTE, then filter in the outer query. Dune table: ethereum.blocks.
Return each block's base fee in gwei, rounded to 2 decimals, and the change from the previous block. Exclude the first block, which has nothing to compare to.
WITH base AS (
SELECT block_number, ROUND(base_fee_per_gas / 1e9, 2) AS base_fee_gwei FROM blocks
),
lagged AS (
SELECT block_number, base_fee_gwei,
LAG(base_fee_gwei) OVER (ORDER BY block_number) AS prev_gwei
FROM base
)
SELECT block_number, base_fee_gwei, ROUND(base_fee_gwei - prev_gwei, 2) AS change_gwei
FROM lagged
WHERE prev_gwei IS NOT NULL
ORDER BY block_number ASCThe sample is 12 consecutive blocks, 19,000,000 to 19,000,011. The query returns 11 rows. An excerpt:
| block_number | base_fee_gwei | change_gwei |
|---|---|---|
| 19000001 | 19.16 | -0.91 |
| 19000002 | 18.53 | -0.63 |
| 19000005 | 17.92 | 0.12 |
| 19000008 | 17.88 | -0.58 |
| 19000009 | 19.43 | 1.55 |
The lesson: the base fee is set by the protocol, block by block. Since EIP-1559 (the London upgrade), it rises when the previous block was more than half full and falls when it was less, by at most 12.5% per block. In this sample it drifts between 17.80 and 19.43 gwei, and the biggest move is +1.55 gwei at block 19,000,009. Two SQL details matter: you cannot filter on a window function in the same SELECT, so the CTE is required, and negative changes are real values, not errors to clean out.
Query 2: Sum the ETH each sender actually transferred
From WizardCamp's Ethereum Blockchain Fundamentals module ("ETH Value Transferred Per Sender", Ethereum path).
SQL concept: filter before you aggregate, and cast big numbers. Dune table: ethereum.transactions.
For each sender, compute the total ETH value transferred in successful transactions only.
SELECT
lower(hex(from_address)) AS from_address,
SUM(CAST(value AS HUGEINT)) AS total_value_wei
FROM transactions
WHERE success = TRUE
GROUP BY from_address
ORDER BY total_value_wei DESCThe query returns 28 senders. The top one, 0x3bf3345b..., sent 10000000000000000000 wei, which is 10 ETH, because 1 ETH is 10^18 wei. Four senders tie at 0.2 ETH. And 15 of the 28 senders, more than half, transferred exactly 0 ETH.
The lesson: two things. First, a failed transaction stays in the table with its value filled in, but it moved nothing. In the sample, one sender sent three transactions worth 4.23, 4.23 and 1.89 ETH, and all three failed, so that sender must not appear at all. Second, most Ethereum transactions are contract calls, like swaps, approvals and token transfers, which carry 0 ETH. ETH value is not activity: a sender with zero value can still be your most active user. On Dune, value is a uint256, so keep the math in wei and convert at the end.
Query 3: Detect nonce gaps per sender
From WizardCamp's Ethereum Blockchain Fundamentals module ("Nonce Gap Detection", Ethereum path).
SQL concept: MIN, MAX, COUNT and HAVING as a gaps check. Dune table: ethereum.transactions.
Each transaction from an address carries a nonce that goes up by 1 with every transaction. Find senders whose nonce sequence has gaps.
SELECT
lower(hex(from_address)) AS from_address,
MAX(nonce) - MIN(nonce) + 1 AS expected_count,
COUNT(*) AS actual_count,
(MAX(nonce) - MIN(nonce) + 1) - COUNT(*) AS gap_count
FROM transactions
GROUP BY from_address
HAVING (MAX(nonce) - MIN(nonce) + 1) - COUNT(*) > 0
ORDER BY gap_count DESC, from_address ASC| from_address | expected_count | actual_count | gap_count |
|---|---|---|---|
| 357c6fd2cee77ba5de49e0bb9d49444781a8f0cc | 45 | 8 | 37 |
| ced92fa7f0797cbc851b48140ae218a0b0d41ce0 | 24 | 8 | 16 |
| d8a52d79d4d13083856adda57a32a57d0f932c26 | 15 | 8 | 7 |
The lesson: on Ethereum, a sender's nonces are strictly sequential, so the chain itself has no gaps. A gap in your table means your data is incomplete. Here the three senders were sampled in two windows, and their real activity in between is simply not in the sample. That makes this query a data quality check you can run on any pipeline or extract. Also note what is not in the result: a fresh wallet with nonces 3 to 6 has no gap, because the check starts where your data starts, not at 0.
Query 4: Total ERC-20 transfer volume by token
From WizardCamp's Token Analytics module ("ERC-20 Transfer Volume by Token", Ethereum path).
SQL concept: GROUP BY with COUNT and SUM, plus a tiebreaker. Dune table: erc20_ethereum.evt_Transfer.
For each token contract, compute the number of transfers and the total raw transfer value, before decimal adjustment.
SELECT
lower(hex(contract_address)) AS contract_address,
COUNT(*) AS total_transfers,
SUM(value) AS total_raw_value
FROM erc20_transfers
GROUP BY contract_address
ORDER BY total_transfers DESC, contract_address ASC| contract_address | token | total_transfers | total_raw_value |
|---|---|---|---|
| 2260fac5e5542a773aa44fbcfedf7c193bc2c599 | WBTC | 30 | 5930281637 |
| a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 | USDC | 30 | 5118448267808 |
| c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 | WETH | 30 | 96034968933041298204 |
The lesson: a token is its contract address, and its amounts are raw integers. The WETH total looks like the biggest by far, but it is about 96.03 WETH, because WETH has 18 decimals. The USDC total is about 5.12 million USDC (6 decimals), and the WBTC total about 59.30 WBTC (8 decimals). Raw totals are not comparable between tokens until you join token metadata and divide by 10^decimals, which the Dune SQL tutorial covers in its decimals query. All three tokens tie at 30 transfers, so the second ORDER BY column is the only thing that makes the order deterministic.
Query 5: Find ERC-20 mints, burns and net supply change
From WizardCamp's Token Analytics module ("ERC-20 Mints, Burns, and Net Supply Change", Ethereum path).
SQL concept: conditional aggregation with SUM(CASE WHEN ...) and HAVING. Dune table: erc20_ethereum.evt_Transfer.
Mints are transfers from the zero address, and burns are transfers to it. For each token with a mint or a burn, return the totals and the net supply change.
SELECT
lower(hex(contract_address)) AS contract_address,
SUM(CASE WHEN from_address = unhex('0000000000000000000000000000000000000000') THEN value ELSE CAST(0 AS HUGEINT) END) AS total_minted,
SUM(CASE WHEN to_address = unhex('0000000000000000000000000000000000000000') THEN value ELSE CAST(0 AS HUGEINT) END) AS total_burned,
SUM(CASE WHEN from_address = unhex('0000000000000000000000000000000000000000') THEN value ELSE CAST(0 AS HUGEINT) END) -
SUM(CASE WHEN to_address = unhex('0000000000000000000000000000000000000000') THEN value ELSE CAST(0 AS HUGEINT) END) AS net_supply_change
FROM erc20_transfers
GROUP BY contract_address
HAVING SUM(CASE WHEN from_address = unhex('0000000000000000000000000000000000000000') THEN 1 ELSE 0 END) > 0
OR SUM(CASE WHEN to_address = unhex('0000000000000000000000000000000000000000') THEN 1 ELSE 0 END) > 0
ORDER BY net_supply_change DESCThe sample covers blocks 18,994,254 to 18,994,528 on January 13, 2024. The query returns two tokens. DAI minted about 200,196,796.63 DAI and burned about 65,896.41, for a net supply change of about +200.13 million. USDC minted about 55,067.27 and burned about 28,566.98, for a net change of about +26,500.29.
The lesson: nobody holds the zero address, so it is how ERC-20 events usually encode supply changes. That mint from or burn to the zero address pattern is the usual ERC-20 convention, not a hard rule, and some tokens burn to another dead address. Three details make this query correct. WBTC had only ordinary transfers in the sample, so HAVING removes it. DAI's total is dominated by a real double mint: two identical rows of 100 million DAI each to the same recipient in block 18,994,293. They are two separate events, so both count, and a "cleanup" that deduplicates rows would undercount supply by 100 million. And the totals exceed what a 64-bit integer can hold, so the sum needs a wide integer type.
Query 6: Find who received ETH through internal calls
From WizardCamp's Traces & Internal Calls module ("Internal ETH Receivers", Ethereum path).
SQL concept: filter on several columns, then aggregate. Dune table: ethereum.traces.
For each recipient, sum the ETH received through successful call traces with a positive value.
SELECT
lower(hex(to_address)) AS to_address,
SUM(CAST(value AS HUGEINT)) AS total_received_wei
FROM traces
WHERE call_type = 'call'
AND value > 0
AND success = TRUE
GROUP BY to_address
ORDER BY total_received_wei DESCThe sample is 70 traces from block 19,000,000. The query returns six recipients. The top two are WETH (0xc02aaa39...), with 10.65 ETH, and the Uniswap V2 Router (0x7a250d56...), with 10.2 ETH.
The lesson: ETH moves inside transactions in ways the transactions table never shows, and the filter is what makes the number right. Staticcalls carry a NULL value, and value > 0 drops them. Delegatecalls report the value of the call that contains them without moving any ETH. In this sample, two addresses received only delegatecall value (0.2 ETH and 0.05 ETH), and a query without call_type = 'call' would wrongly count them as receivers. The traces total includes the top-level call, not only nested internal calls. That is why a 10 ETH transaction that forwards the same 10 ETH can show 20 ETH in traces.
Query 7: Compare direct ETH with internal ETH per transaction
From WizardCamp's Traces & Internal Calls module ("Transaction ETH vs Internal Call ETH", Ethereum path).
SQL concept: aggregate in a CTE, LEFT JOIN and COALESCE. Dune tables: ethereum.transactions and ethereum.traces.
For each successful transaction, return the ETH in its value field and the total ETH moved by its successful call traces, with 0 when there are none.
WITH internal AS (
SELECT
tx_hash,
SUM(CAST(value AS HUGEINT)) AS internal_value_wei
FROM traces
WHERE call_type = 'call' AND success = TRUE
GROUP BY tx_hash
)
SELECT
lower(hex(t.tx_hash)) AS tx_hash,
t.value AS direct_value_wei,
COALESCE(i.internal_value_wei, 0) AS internal_value_wei
FROM transactions t
LEFT JOIN internal i ON i.tx_hash = t.tx_hash
WHERE t.success = TRUE
ORDER BY t.tx_hash ASCOn Dune, this query is not runnable as written, because ethereum.transactions uses hash rather than tx_hash, while ethereum.traces keeps tx_hash. Cast HUGEINT to uint256, drop the lower(hex(...)) wrappers, and add a block filter so you do not scan the whole chain:
WITH internal AS (
SELECT
tx_hash,
SUM(CAST(value AS uint256)) AS internal_value_wei
FROM ethereum.traces
WHERE call_type = 'call' AND success = TRUE
AND block_number = 19000000
GROUP BY tx_hash
)
SELECT
t.hash AS tx_hash,
t.value AS direct_value_wei,
COALESCE(i.internal_value_wei, 0) AS internal_value_wei
FROM ethereum.transactions t
LEFT JOIN internal i ON i.tx_hash = t.hash
WHERE t.success = TRUE
AND t.block_number = 19000000
ORDER BY t.hash ASCThe query returns 39 rows: the 40 transactions of block 19,000,000 minus one failed transaction carrying 4.23 ETH. Only 8 of the 39 have any internal ETH movement. In five of those, the internal total is exactly double the direct value. Transaction 0x0c4bc163... sent 10 ETH directly, and its call traces moved 20 ETH.
The lesson: direct and internal ETH answer different questions, and both are correct. The traces total includes the top-level call, not only nested internal calls. That is why a 10 ETH transaction that forwards the same 10 ETH can show 20 ETH in traces. Never add the two columns together to get "ETH moved". That is why blockchain queries double-count when you mix layers. Aggregating traces first and then using a LEFT JOIN keeps every successful transaction, including those with no matching call traces, and COALESCE turns their missing total into 0 instead of NULL.
The 7 Ethereum SQL patterns at a glance
| # | Query | SQL concept | Ethereum lesson |
|---|---|---|---|
| 1 | Base fee change per block | LAG in a CTE | The protocol moves the base fee by at most 12.5% per block |
| 2 | ETH sent per sender | WHERE before SUM, cast | Failed transactions keep their value, and most transactions carry 0 ETH |
| 3 | Nonce gaps | MIN, MAX, COUNT, HAVING | Nonces never skip on chain, so a gap means missing data |
| 4 | ERC-20 volume by token | GROUP BY, tiebreaker | Amounts are raw, so divide by decimals before comparing tokens |
| 5 | Mints and burns | SUM(CASE WHEN ...), HAVING | The zero address encodes supply, and duplicate looking events can be real |
| 6 | Internal ETH receivers | Multi-column filter | Delegatecalls and staticcalls do not move ETH |
| 7 | Direct vs internal ETH | CTE, LEFT JOIN, COALESCE | The traces total includes the top-level call, so never add the two |
Is Ethereum SQL worth learning for a crypto data job?
Yes. As of November 22, 2026, the Unchain Data job board lists 330 live data roles. 199 of them (60%) mention SQL, and 26 (8%) name Ethereum or the EVM, almost twice as many as name Solana (14). 16 of those 26 Ethereum roles also ask for SQL.
Those Ethereum roles include teams at Chainalysis, Coinbase, Dune, Everstake, Merkle Science and OP Labs. One more detail is worth knowing: none of the 53 live roles in our Data Analyst category name Ethereum or the EVM, while 38 of them (72%) mention SQL. In this snapshot, Ethereum by name shows up in engineer, scientist, research and head of data roles. For an analyst role, Ethereum SQL is something you prove in the take home test, not a keyword you need on your CV. You can browse the live roles on the Web3 data analyst job board and the live Ethereum data jobs page, and practice the interview side with our crypto data analyst interview questions.
What to learn after these 7 queries
After these seven Ethereum SQL queries, go back to the first ten Dune SQL patterns if you skipped them, then learn holders and users, protocol data such as DEX and lending tables, and Solana. When you start applying, add a portfolio and interview practice.
- The first ten patterns. If you skipped it, the Dune SQL tutorial covers transaction types, gas costs, EIP-1559 fee splits, rolling averages, decimals, DEX volume and stablecoin market share.
- Holders and users. Net balances, first transaction per wallet and new vs returning users, in our best SQL practice sites guide.
- Protocol data. The rest of the Ethereum path covers NFTs, DeFi lending and DEXs, MEV, bridges, L2 fees, real world assets, oracles and stablecoins, plus Uniswap and Aave protocol packs. Our guide on how to build a Dune dashboard for a DeFi protocol walks through that next step.
- Solana. The same layers work differently on Solana. Our Solana SQL tutorial and the WizardCamp Solana path cover the accounts model, fees in lamports and DEX trades.
- The career side. How to Become a Crypto Data Analyst covers portfolios and where to apply, and Best Blockchain Analytics Courses compares your learning options.
All seven exercises sit in parts 1, 2 and 5 of the Ethereum path, which are required for WizardCamp's Ethereum Blockchain Foundations certificate, so working through this tutorial is real progress toward it. WizardCamp has 163 graded exercises on real Ethereum and Solana data, and Learn Onchain SQL: Inside WizardCamp shows how the course works. For a crypto data analyst role, it is the best place to practice Ethereum SQL, because every exercise runs on real chain data and the grader shows you exactly which rows are wrong.
Frequently Asked Questions
Can you query Ethereum with SQL?
Yes. Dune, Google BigQuery, Allium, Flipside and other platforms index Ethereum into SQL tables, so you can query blocks, transactions, token transfers and traces with standard SQL. Dune is the easiest place to start because it has a free tier, decoded tables and a large library of public queries you can read and fork.
What are the main Dune tables for Ethereum?
Start with ethereum.blocks for base fees and block data, ethereum.transactions for senders, values, nonces and gas, erc20_ethereum.evt_Transfer for token transfers, mints and burns, and ethereum.traces for internal calls and internal ETH transfers. For decimals, join tokens.erc20; for cleaned cross-chain transfers, use tokens.transfers.
What is the difference between transactions and traces on Ethereum?
A transaction is what a wallet signs and sends: one sender, one recipient and one value. Traces record every call made while that transaction runs, including calls between contracts. Internal ETH transfers, like a router sending ETH to WETH, only appear in traces.
Why is my ETH or token amount so large in SQL?
Because Ethereum stores amounts as raw integers. ETH values are in wei (1 ETH is 10^18 wei), and each ERC-20 token has its own decimals, for example 6 for USDC and 18 for WETH and DAI. Divide by 10 to the power of the decimals at the end of your query, and keep sums in a wide integer type until then.
How long does it take to learn Ethereum SQL?
If you know basic SQL, you can write the seven queries in this tutorial in one or two sessions. If you are new to SQL, start with WizardCamp's free SQL Foundations module and the free first exercises of the Ethereum path, then move on to transactions, tokens and traces.
Key Takeaways
- Ethereum SQL is standard SQL on four layers: blocks, transactions, decoded events and traces. The traps are in the data model.
- Filter failed transactions before you sum
value, and remember that most transactions carry 0 ETH. - Nonces never skip on chain, so a nonce gap in your table is a data quality problem, not user behavior.
- Token amounts are raw. Join on the contract address and divide by decimals before comparing tokens.
- Use traces for internal ETH, keep only successful
calltraces with a positive value, and never add direct and internal ETH together. - Data teams name Ethereum more than Solana. As of November 22, 2026, 26 of 330 live data roles on the Unchain Data job board name Ethereum or the EVM, versus 14 for Solana.
Looking for blockchain data engineering instead? Check out our blockchain data engineering guide.
Related
Best Blockchain Analytics Courses in 2026: Chainalysis, TRM and WizardCamp Compared
Solana SQL Tutorial: How to Query Solana Data on Dune (2026)
Best SQL Practice Sites in 2026: LeetCode, DataLemur, StrataScratch and WizardCamp
Dune SQL Tutorial for Beginners: 10 Queries on Real Onchain Data
Crypto Data Analyst Interview Questions: 7 Onchain SQL Problems

- Founder of Unchain Data
- Former data lead at Morpho Labs and Binance
- Builds Dune dashboards and data pipelines across Ethereum, Solana and Sui
- Advises VC funds and DeFi protocols on data strategy
- Featured on BBC for blockchain data research
Ready to practice what you just read?
WizardCamp has interactive SQL exercises on real blockchain data. Write queries, get instant feedback, earn certificates.
Explore WizardCamp