当前位置:网站首页>[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
边栏推荐
猜你喜欢

Caesar

Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years

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

Office365 - how to use stream app to watch offline files at any time

Huawei modelarts training alexnet model
![[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 (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

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

Download xshell and xftp

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

QT -- 1. QT connection database
随机推荐
Kickback -- find the first palindrome character in a group of characters
【入门】输入n个整数,输出其中最小的k个
Latex formula code
Principle and process of embossing
[R language] two /n data merge functions
Discussion on several research hotspots of cvpr2022
The database is locked. Is there a solution
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
Aardio - Shadow Gradient Text
php laravel微信支付
SQL number injection and character injection
Caesar
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
PWN攻防世界int_overflow
下载Xshell和Xftp
STM32 uses esp01s to go to the cloud, mqtt FX debugging
getInputStream() has already been called for this request
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
Teach you how to apply for domestic trademark online step by step