1587. Bank Account Summary II 👇👇👇
Write an SQL query to report the name and balance of users with a balance higher than 10000
. The balance of an account is equal to the sum of the amounts of all transactions involving that account.
Return the result table in any order.
The query result format is in the following example.
Explanation:
Alice's balance is (7000 + 7000 - 3000) = 11000.
Bob's balance is 1000.
Charlie's balance is (6000 + 6000 - 4000) = 8000.
SQL Solution :-
# Write your MySQL query statement below
# select *
select name,sum(amount) as balance
from users inner join transactions
on users.account = transactions.account
group by name
having balance > 10000;
Full-Explanation :-
Appoach:
- We need to find the name and balance of the tables.
- balance is sum of amount
Query:
- firstly, selecting all columns (select * )
- We apply users inner join transactions satisfying users.account = transactions.account
select *
from users inner join transactions
on users.account = transactions.account
We get :-
3. From this table we group by name then aggregation (sum) is perfomed on amount as balance
4. after aggregation we use having clause to get the name and balance greater than 10000
In this way we get the clear output.
Thank you for reading this solution.
Happy Learning :)
Comments
Post a Comment