1587. Bank Account Summary II
Problem Statement
Report the name and balance of users whose balance is greater than 10000. The balance of an account is calculated as the sum of the amounts of all transactions involving that account. Return the result table in any order.
Table Schema
Table: Users
+-------------+---------+
| account | int |
| name | varchar |
+-------------+---------+
account is the primary key.
Table: Transactions
+----------------+------+
| trans_id | int |
| account | int |
| amount | int |
| transacted_on | date |
+----------------+------+
trans_id is the primary key.
Examples
Example 1
Input Table:
Users table:
+---------+-------+
| account | name |
+---------+-------+
| 900001 | Alice |
| 900002 | Bob |
+---------+-------+
Transactions table:
+----------+---------+--------+---------------+
| trans_id | account | amount | transacted_on |
+----------+---------+--------+---------------+
| 1 | 900001 | 7000 | 2020-08-01 |
| 2 | 900001 | 7000 | 2020-09-01 |
| 3 | 900002 | 1000 | 2020-01-01 |
+----------+---------+--------+---------------+
Expected Output:
+-------+---------+
| name | balance |
+-------+---------+
| Alice | 14000 |
+-------+---------+
SQL Solution
SELECT u.name, SUM(t.amount) AS balance
FROM Users u
JOIN Transactions t ON u.account = t.account
GROUP BY u.account, u.name
HAVING SUM(t.amount) > 10000;
Problem Info
DifficultyEASY
Topics
joingroup-byhaving
Reference Links