Because the join clause (your where statement)
is saying
"For each row meeting this criteria, do the Select from above".
So it runs for all clients and their invoices AND all clients and their payments.
To see that, rerun your query without the group by. Just do the join and see how many rows you get back.
If what you are looking for is a table with a client and their SUM invoice amounts and SUM payment amounts you should do the SUM./ Group By FIRST and then you can join those.
--what you might be looking for
with ci as (
select
c.id clientid,
c.name,
sum(i.quantity*i.unitprice) invoicedAmt
from
clients c,
invoices i
where c.id = i.clientid
group by c.id,c.name
),
cp as (
select
c.id clientid,
c.name,
sum(p.payment) paidAmt
from
clients c left outer join payments p on c.id=p.clientid
group by c.id, c.name
)
select
c.id,
c.name,
ci.invoicedAmt,
cp.paidAmt
from
clients c
LEFT OUTER JOIN ci ON c.id=ci.clientid
LEFT OUTER JOIN cp ON c.id=cp.clientid;
Gives
Result Set 6
ID NAME INVOICEDAMT PAIDAMT
1 client1 325 305
2 client2 1300 1280
4 client4 100 -
3 client3 120 110
NOTE: I shortpaid all the payments by 10$ so you can see the values from each tables.
NOTE: a,b,c are not great aliases. Try and use ones that clearly identify the source table (clients c, invoices i, payments p ...)
NOTE: used LEFT OUTER join because maybe a client has an invoice and no payment or no payments or invoices.
LiveSQL Link to see the different queries
https://livesql.oracle.com/ords/livesql/s/cpb02dav5l5wyjxbtll3sg290