当前位置:网站首页>[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
边栏推荐
- php laravel微信支付
- Download xshell and xftp
- 【入门】取近似值
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Sorting out tcp/udp communication problems
- LM08丨网格系列之网格反转(精)
- The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
- Atguigu---- scaffold --02- use scaffold (2)
- [MySQL learning notes 25] SQL statement optimization
- Office365 - how to use stream app to watch offline files at any time
猜你喜欢

Minecraft 1.16.5 module development (51) tile entity

Aardio - 阴影渐变文字

Huawei modelarts training alexnet model

How outlook puts together messages with the same discussion

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

Set up file server Minio for quick use

Redisson uses the full solution - redisson official document + comments (Part 2)

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

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

Array: question brushing record
随机推荐
Two expressions of string
How relational databases work
Redisson uses the full solution - redisson official documents + comments (Part 2)
AUTOSAR learning record (1) – ECUM_ Init
What information does the supplier need to know about Audi EDI project?
Access报表实现小计功能
Minecraft 1.16.5 module development (51) tile entity
Saving db4i depth camera pictures with MATLAB
Basic knowledge of MATLAB
【mysql学习笔记27】存储过程
源代码加密的意义和措施
QT -- 1. QT connection database
软件测试方法和技术 - 基础知识概括
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
2022 tea master (intermediate) recurrent training question bank and answers
2022 test questions and mock examinations for main principals of hazardous chemicals business units
Implementation and encapsulation of go universal dynamic retry mechanism
力扣每日一题-第31天-202.快乐数
Eigen matrix operation Library
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed