Question: SQL syntax question about UPDATE. It would be much easier to ask the question by giving example below:

```
**Payments Table**

ID  user_id  payment_due   payment_made 
1      3        10.0           5.0
1      3        10.0           10.0
1      9        20.0           20.0

**Balance Table**

ID  user_id    current_balance 
1      3            ???
2      9            ???

```

Let say I would like to update the current balance for specific user. What would be the correct and efficient SQL syntax to add all payment dues and subtract it from all payment made for a specific user?

In this case, current\_balance for user '3' is 5.0 and for user '9' is 0.0

Answer: 
How about:

```
select
   ID,
   user_id,
   (sum(payment_due) - sum(payment_made)) current_balance
from
   PaymentsTable

group by
   ID,
   user_id

```
You need to use a subquery. Something like this should work:

```
UPDATE
    balance_table b
SET
    current_balance = (SELECT
            SUM(payment_due - payment_made)
        FROM
            payments_table p
        WHERE
            p.user_id = b.user_id)

```