当前位置:网站首页>【力扣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入门成功!撒花
边栏推荐
- The triode is a great invention
- 浏览器本地存储
- 【mysql学习笔记27】存储过程
- Eigen矩阵运算库快速上手
- redisson使用全解——redisson官方文档+注释(上篇)
- Browser local storage
- How to use layui to display the data in the database in the form of tables
- 2022危险化学品经营单位主要负责人试题及模拟考试
- Teach you how to apply for domestic trademark online step by step
- Vhost kick & call principle
猜你喜欢

奥迪AUDI EDI 项目中供应商需要了解哪些信息?

How to use layui to display the data in the database in the form of tables

PWN攻防世界int_overflow

The database is locked. Is there a solution

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money

下载Xshell和Xftp

MATLAB之基础知识

2022年茶艺师(中级)复训题库及答案

LM08丨网格系列之网格反转(精)

base64
随机推荐
软件测试方法和技术 - 基础知识概括
Long way to go with technology
Custom events of components ①
Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
2022制冷与空调设备运行操作国家题库模拟考试平台操作
Gui Gui programming (XV) - use scale to control font size changes
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
Office365 - how to use stream app to watch offline files at any time
Source code analysis of open source API gateway APIs IX
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Php laraver Wechat payment
十大劵商如何开户?另外,手机开户安全么?
【mysql学习笔记27】存储过程
组件的自定义事件②
如何使用layui将数据库中的数据以表格的形式展现出来
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
Atguigu---- scaffold --02- use scaffold (2)
[kv260] generate chip temperature curve with xadc