with cte as( select u.name,(select sum(t.amount) from transactions t group by t.account having t.account=u.account) as balance from users u ) select * from cte where balance > 10000; I have learnt to use cte from your videos and now i able to apply my logic.
I have solved the same problem like this WITH cte AS (SELECT account, SUM(amount) AS balance FROM Transactions GROUP BY account), cte2 AS (SELECT * FROM cte WHERE balance > 10000) SELECT name, balance FROM cte2 LEFT JOIN Users USING (account) The code is big, but isn't it better to break down big table into small and then join them. We can have faster query execution like that
Great catch, Madhur. Yes, since in the question there was no repetition in the name, GROUP BY name also works, but ideally it should be GROUP BY account number.
with cte as(
select u.name,(select sum(t.amount) from transactions t group by t.account having t.account=u.account)
as balance from users u
)
select * from cte where balance > 10000;
I have learnt to use cte from your videos and now i able to apply my logic.
I have solved the same problem like this
WITH cte AS
(SELECT account, SUM(amount) AS balance
FROM Transactions
GROUP BY account),
cte2 AS
(SELECT *
FROM cte
WHERE balance > 10000)
SELECT name, balance
FROM cte2 LEFT JOIN Users
USING (account)
The code is big, but isn't it better to break down big table into small and then join them. We can have faster query execution like that
I feel that group by should be done using account and not name as account is a primary key and name can be duplicated.
Great catch, Madhur. Yes, since in the question there was no repetition in the name, GROUP BY name also works, but ideally it should be GROUP BY account number.
But we are not returning account column or any agregate function of it so how can we group by account
33