|
1 | 1 | <h3 align="left"> 1587. Bank Account Summary II</h3>
|
2 |
| -<div class="sql-schema-wrapper__3VBi"><a class="sql-schema-link__3cEg">SQL Schema<svg viewBox="0 0 24 24" width="1em" height="1em" class="icon__1Md2"><path fill-rule="evenodd" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"></path></svg></a></div> |
| 2 | +<div class="content__u3I1 question-content__JfgR"><div class="sql-schema-wrapper__3VBi"><a class="sql-schema-link__3cEg">SQL Schema<svg viewBox="0 0 24 24" width="1em" height="1em" class="icon__1Md2"><path fill-rule="evenodd" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"></path></svg></a></div><div><p>Table: <code>Users</code></p> |
| 3 | + |
| 4 | +<pre>+--------------+---------+ |
| 5 | +| Column Name | Type | |
| 6 | ++--------------+---------+ |
| 7 | +| account | int | |
| 8 | +| name | varchar | |
| 9 | ++--------------+---------+ |
| 10 | +account is the primary key for this table. |
| 11 | +Each row of this table contains the account number of each user in the bank. |
| 12 | +There will be no two users having the same name in the table. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | + |
| 17 | +<p>Table: <code>Transactions</code></p> |
| 18 | + |
| 19 | +<pre>+---------------+---------+ |
| 20 | +| Column Name | Type | |
| 21 | ++---------------+---------+ |
| 22 | +| trans_id | int | |
| 23 | +| account | int | |
| 24 | +| amount | int | |
| 25 | +| transacted_on | date | |
| 26 | ++---------------+---------+ |
| 27 | +trans_id is the primary key for this table. |
| 28 | +Each row of this table contains all changes made to all accounts. |
| 29 | +amount is positive if the user received money and negative if they transferred money. |
| 30 | +All accounts start with a balance of 0. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | + |
| 35 | +<p>Write an SQL query to report the name and balance of users with a balance higher than <code>10000</code>. The balance of an account is equal to the sum of the amounts of all transactions involving that account.</p> |
| 36 | + |
| 37 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 38 | + |
| 39 | +<p>The query result format is in the following example.</p> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Example 1:</strong></p> |
| 43 | + |
| 44 | +<pre><strong>Input:</strong> |
| 45 | +Users table: |
| 46 | ++------------+--------------+ |
| 47 | +| account | name | |
| 48 | ++------------+--------------+ |
| 49 | +| 900001 | Alice | |
| 50 | +| 900002 | Bob | |
| 51 | +| 900003 | Charlie | |
| 52 | ++------------+--------------+ |
| 53 | +Transactions table: |
| 54 | ++------------+------------+------------+---------------+ |
| 55 | +| trans_id | account | amount | transacted_on | |
| 56 | ++------------+------------+------------+---------------+ |
| 57 | +| 1 | 900001 | 7000 | 2020-08-01 | |
| 58 | +| 2 | 900001 | 7000 | 2020-09-01 | |
| 59 | +| 3 | 900001 | -3000 | 2020-09-02 | |
| 60 | +| 4 | 900002 | 1000 | 2020-09-12 | |
| 61 | +| 5 | 900003 | 6000 | 2020-08-07 | |
| 62 | +| 6 | 900003 | 6000 | 2020-09-07 | |
| 63 | +| 7 | 900003 | -4000 | 2020-09-11 | |
| 64 | ++------------+------------+------------+---------------+ |
| 65 | +<strong>Output:</strong> |
| 66 | ++------------+------------+ |
| 67 | +| name | balance | |
| 68 | ++------------+------------+ |
| 69 | +| Alice | 11000 | |
| 70 | ++------------+------------+ |
| 71 | +<strong>Explanation:</strong> |
| 72 | +Alice's balance is (7000 + 7000 - 3000) = 11000. |
| 73 | +Bob's balance is 1000. |
| 74 | +Charlie's balance is (6000 + 6000 - 4000) = 8000. |
| 75 | +</pre> |
| 76 | +</div></div> |
0 commit comments