<p>The <code>tx_out</code> table contains both <strong>spent</strong> and <strong>unspent</strong> tx outputs. In order to calculate your balance, you need to sum only the <strong>unspent</strong> outputs.</p>
<p>We can check which outputs are unspent by joining on the <code>tx_in</code> table. If there is no associated entry for a particular <code>tx_out</code> row in the <code>tx_in</code> table, this output has not been spent.</p>
<p>Here are the associated queries for calculating both address and stake address balances:</p>
<pre class="lang-sql prettyprint-override"><code>-- address balance
select sum(tx_out.value)
from tx_out
left join tx_in on tx_in.tx_out_id = tx_out.tx_id and tx_in.tx_out_index = tx_out.index
where address = '&lt;your-address-here&gt;'
  and tx_in.id is null

-- stake address balance
select sum(tx_out.value)
from stake_address
inner join tx_out on tx_out.stake_address_id = stake_address.id
left join tx_in on tx_in.tx_out_id = tx_out.tx_id and tx_in.tx_out_index = tx_out.index
where stake_address.view = '&lt;your-stake-address-here&gt;'
  and tx_in.id is null
</code></pre>
<p>Note that these balances will not include your staking reward balance.</p>

CC BY-SA 4.0
2024-03-17T08:40:28.267Z
This is a concise and correct answer.
I ran the queries and they work just as expected.
Much appreciated, thank you!
RelevantData
CC BY-SA 4.0
2024-03-16T17:53:37.243Z
2024-03-16T17:53:37.243Z
adjuric
<p>I want to write the ADA balance of my wallet to a console window using sql queries against the db-sync database.</p>
<p>How can I use sql statements to obtain the ADA balance of a given address by querying db-sync via postgresql? I tried querying the sum of the [value] of the utxo's of my wallet but seem to get inaccurate results acording to what my Eternl wallet says my balance is. Somehow I am misunderstanding what I should be summing etc.</p>
<p>There is no straightforward query for &quot;wallet balance&quot; in the interesting queries in the db-sync git repo.</p>

<cardano-db-sync>
Querying db-sync for ADA Balance
# Querying db-sync for ADA Balance

(Asked by: username_0 on 2024-03-15)

I want to write the ADA balance of my wallet to a console window using sql queries against the db-sync database.

How can I use sql statements to obtain the ADA balance of a given address by querying db-sync via postgresql? I tried querying the sum of the [value] of the utxo's of my wallet but seem to get inaccurate results acording to what my Eternl wallet says my balance is. Somehow I am misunderstanding what I should be summing etc.

There is no straightforward query for "wallet balance" in the interesting queries in the db-sync git repo.

**Tags:** cardano-db-sync


---

## 1 answers

### Answer by username_1 on 2024-03-16. Score: 1 | Accepted answer

The `tx_out` table contains both **spent** and **unspent** tx outputs. In order to calculate your balance, you need to sum only the **unspent** outputs.

We can check which outputs are unspent by joining on the `tx_in` table. If there is no associated entry for a particular `tx_out` row in the `tx_in` table, this output has not been spent.

Here are the associated queries for calculating both address and stake address balances:

```sql
-- address balance
select sum(tx_out.value)
from tx_out
left join tx_in on tx_in.tx_out_id = tx_out.tx_id and tx_in.tx_out_index = tx_out.index
where address = '<your-address-here>'
  and tx_in.id is null

-- stake address balance
select sum(tx_out.value)
from stake_address
inner join tx_out on tx_out.stake_address_id = stake_address.id
left join tx_in on tx_in.tx_out_id = tx_out.tx_id and tx_in.tx_out_index = tx_out.index
where stake_address.view = '<your-stake-address-here>'
  and tx_in.id is null

```

Note that these balances will not include your staking reward balance.

> **Comments:**
> **username_0**: This is a concise and correct answer. I ran the queries and they work just as expected. Much appreciated, thank you!  

---

