You are given two tables: Users and Transactions.
The Users table contains account information with columns:
account(int): Primary key, unique account numbername(varchar): User name (unique)
The Transactions table contains all account transactions with columns:
trans_id(int): Primary key, unique transaction IDaccount(int): Account numberamount(int): Transaction amount (positive for deposits, negative for withdrawals)transacted_on(date): Transaction date
Write a SQL query to find the name and balance of users whose balance is higher than 10000. The balance is calculated as the sum of all transaction amounts for each account. All accounts start with a balance of 0.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
account
PK
|
int | Primary key, unique account number |
name
|
varchar | User name (unique) |
| Column Name | Type | Description |
|---|---|---|
trans_id
PK
|
int | Primary key, unique transaction ID |
account
|
int | Account number (foreign key) |
amount
|
int | Transaction amount (positive for deposits, negative for withdrawals) |
transacted_on
|
date | Transaction date |
Input & Output
| account | name |
|---|---|
| 900001 | Alice |
| 900002 | Bob |
| 900003 | Charlie |
| trans_id | account | amount | transacted_on |
|---|---|---|---|
| 1 | 900001 | 7000 | 2020-08-01 |
| 2 | 900001 | 9000 | 2020-08-02 |
| 3 | 900001 | -5000 | 2020-08-03 |
| 4 | 900002 | 1000 | 2020-09-01 |
| 5 | 900003 | 6000 | 2020-09-02 |
| 6 | 900003 | 6000 | 2020-09-03 |
| name | balance |
|---|---|
| Alice | 11000 |
| Charlie | 12000 |
Alice has transactions: 7000 + 9000 - 5000 = 11000 (> 10000, included). Bob has 1000 (≤ 10000, excluded). Charlie has 6000 + 6000 = 12000 (> 10000, included).
| account | name |
|---|---|
| 900001 | Alice |
| 900002 | Bob |
| trans_id | account | amount | transacted_on |
|---|---|---|---|
| 1 | 900001 | 5000 | 2020-08-01 |
| 2 | 900002 | 3000 | 2020-08-02 |
| name | balance |
|---|
Alice has balance 5000 and Bob has balance 3000. Both are ≤ 10000, so no users are returned.
| account | name |
|---|---|
| 900001 | Alice |
| trans_id | account | amount | transacted_on |
|---|---|---|---|
| 1 | 900001 | 15000 | 2020-08-01 |
| 2 | 900001 | -2000 | 2020-08-02 |
| 3 | 900001 | -1000 | 2020-08-03 |
| name | balance |
|---|---|
| Alice | 12000 |
Alice has multiple transactions: 15000 - 2000 - 1000 = 12000, which is greater than 10000, so she is included in the result.
Constraints
-
1 ≤ Users.account ≤ 10^4 -
1 ≤ Transactions.trans_id ≤ 10^5 -
1 ≤ name.length ≤ 20 -
-10^4 ≤ amount ≤ 10^4 -
amount ≠ 0