当前位置:网站首页>[force deduction 10 days SQL introduction] Day10 control flow
[force deduction 10 days SQL introduction] Day10 control flow
2022-07-01 07:59:00 【Ly methane】
182. Find duplicate email
Write a SQL Inquire about , lookup Person All duplicate email addresses in the table .
Example :
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
According to the above input , Your query should return the following results :
+---------+
| Email |
+---------+
| [email protected] |
+---------+
Analysis of the answer
Group by mailbox , The number is larger than 1 Output
SELECT A.Email
FROM (
SELECT Email, count(Id) id
FROM Person
GROUP BY Email) A
WHERE A.id > 1
solution 2, HAVING Sentences are usually associated with GROUP BY Statements are used in conjunction with , Used to filter by GROUP BY Statement returned recordset .
SELECT Email
FROM Person
GROUP BY Email
HAVING count(Id) > 1
1050. Actors and directors who have worked together at least three times
ActorDirector surface :
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| actor_id | int |
| director_id | int |
| timestamp | int |
+-------------+---------+
timestamp It's the primary key of this table .
Write a SQL Query statement to get actors and directors who have cooperated at least three times id Yes (actor_id, director_id)
ActorDirector surface :
+-------------+-------------+-------------+
| 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 surface :
+-------------+-------------+
| actor_id | director_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
Analysis of the answer
SELECT actor_id, director_id
FROM ActorDirector
GROUP BY actor_id, director_id
HAVING COUNT(1) >= 3
1587. Bank account summary II
surface : Users
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| account | int |
| name | varchar |
+--------------+---------+
account Is the primary key of the table . Each row in the table contains the account number of each user in the bank .
surface : Transactions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trans_id | int |
| account | int |
| amount | int |
| transacted_on | date |
+---------------+---------+
trans_id Is the primary key of the table . Each row of the table contains the transaction changes of all accounts .
If the user receives the money , Then the amount is positive ; If the user transfers money , Then the amount is negative .
The starting balance of all accounts is 0.
Write a SQL, The reported balance is higher than 10000 The names and balances of all users . The balance of the account is equal to the sum of all transactions including the account .
The returned result form has no sequence requirements .
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 |
+------------+------------+
Analysis of the answer
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. Sales analysis III
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
Product_id Is the primary key of the table .
Each row of the table shows the name and price of each product .
Table: Sales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
+------ ------+---------+
This table has no primary key , It can have duplicate lines .product_id yes Product Table foreign key .
Each row of the table contains some information about a sale .
Write a SQL Inquire about , The report 2019 Products sold only in the spring of . namely Only in 2019-01-01 to 2019-03-31( contain ) Sale between The goods .
With In any order Return result table .
Input :
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 |
+-----------+------------+----------+------------+----------+-------+
Output :
+-------------+--------------+
| product_id | product_name |
+-------------+--------------+
| 1 | S8 |
+-------------+--------------+
id by 1 Of products are only available in 2019 Spring sales .
id by 2 Our products are in 2019 Spring sales , But also 2019 Sales after spring 2004 .
id 3 Our products are in 2019 Sales after spring 2004 .
Analysis of the answer
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
summary
HAVING Sentences are usually associated with GROUP BY Statements are used in conjunction with , Used to filter by GROUP BY Statement returned recordset .
SQL Success in getting started ! And the flower
边栏推荐
- [kv260] generate chip temperature curve with xadc
- Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
- 力扣每日一题-第32天-1822.数组元素积的符号
- Implementation and encapsulation of go universal dynamic retry mechanism
- Source code analysis of open source API gateway APIs IX
- I bet on performance and won the CTO of the company. I want to build Devops platform!
- 【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
- Basic knowledge of MATLAB
- H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
- LSTM of RNN
猜你喜欢
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Gdip - hatchbrush pattern table

【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序

Teach you how to apply for domestic trademark online step by step

凸印的印刷原理及工艺介绍

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

【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion

Lm08 mesh series mesh inversion (fine)
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
随机推荐
力扣——求一组字符中的第一个回文字符
PWN攻防世界int_overflow
Instead of houses, another kind of capital in China is rising
Two expressions of string
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
AUTOSAR learning record (1) – ECUM_ Init
EDA开源仿真工具verilator入门6:调试实例
Gru of RNN
她就是那个「别人家的HR」|ONES 人物
web254
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
2022 test question bank and simulation test of tea master (primary) operation certificate
base64
【力扣10天SQL入门】Day9 控制流
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
What information does the supplier need to know about Audi EDI project?
[skill] create Bat quick open web page
Conscience Amway universal wheel SolidWorks model material website
PWN attack and defense world int_ overflow