当前位置:网站首页>【力扣10天SQL入门】Day10 控制流
【力扣10天SQL入门】Day10 控制流
2022-07-01 07:55:00 【ly甲烷】
182.查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
| [email protected] |
+---------+
答案解析
按邮箱分组,数量大于1的输出
SELECT A.Email
FROM (
SELECT Email, count(Id) id
FROM Person
GROUP BY Email) A
WHERE A.id > 1
解法2, HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集。
SELECT Email
FROM Person
GROUP BY Email
HAVING count(Id) > 1
1050.合作过至少三次的演员和导演
ActorDirector 表:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| actor_id | int |
| director_id | int |
| timestamp | int |
+-------------+---------+
timestamp 是这张表的主键.
写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)
ActorDirector 表:
+-------------+-------------+-------------+
| actor_id | director_id | timestamp |
+-------------+-------------+-------------+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 1 | 2 | 4 |
| 2 | 1 | 5 |
| 2 | 1 | 6 |
+-------------+-------------+-------------+
Result 表:
+-------------+-------------+
| actor_id | director_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
答案解析
SELECT actor_id, director_id
FROM ActorDirector
GROUP BY actor_id, director_id
HAVING COUNT(1) >= 3
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 |
+------------+------------+
答案解析
SELECT A.name NAME, sum(B.amount) BALANCE
FROM Users A LEFT JOIN Transactions B ON A.account = B.account
GROUP BY A.account
HAVING balance > 10000
1084.销售分析III
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
Product_id是该表的主键。
该表的每一行显示每个产品的名称和价格。
Table: Sales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
+------ ------+---------+
这个表没有主键,它可以有重复的行。product_id 是 Product 表的外键。
该表的每一行包含关于一个销售的一些信息。
编写一个SQL查询,报告2019年春季才售出的产品。即 仅在2019-01-01至2019-03-31(含)之间出售 的商品。
以 任意顺序 返回结果表。
输入:
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
输出:
+-------------+--------------+
| product_id | product_name |
+-------------+--------------+
| 1 | S8 |
+-------------+--------------+
id为1的产品仅在2019年春季销售。
id为2的产品在2019年春季销售,但也在2019年春季之后销售。
id 3的产品在2019年春季之后销售。
答案解析
SELECT A.product_id, A.product_name
FROM Product A LEFT JOIN Sales B ON A.product_id = B.product_id
GROUP BY A.product_id
HAVING SUM(B.sale_date NOT BETWEEN '2019-01-01' AND '2019-03-31') = 0
总结
HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集。
SQL入门成功!撒花
边栏推荐
- How to troubleshoot SharePoint online map network drive failure?
- 关系数据库如何工作
- Gru of RNN
- [kv260] generate chip temperature curve with xadc
- ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
- Insufficient executors to build thread pool
- The triode is a great invention
- 2022制冷与空调设备运行操作国家题库模拟考试平台操作
- Aardio - 阴影渐变文字
- Aardio - 自己构造的getIconHandle的方法
猜你喜欢

Principle and process of embossing

Atguigu---- scaffold --02- use scaffold (2)

【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)

Latex formula code

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证

组件的自定义事件②

组件的自定义事件①

Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)

Apple account password auto fill

web254
随机推荐
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
How relational databases work
Minecraft 1.16.5 module development (51) tile entity
Huawei modelarts training alexnet model
redisson使用全解——redisson官方文档+注释(上篇)
[MySQL learning notes 28] storage function
How to get a SharePoint online site created using the office365 group template
IMDB practice of emotion classification (simplernn, LSTM, Gru)
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
Long way to go with technology
LM08丨网格系列之网格反转(精)
2022年茶艺师(中级)复训题库及答案
[MySQL learning notes27] stored procedure
php laravel微信支付
力扣每日一题-第31天-1502.判断能否形成等差数列
如何让两融交易更极速
论文学习——水文时间序列相似性查询的分析与研究
base64
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis