当前位置:网站首页>【力扣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入门成功!撒花
边栏推荐
- Redisson uses the complete solution - redisson official documents + Notes (Part 1)
- Custom events of components ①
- Redisson uses the full solution - redisson official document + comments (Part 2)
- postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
- 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 full solution - redisson official documents + comments (Part 2)
- 2022年茶艺师(中级)复训题库及答案
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
- Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
- Eigen matrix operation Library
猜你喜欢

PWN攻防世界int_overflow

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

2022 mobile crane driver test exercises and online simulation test

Custom events of components ①

2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql

源代码加密的意义和措施

The Windows C disk is full

论文学习——水文时间序列相似性查询的分析与研究

How to use layui to display the data in the database in the form of tables
随机推荐
如何使用layui将数据库中的数据以表格的形式展现出来
【无标题】
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
She is the "HR of others" | ones character
Li Kou daily question - Day 32 -1822 Symbol of array element product
Android screen adaptation (using constraintlayout), kotlin array sorting
Microsoft stream - how to modify video subtitles
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
php laravel微信支付
【mysql学习笔记27】存储过程
5大组合拳,解决校园6大难题,护航教育信息化建设
The database is locked. Is there a solution
2022年流动式起重机司机考试练习题及在线模拟考试
[软件] phantomjs屏幕截图
Thesis learning -- Analysis and Research on similarity query of hydrological time series
Kickback -- find the first palindrome character in a group of characters
Oracle创建自增id
How to use layui to display the data in the database in the form of tables