大家好,我是空空star,本篇带大家了解一道简单的力扣sql练习题。
文章目录
- 前言
- 一、题目:1587. 银行账户概要 II
- 二、解题
- 1.正确示范①
- 提交SQL
- 运行结果
- 2.正确示范②
- 提交SQL
- 运行结果
- 3.正确示范③
- 提交SQL
- 运行结果
- 4.正确示范④
- 提交SQL
- 运行结果
前言
一、题目:1587. 银行账户概要 II
表: Users
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| account | int |
| name | varchar |
+--------------+---------+
account 是该表的主键.
表中的每一行包含银行里中每一个用户的账号.
表: Transactions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trans_id | int |
| account | int |
| amount | int |
| transacted_on | date |
+---------------+---------+
trans_id 是该表主键.
该表的每一行包含了所有账户的交易改变情况.
如果用户收到了钱, 那么金额是正的; 如果用户转了钱, 那么金额是负的.
所有账户的起始余额为 0.
写一个 SQL, 报告余额高于 10000 的所有用户的名字和余额. 账户的余额等于包含该账户的所有交易的总和.
返回结果表单没有顺序要求.
查询结果格式如下例所示.
Users table:
+------------+--------------+
| account | name |
+------------+--------------+
| 900001 | Alice |
| 900002 | Bob |
| 900003 | Charlie |
+------------+--------------+
Transactions table:
+------------+------------+------------+---------------+
| trans_id | account | amount | transacted_on |
+------------+------------+------------+---------------+
| 1 | 900001 | 7000 | 2020-08-01 |
| 2 | 900001 | 7000 | 2020-09-01 |
| 3 | 900001 | -3000 | 2020-09-02 |
| 4 | 900002 | 1000 | 2020-09-12 |
| 5 | 900003 | 6000 | 2020-08-07 |
| 6 | 900003 | 6000 | 2020-09-07 |
| 7 | 900003 | -4000 | 2020-09-11 |
+------------+------------+------------+---------------+
Result table:
+------------+------------+
| name | balance |
+------------+------------+
| Alice | 11000 |
+------------+------------+
Alice 的余额为(7000 + 7000 - 3000) = 11000.
Bob 的余额为1000.
Charlie 的余额为(6000 + 6000 - 4000) = 8000.
二、解题
1.正确示范①
提交SQL
select name,sum(amount) balance
from Users u1
join Transactions u2
on u1.account=u2.account
group by u1.account
having sum(amount)>10000;
运行结果
2.正确示范②
提交SQL
select name,balance from(
select name,sum(amount) balance
from Users u1
join Transactions u2
on u1.account=u2.account
group by u1.account
) u where balance>10000;
运行结果
3.正确示范③
提交SQL
select name,balance from(
select name,sum(amount) balance
from Users u1,Transactions u2
where u1.account=u2.account
group by u1.account
) u where balance>10000;
运行结果
4.正确示范④
提交SQL
select name,sum(amount) balance
from Users u1,Transactions u2
where u1.account=u2.account
group by u1.account
having sum(amount)>10000;