Intermediate⏱ 30 min9 steps

Join Two Decoded Event Tables: Your First ClickHouse JOIN

Combine swap events with pool-creation events to label every Uniswap swap with its token pair. A gentle first JOIN, with the mistakes to avoid and how to keep it fast.

Wevr Guides ·

Single-table queries answer a lot of questions, but on-chain data is spread across many tables. Swap events know which pool was used but not which tokens it holds. Pool-creation events know which tokens each pool holds but nothing about trading. To answer "which token pairs traded the most this week?", you need both — and that means a JOIN.

This tutorial builds your first JOIN step by step, using two decoded Uniswap v2-style tables. You should be comfortable with SELECT, WHERE and GROUP BY before starting. Open the Wevr query editor and follow along.

Meet the two tables

Pool creation events. In our first SQL lesson we used uniswap_evt_paircreated, which records a PairCreated event every time the factory deploys a new pool. Each row tells you the two token addresses (token0 and token1) and the new pool's address (pair).

Swap events. Every pool emits a Swap event whenever someone trades through it. The decoded swap table records the pool's address (the contract that emitted the event), the amounts in and out, and the timestamp.

The shared column is the pool address: pair in the creation table and the emitting contract address in the swap table. That is our join key. Check the exact table and column names in the tables catalog before you run anything.

Step 1: count swaps per pool

Start with one table. Count the past week's swaps per pool:

SELECT contract_address AS pool,
       count() AS swaps
FROM uniswap_v2_evt_swap
WHERE block_timestamp >= now() - INTERVAL 7 DAY
GROUP BY pool
ORDER BY swaps DESC
LIMIT 20;

Useful, but the pools are just addresses. Nobody can read that list.

Step 2: join the token addresses

Now join each pool to its creation event to get its tokens:

SELECT s.pool,
       p.token0,
       p.token1,
       s.swaps
FROM (
  SELECT contract_address AS pool, count() AS swaps
  FROM uniswap_v2_evt_swap
  WHERE block_timestamp >= now() - INTERVAL 7 DAY
  GROUP BY pool
) AS s
INNER JOIN uniswap_evt_paircreated AS p
  ON s.pool = p.pair
ORDER BY s.swaps DESC
LIMIT 20;

Read it from the inside out. The subquery s produces one row per pool with its swap count. The INNER JOIN matches each of those rows to the creation event with the same pool address, bringing in token0 and token1. Every output row now says which two tokens the pool trades.

Why aggregate before joining

Notice that we counted swaps before joining. The alternative — joining every swap event to the creation table and then counting — produces the same answer but processes vastly more rows, because every individual swap would be matched.

The rule of thumb: aggregate the big table first, then join the small result. In ClickHouse, it also helps to put the smaller table on the right-hand side of the join, because the engine builds an in-memory lookup from the right side.

Step 3: make it readable

Token addresses are still hard to read. If the catalog has a token metadata table with symbols and decimals, add a second join for each token:

SELECT s.swaps, t0.symbol AS token0, t1.symbol AS token1, s.pool
FROM ( ...same subquery as before... ) AS s
INNER JOIN uniswap_evt_paircreated AS p ON s.pool = p.pair
LEFT JOIN tokens AS t0 ON p.token0 = t0.address
LEFT JOIN tokens AS t1 ON p.token1 = t1.address
ORDER BY s.swaps DESC;

LEFT JOIN keeps the row even if a token is missing from the metadata table — common for new or obscure tokens — and fills the symbol with an empty value instead of dropping the pool.

INNER vs LEFT, in one sentence

An INNER JOIN keeps only rows that match on both sides; a LEFT JOIN keeps every row from the left side and fills gaps with empty values. Use INNER when a missing match means the row is meaningless, and LEFT when you want to keep everything and see what is missing.

Troubleshooting

The result has far more rows than expected. Your join key is not unique on one side, so rows multiply. Check that each pool appears once in the creation table — it should — and that you did not join on the wrong column.

Zero rows come back. Address formats differ. Compare lowercase to lowercase, and check whether one table stores addresses with a different prefix or casing.

The query is slow. Add a time filter to the swap table, aggregate before joining, and keep the smaller table on the right.

Some pools have no tokens. They may come from a different factory than the creation table covers. That is a real finding, not an error — those pools were deployed elsewhere.

A second example: label transfers with exchange names

The same pattern works for labelling. Suppose you have a list of known exchange addresses in a lookup table with columns address and exchange. To see which exchanges received the most USDC this week, aggregate transfers by recipient first, then join:

SELECT l.exchange, sum(t.usdc) AS usdc_in
FROM (
  SELECT "to" AS recipient, sum(value) / 1e6 AS usdc
  FROM erc20_evt_transfer
  WHERE contract_address = lower('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48')
    AND block_timestamp >= now() - INTERVAL 7 DAY
  GROUP BY recipient
) AS t
INNER JOIN exchange_labels AS l ON t.recipient = l.address
GROUP BY l.exchange
ORDER BY usdc_in DESC;

Aggregate the big table, join the small lookup, then aggregate again by the label. It is the same shape as the swap query, applied to a different question.

Where to go next

The same pattern — aggregate one table, join it to a lookup table on a shared key — answers a huge range of questions: label transfers with exchange names, attach protocol names to contract addresses, or match bridge deposits on one chain to withdrawals on another. Save this query as a template. Your next JOIN will take five minutes.