# 一、查询
# 1. 可回收且低脂的产品【简单】
问题描述
表:Products | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| product_id | int | | |
| low_fats | enum | | |
| recyclable | enum | | |
+-------------+---------+ | |
product_id 是该表的主键(具有唯一值的列)。 | |
low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。 | |
recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。 | |
编写解决方案找出既是低脂又是可回收的产品编号。 | |
返回结果 无顺序要求 。 | |
返回结果格式如下例所示: | |
示例 1: | |
输入: | |
Products 表: | |
+-------------+----------+------------+ | |
| product_id | low_fats | recyclable | | |
+-------------+----------+------------+ | |
| 0 | Y | N | | |
| 1 | Y | Y | | |
| 2 | N | Y | | |
| 3 | Y | Y | | |
| 4 | N | N | | |
+-------------+----------+------------+ | |
输出: | |
+-------------+ | |
| product_id | | |
+-------------+ | |
| 1 | | |
| 3 | | |
+-------------+ | |
解释: | |
只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。 |
建表语句
Create table If Not Exists Products (product_id int, low_fats ENUM('Y', 'N'), recyclable ENUM('Y','N')); | |
Truncate table Products; | |
insert into Products (product_id, low_fats, recyclable) values ('0', 'Y', 'N'); | |
insert into Products (product_id, low_fats, recyclable) values ('1', 'Y', 'Y'); | |
insert into Products (product_id, low_fats, recyclable) values ('2', 'N', 'Y'); | |
insert into Products (product_id, low_fats, recyclable) values ('3', 'Y', 'Y'); | |
insert into Products (product_id, low_fats, recyclable) values ('4', 'N', 'N'); |
答案
select product_id from Products where low_fats = 'Y' and recyclable = 'Y'; |
# 2. 寻找用户推荐人【简单】
问题描述
表: Customer | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| id | int | | |
| name | varchar | | |
| referee_id | int | | |
+-------------+---------+ | |
在 SQL 中,id 是该表的主键列。 | |
该表的每一行表示一个客户的 id、姓名以及推荐他们的客户的 id。 | |
找出那些 没有被 id = 2 的客户 推荐 的客户的姓名。 | |
以 任意顺序 返回结果表。 | |
结果格式如下所示。 | |
示例 1: | |
输入: | |
Customer 表: | |
+----+------+------------+ | |
| id | name | referee_id | | |
+----+------+------------+ | |
| 1 | Will | null | | |
| 2 | Jane | null | | |
| 3 | Alex | 2 | | |
| 4 | Bill | null | | |
| 5 | Zack | 1 | | |
| 6 | Mark | 2 | | |
+----+------+------------+ | |
输出: | |
+------+ | |
| name | | |
+------+ | |
| Will | | |
| Jane | | |
| Bill | | |
| Zack | | |
+------+ |
建表语句
Create table If Not Exists Customer (id int, name varchar(25), referee_id int); | |
Truncate table Customer; | |
insert into Customer (id, name, referee_id) values ('1', 'Will', 'None'); | |
insert into Customer (id, name, referee_id) values ('2', 'Jane', 'None'); | |
insert into Customer (id, name, referee_id) values ('3', 'Alex', '2'); | |
insert into Customer (id, name, referee_id) values ('4', 'Bill', 'None'); | |
insert into Customer (id, name, referee_id) values ('5', 'Zack', '1'); | |
insert into Customer (id, name, referee_id) values ('6', 'Mark', '2'); |
答案
select name from customer where referee_id <> 2 or referee_id is null; |
# 3. 大的国家【简单】
问题描述
World 表: | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| name | varchar | | |
| continent | varchar | | |
| area | int | | |
| population | int | | |
| gdp | bigint | | |
+-------------+---------+ | |
name 是该表的主键(具有唯一值的列)。 | |
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。 | |
如果一个国家满足下述两个条件之一,则认为该国是 大国 : | |
面积至少为 300 万平方公里(即,3000000 km2),或者 | |
人口至少为 2500 万(即 25000000) | |
编写解决方案找出 大国 的国家名称、人口和面积。 | |
按 任意顺序 返回结果表。 | |
返回结果格式如下例所示。 | |
示例: | |
输入: | |
World 表: | |
+-------------+-----------+---------+------------+--------------+ | |
| name | continent | area | population | gdp | | |
+-------------+-----------+---------+------------+--------------+ | |
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | | |
| Albania | Europe | 28748 | 2831741 | 12960000000 | | |
| Algeria | Africa | 2381741 | 37100000 | 188681000000 | | |
| Andorra | Europe | 468 | 78115 | 3712000000 | | |
| Angola | Africa | 1246700 | 20609294 | 100990000000 | | |
+-------------+-----------+---------+------------+--------------+ | |
输出: | |
+-------------+------------+---------+ | |
| name | population | area | | |
+-------------+------------+---------+ | |
| Afghanistan | 25500100 | 652230 | | |
| Algeria | 37100000 | 2381741 | | |
+-------------+------------+---------+ |
建表语句
Create table If Not Exists World | |
( | |
name varchar(255), | |
continent varchar(255), | |
area int, | |
population int, | |
gdp bigint | |
); | |
Truncate table World; | |
insert into World (name, continent, area, population, gdp) | |
values ('Afghanistan', 'Asia', '652230', '25500100', '20343000000'); | |
insert into World (name, continent, area, population, gdp) | |
values ('Albania', 'Europe', '28748', '2831741', '12960000000'); | |
insert into World (name, continent, area, population, gdp) | |
values ('Algeria', 'Africa', '2381741', '37100000', '188681000000'); | |
insert into World (name, continent, area, population, gdp) | |
values ('Andorra', 'Europe', '468', '78115', '3712000000'); | |
insert into World (name, continent, area, population, gdp) | |
values ('Angola', 'Africa', '1246700', '20609294', '100990000000'); |
答案
select name, population, area | |
from World | |
where area >= 3000000 | |
or population >= 25000000; |
# 4. 文章浏览 Ⅰ【简单】
问题描述
Views 表: | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| article_id | int | | |
| author_id | int | | |
| viewer_id | int | | |
| view_date | date | | |
+---------------+---------+ | |
此表可能会存在重复行。(换句话说,在 SQL 中这个表没有主键) | |
此表的每一行都表示某人在某天浏览了某位作者的某篇文章。 | |
请注意,同一人的 author_id 和 viewer_id 是相同的。 | |
请查询出所有浏览过自己文章的作者 | |
结果按照 id 升序排列。 | |
查询结果的格式如下所示: | |
示例 1: | |
输入: | |
Views 表: | |
+------------+-----------+-----------+------------+ | |
| article_id | author_id | viewer_id | view_date | | |
+------------+-----------+-----------+------------+ | |
| 1 | 3 | 5 | 2019-08-01 | | |
| 1 | 3 | 6 | 2019-08-02 | | |
| 2 | 7 | 7 | 2019-08-01 | | |
| 2 | 7 | 6 | 2019-08-02 | | |
| 4 | 7 | 1 | 2019-07-22 | | |
| 3 | 4 | 4 | 2019-07-21 | | |
| 3 | 4 | 4 | 2019-07-21 | | |
+------------+-----------+-----------+------------+ | |
输出: | |
+------+ | |
| id | | |
+------+ | |
| 4 | | |
| 7 | | |
+------+ |
建表语句
Create table If Not Exists Views | |
( | |
article_id int, | |
author_id int, | |
viewer_id int, | |
view_date date | |
); | |
Truncate table Views; | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('1', '3', '5', '2019-08-01'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('1', '3', '6', '2019-08-02'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('2', '7', '7', '2019-08-01'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('2', '7', '6', '2019-08-02'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('4', '7', '1', '2019-07-22'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('3', '4', '4', '2019-07-21'); | |
insert into Views (article_id, author_id, viewer_id, view_date) | |
values ('3', '4', '4', '2019-07-21'); |
答案
select distinct author_id as id | |
from views | |
where author_id = viewer_id | |
order by author_id asc; |
# 5. 无效的推文【简单】
问题描述
表:Tweets | |
+----------------+---------+ | |
| Column Name | Type | | |
+----------------+---------+ | |
| tweet_id | int | | |
| content | varchar | | |
+----------------+---------+ | |
在 SQL 中,tweet_id 是这个表的主键。 | |
这个表包含某社交媒体 App 中所有的推文。 | |
查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15 时,该推文是无效的。 | |
以任意顺序返回结果表。 | |
查询结果格式如下所示: | |
示例 1: | |
输入: | |
Tweets 表: | |
+----------+----------------------------------+ | |
| tweet_id | content | | |
+----------+----------------------------------+ | |
| 1 | Vote for Biden | | |
| 2 | Let us make America great again! | | |
+----------+----------------------------------+ | |
输出: | |
+----------+ | |
| tweet_id | | |
+----------+ | |
| 2 | | |
+----------+ | |
解释: | |
推文 1 的长度 length = 14。该推文是有效的。 | |
推文 2 的长度 length = 32。该推文是无效的。 |
建表语句
Create table If Not Exists Tweets | |
( | |
tweet_id int, | |
content varchar(50) | |
); | |
Truncate table Tweets; | |
insert into Tweets (tweet_id, content) | |
values ('1', 'Vote for Biden'); | |
insert into Tweets (tweet_id, content) | |
values ('2', 'Let us make America great again!'); |
答案
select tweet_id | |
from Tweets | |
where length(content) > 15; |
# 二、连接
# 1. 使用唯一标识码替换员工 ID【简单】
问题描述
Employees 表: | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| id | int | | |
| name | varchar | | |
+---------------+---------+ | |
在 SQL 中,id 是这张表的主键。 | |
这张表的每一行分别代表了某公司其中一位员工的名字和 ID 。 | |
EmployeeUNI 表: | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| id | int | | |
| unique_id | int | | |
+---------------+---------+ | |
在 SQL 中,(id, unique_id) 是这张表的主键。 | |
这张表的每一行包含了该公司某位员工的 ID 和他的唯一标识码(unique ID)。 | |
展示每位用户的 唯一标识码(unique ID );如果某位员工没有唯一标识码,使用 null 填充即可。 | |
你可以以 任意 顺序返回结果表。 | |
返回结果的格式如下例所示。 | |
示例 1: | |
输入: | |
Employees 表: | |
+----+----------+ | |
| id | name | | |
+----+----------+ | |
| 1 | Alice | | |
| 7 | Bob | | |
| 11 | Meir | | |
| 90 | Winston | | |
| 3 | Jonathan | | |
+----+----------+ | |
EmployeeUNI 表: | |
+----+-----------+ | |
| id | unique_id | | |
+----+-----------+ | |
| 3 | 1 | | |
| 11 | 2 | | |
| 90 | 3 | | |
+----+-----------+ | |
输出: | |
+-----------+----------+ | |
| unique_id | name | | |
+-----------+----------+ | |
| null | Alice | | |
| null | Bob | | |
| 2 | Meir | | |
| 3 | Winston | | |
| 1 | Jonathan | | |
+-----------+----------+ | |
解释: | |
Alice and Bob 没有唯一标识码, 因此我们使用 null 替代。 | |
Meir 的唯一标识码是 2 。 | |
Winston 的唯一标识码是 3 。 | |
Jonathan 唯一标识码是 1 。 |
建表语句
Create table If Not Exists Employees (id int, name varchar(20)); | |
Create table If Not Exists EmployeeUNI (id int, unique_id int); | |
Truncate table Employees; | |
insert into Employees (id, name) values ('1', 'Alice'); | |
insert into Employees (id, name) values ('7', 'Bob'); | |
insert into Employees (id, name) values ('11', 'Meir'); | |
insert into Employees (id, name) values ('90', 'Winston'); | |
insert into Employees (id, name) values ('3', 'Jonathan'); | |
Truncate table EmployeeUNI; | |
insert into EmployeeUNI (id, unique_id) values ('3', '1'); | |
insert into EmployeeUNI (id, unique_id) values ('11', '2'); | |
insert into EmployeeUNI (id, unique_id) values ('90', '3'); |
答案
select e2.unique_id, e.name | |
from Employees e | |
left join employeeuni e2 on e.id = e2.id; |
# 2. 产品销售分析【简单】
问题描述
销售表 Sales: | |
+-------------+-------+ | |
| Column Name | Type | | |
+-------------+-------+ | |
| sale_id | int | | |
| product_id | int | | |
| year | int | | |
| quantity | int | | |
| price | int | | |
+-------------+-------+ | |
(sale_id, year) 是销售表 Sales 的主键(具有唯一值的列的组合)。 | |
product_id 是关联到产品表 Product 的外键(reference 列)。 | |
该表的每一行显示 product_id 在某一年的销售情况。 | |
注意: price 表示每单位价格。 | |
产品表 Product: | |
+--------------+---------+ | |
| Column Name | Type | | |
+--------------+---------+ | |
| product_id | int | | |
| product_name | varchar | | |
+--------------+---------+ | |
product_id 是表的主键(具有唯一值的列)。 | |
该表的每一行表示每种产品的产品名称。 | |
编写解决方案,以获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。 | |
返回结果表 无顺序要求 。 | |
结果格式示例如下。 | |
示例 1: | |
输入: | |
Sales 表: | |
+---------+------------+------+----------+-------+ | |
| sale_id | product_id | year | quantity | price | | |
+---------+------------+------+----------+-------+ | |
| 1 | 100 | 2008 | 10 | 5000 | | |
| 2 | 100 | 2009 | 12 | 5000 | | |
| 7 | 200 | 2011 | 15 | 9000 | | |
+---------+------------+------+----------+-------+ | |
Product 表: | |
+------------+--------------+ | |
| product_id | product_name | | |
+------------+--------------+ | |
| 100 | Nokia | | |
| 200 | Apple | | |
| 300 | Samsung | | |
+------------+--------------+ | |
输出: | |
+--------------+-------+-------+ | |
| product_name | year | price | | |
+--------------+-------+-------+ | |
| Nokia | 2008 | 5000 | | |
| Nokia | 2009 | 5000 | | |
| Apple | 2011 | 9000 | | |
+--------------+-------+-------+ |
建表语句
Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int); | |
Create table If Not Exists Product (product_id int, product_name varchar(10)); | |
Truncate table Sales; | |
insert into Sales (sale_id, product_id, year, quantity, price) values ('1', '100', '2008', '10', '5000'); | |
insert into Sales (sale_id, product_id, year, quantity, price) values ('2', '100', '2009', '12', '5000'); | |
insert into Sales (sale_id, product_id, year, quantity, price) values ('7', '200', '2011', '15', '9000'); | |
Truncate table Product; | |
insert into Product (product_id, product_name) values ('100', 'Nokia'); | |
insert into Product (product_id, product_name) values ('200', 'Apple'); | |
insert into Product (product_id, product_name) values ('300', 'Samsung'); |
答案
select p.product_name, s.year, s.price | |
from sales s | |
left outer join product p on s.product_id = p.product_id; |
# 3. 进店却未进行过交易的顾客【简单】
问题描述
表:Visits | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| visit_id | int | | |
| customer_id | int | | |
+-------------+---------+ | |
visit_id 是该表中具有唯一值的列。 | |
该表包含有关光临过购物中心的顾客的信息。 | |
表:Transactions | |
+----------------+---------+ | |
| Column Name | Type | | |
+----------------+---------+ | |
| transaction_id | int | | |
| visit_id | int | | |
| amount | int | | |
+----------------+---------+ | |
transaction_id 是该表中具有唯一值的列。 | |
此表包含 visit_id 期间进行的交易的信息。 | |
有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个解决方案,来查找这些顾客的 ID ,以及他们只光顾不交易的次数。 | |
返回以 任何顺序 排序的结果表。 | |
返回结果格式如下例所示。 | |
示例 1: | |
输入: | |
Visits | |
+----------+-------------+ | |
| visit_id | customer_id | | |
+----------+-------------+ | |
| 1 | 23 | | |
| 2 | 9 | | |
| 4 | 30 | | |
| 5 | 54 | | |
| 6 | 96 | | |
| 7 | 54 | | |
| 8 | 54 | | |
+----------+-------------+ | |
Transactions | |
+----------------+----------+--------+ | |
| transaction_id | visit_id | amount | | |
+----------------+----------+--------+ | |
| 2 | 5 | 310 | | |
| 3 | 5 | 300 | | |
| 9 | 5 | 200 | | |
| 12 | 1 | 910 | | |
| 13 | 2 | 970 | | |
+----------------+----------+--------+ | |
输出: | |
+-------------+----------------+ | |
| customer_id | count_no_trans | | |
+-------------+----------------+ | |
| 54 | 2 | | |
| 30 | 1 | | |
| 96 | 1 | | |
+-------------+----------------+ | |
解释: | |
ID = 23 的顾客曾经逛过一次购物中心,并在 ID = 12 的访问期间进行了一笔交易。 | |
ID = 9 的顾客曾经逛过一次购物中心,并在 ID = 13 的访问期间进行了一笔交易。 | |
ID = 30 的顾客曾经去过购物中心,并且没有进行任何交易。 | |
ID = 54 的顾客三度造访了购物中心。在 2 次访问中,他们没有进行任何交易,在 1 次访问中,他们进行了 3 次交易。 | |
ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。 | |
如我们所见,ID 为 30 和 96 的顾客一次没有进行任何交易就去了购物中心。顾客 54 也两次访问了购物中心并且没有进行任何交易。 |
建表语句
Create table If Not Exists Visits(visit_id int, customer_id int); | |
Create table If Not Exists Transactions(transaction_id int, visit_id int, amount int); | |
Truncate table Visits; | |
insert into Visits (visit_id, customer_id) values ('1', '23'); | |
insert into Visits (visit_id, customer_id) values ('2', '9'); | |
insert into Visits (visit_id, customer_id) values ('4', '30'); | |
insert into Visits (visit_id, customer_id) values ('5', '54'); | |
insert into Visits (visit_id, customer_id) values ('6', '96'); | |
insert into Visits (visit_id, customer_id) values ('7', '54'); | |
insert into Visits (visit_id, customer_id) values ('8', '54'); | |
Truncate table Transactions; | |
insert into Transactions (transaction_id, visit_id, amount) values ('2', '5', '310'); | |
insert into Transactions (transaction_id, visit_id, amount) values ('3', '5', '300'); | |
insert into Transactions (transaction_id, visit_id, amount) values ('9', '5', '200'); | |
insert into Transactions (transaction_id, visit_id, amount) values ('12', '1', '910'); | |
insert into Transactions (transaction_id, visit_id, amount) values ('13', '2', '970'); |
答案
select v.customer_id, count(*) as count_no_trans | |
from visits v | |
left join transactions t on v.visit_id = t.visit_id | |
where t.transaction_id is null | |
group by v.customer_id; |
# 4. 上升的温度【简单】
问题描述
表:Weather | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| id | int | | |
| recordDate | date | | |
| temperature | int | | |
+---------------+---------+ | |
id 是该表具有唯一值的列。 | |
没有具有相同 recordDate 的不同行。 | |
该表包含特定日期的温度信息 | |
编写解决方案,找出与之前(昨天的)日期相比温度更高的所有日期的 id 。 | |
返回结果 无顺序要求 。 | |
结果格式如下例子所示。 | |
示例 1: | |
输入: | |
Weather 表: | |
+----+------------+-------------+ | |
| id | recordDate | Temperature | | |
+----+------------+-------------+ | |
| 1 | 2015-01-01 | 10 | | |
| 2 | 2015-01-02 | 25 | | |
| 3 | 2015-01-03 | 20 | | |
| 4 | 2015-01-04 | 30 | | |
+----+------------+-------------+ | |
输出: | |
+----+ | |
| id | | |
+----+ | |
| 2 | | |
| 4 | | |
+----+ | |
解释: | |
2015-01-02 的温度比前一天高(10 -> 25) | |
2015-01-04 的温度比前一天高(20 -> 30) |
建表语句
Create table If Not Exists Weather (id int, recordDate date, temperature int); | |
Truncate table Weather; | |
insert into Weather (id, recordDate, temperature) values ('1', '2015-01-01', '10'); | |
insert into Weather (id, recordDate, temperature) values ('2', '2015-01-02', '25'); | |
insert into Weather (id, recordDate, temperature) values ('3', '2015-01-03', '20'); | |
insert into Weather (id, recordDate, temperature) values ('4', '2015-01-04', '30'); |
答案
select w.id | |
from weather w | |
cross join weather w2 on datediff(w.recordDate, w2.recordDate) = 1 | |
where w.temperature > w2.temperature; |
# 5. 每台机器的进程平均运行时间【简单】
考察点
- 自连接
- round
- avg
问题描述
表: Activity | |
+----------------+---------+ | |
| Column Name | Type | | |
+----------------+---------+ | |
| machine_id | int | | |
| process_id | int | | |
| activity_type | enum | | |
| timestamp | float | | |
+----------------+---------+ | |
该表展示了一家工厂网站的用户活动。 | |
(machine_id, process_id, activity_type) 是当前表的主键(具有唯一值的列的组合)。 | |
machine_id 是一台机器的ID号。 | |
process_id 是运行在各机器上的进程ID号。 | |
activity_type 是枚举类型 ('start', 'end')。 | |
timestamp 是浮点类型,代表当前时间(以秒为单位)。 | |
'start' 代表该进程在这台机器上的开始运行时间戳 , 'end' 代表该进程在这台机器上的终止运行时间戳。 | |
同一台机器,同一个进程都有一对开始时间戳和结束时间戳,而且开始时间戳永远在结束时间戳前面。 | |
现在有一个工厂网站由几台机器运行,每台机器上运行着 相同数量的进程 。编写解决方案,计算每台机器各自完成一个进程任务的平均耗时。 | |
完成一个进程任务的时间指进程的'end' 时间戳 减去 'start' 时间戳。平均耗时通过计算每台机器上所有进程任务的总耗费时间除以机器上的总进程数量获得。 | |
结果表必须包含machine_id(机器ID) 和对应的 average time(平均耗时) 别名 processing_time,且四舍五入保留3位小数。 | |
以 任意顺序 返回表。 | |
具体参考例子如下。 | |
示例 1: | |
输入: | |
Activity table: | |
+------------+------------+---------------+-----------+ | |
| machine_id | process_id | activity_type | timestamp | | |
+------------+------------+---------------+-----------+ | |
| 0 | 0 | start | 0.712 | | |
| 0 | 0 | end | 1.520 | | |
| 0 | 1 | start | 3.140 | | |
| 0 | 1 | end | 4.120 | | |
| 1 | 0 | start | 0.550 | | |
| 1 | 0 | end | 1.550 | | |
| 1 | 1 | start | 0.430 | | |
| 1 | 1 | end | 1.420 | | |
| 2 | 0 | start | 4.100 | | |
| 2 | 0 | end | 4.512 | | |
| 2 | 1 | start | 2.500 | | |
| 2 | 1 | end | 5.000 | | |
+------------+------------+---------------+-----------+ | |
输出: | |
+------------+-----------------+ | |
| machine_id | processing_time | | |
+------------+-----------------+ | |
| 0 | 0.894 | | |
| 1 | 0.995 | | |
| 2 | 1.456 | | |
+------------+-----------------+ | |
解释: | |
一共有3台机器,每台机器运行着两个进程. | |
机器 0 的平均耗时: ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894 | |
机器 1 的平均耗时: ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995 | |
机器 2 的平均耗时: ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456 |
建表语句
Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float); | |
Truncate table Activity; | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'start', '0.712'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'end', '1.52'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'start', '3.14'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'end', '4.12'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'start', '0.55'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'end', '1.55'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'start', '0.43'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'end', '1.42'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'start', '4.1'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'end', '4.512'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'start', '2.5'); | |
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'end', '5'); |
答案
select a1.machine_id, round(avg(a2.timestamp - a1.timestamp), 3) processing_time | |
from activity a1 | |
join activity a2 on a1.machine_id = a2.machine_id and a1.process_id = a2.process_id | |
where a1.activity_type = 'start' | |
and a2.activity_type = 'end' | |
group by a1.machine_id; |
# 6. 员工奖金【简单】
问题描述
表:Employee | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| empId | int | | |
| name | varchar | | |
| supervisor | int | | |
| salary | int | | |
+-------------+---------+ | |
empId 是该表中具有唯一值的列。 | |
该表的每一行都表示员工的姓名和 id,以及他们的工资和经理的 id。 | |
表:Bonus | |
+-------------+------+ | |
| Column Name | Type | | |
+-------------+------+ | |
| empId | int | | |
| bonus | int | | |
+-------------+------+ | |
empId 是该表具有唯一值的列。 | |
empId 是 Employee 表中 empId 的外键(reference 列)。 | |
该表的每一行都包含一个员工的 id 和他们各自的奖金。 | |
编写解决方案,报告每个奖金 少于 1000 的员工的姓名和奖金数额。 | |
以 任意顺序 返回结果表。 | |
结果格式如下所示。 | |
示例 1: | |
输入: | |
Employee table: | |
+-------+--------+------------+--------+ | |
| empId | name | supervisor | salary | | |
+-------+--------+------------+--------+ | |
| 3 | Brad | null | 4000 | | |
| 1 | John | 3 | 1000 | | |
| 2 | Dan | 3 | 2000 | | |
| 4 | Thomas | 3 | 4000 | | |
+-------+--------+------------+--------+ | |
Bonus table: | |
+-------+-------+ | |
| empId | bonus | | |
+-------+-------+ | |
| 2 | 500 | | |
| 4 | 2000 | | |
+-------+-------+ | |
输出: | |
+------+-------+ | |
| name | bonus | | |
+------+-------+ | |
| Brad | null | | |
| John | null | | |
| Dan | 500 | | |
+------+-------+ |
建表语句
Create table If Not Exists Employee (empId int, name varchar(255), supervisor int, salary int); | |
Create table If Not Exists Bonus (empId int, bonus int); | |
Truncate table Employee; | |
insert into Employee (empId, name, supervisor, salary) values ('3', 'Brad', null, '4000'); | |
insert into Employee (empId, name, supervisor, salary) values ('1', 'John', '3', '1000'); | |
insert into Employee (empId, name, supervisor, salary) values ('2', 'Dan', '3', '2000'); | |
insert into Employee (empId, name, supervisor, salary) values ('4', 'Thomas', '3', '4000'); | |
Truncate table Bonus; | |
insert into Bonus (empId, bonus) values ('2', '500'); | |
insert into Bonus (empId, bonus) values ('4', '2000'); |
答案
select e.name, b.bonus | |
from employee e | |
left join bonus b on e.empId = b.empId | |
where b.bonus < 1000 | |
or b.bonus is null; |
# 7. 学生们参加各科测试的次数【简单】[标注]
问题描述
学生表: Students | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| student_id | int | | |
| student_name | varchar | | |
+---------------+---------+ | |
在 SQL 中,主键为 student_id(学生ID)。 | |
该表内的每一行都记录有学校一名学生的信息。 | |
科目表: Subjects | |
+--------------+---------+ | |
| Column Name | Type | | |
+--------------+---------+ | |
| subject_name | varchar | | |
+--------------+---------+ | |
在 SQL 中,主键为 subject_name(科目名称)。 | |
每一行记录学校的一门科目名称。 | |
考试表: Examinations | |
+--------------+---------+ | |
| Column Name | Type | | |
+--------------+---------+ | |
| student_id | int | | |
| subject_name | varchar | | |
+--------------+---------+ | |
这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。 | |
学生表里的一个学生修读科目表里的每一门科目。 | |
这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。 | |
查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。 | |
查询结构格式如下所示。 | |
示例 1: | |
输入: | |
Students table: | |
+------------+--------------+ | |
| student_id | student_name | | |
+------------+--------------+ | |
| 1 | Alice | | |
| 2 | Bob | | |
| 13 | John | | |
| 6 | Alex | | |
+------------+--------------+ | |
Subjects table: | |
+--------------+ | |
| subject_name | | |
+--------------+ | |
| Math | | |
| Physics | | |
| Programming | | |
+--------------+ | |
Examinations table: | |
+------------+--------------+ | |
| student_id | subject_name | | |
+------------+--------------+ | |
| 1 | Math | | |
| 1 | Physics | | |
| 1 | Programming | | |
| 2 | Programming | | |
| 1 | Physics | | |
| 1 | Math | | |
| 13 | Math | | |
| 13 | Programming | | |
| 13 | Physics | | |
| 2 | Math | | |
| 1 | Math | | |
+------------+--------------+ | |
输出: | |
+------------+--------------+--------------+----------------+ | |
| student_id | student_name | subject_name | attended_exams | | |
+------------+--------------+--------------+----------------+ | |
| 1 | Alice | Math | 3 | | |
| 1 | Alice | Physics | 2 | | |
| 1 | Alice | Programming | 1 | | |
| 2 | Bob | Math | 1 | | |
| 2 | Bob | Physics | 0 | | |
| 2 | Bob | Programming | 1 | | |
| 6 | Alex | Math | 0 | | |
| 6 | Alex | Physics | 0 | | |
| 6 | Alex | Programming | 0 | | |
| 13 | John | Math | 1 | | |
| 13 | John | Physics | 1 | | |
| 13 | John | Programming | 1 | | |
+------------+--------------+--------------+----------------+ | |
解释: | |
结果表需包含所有学生和所有科目(即便测试次数为0): | |
Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试; | |
Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试; | |
Alex 啥测试都没参加; | |
John 参加了数学、物理、编程测试各 1 次。 |
建表语句
Create table If Not Exists Students (student_id int, student_name varchar(20)); | |
Create table If Not Exists Subjects (subject_name varchar(20)); | |
Create table If Not Exists Examinations (student_id int, subject_name varchar(20)); | |
Truncate table Students; | |
insert into Students (student_id, student_name) values ('1', 'Alice'); | |
insert into Students (student_id, student_name) values ('2', 'Bob'); | |
insert into Students (student_id, student_name) values ('13', 'John'); | |
insert into Students (student_id, student_name) values ('6', 'Alex'); | |
Truncate table Subjects; | |
insert into Subjects (subject_name) values ('Math'); | |
insert into Subjects (subject_name) values ('Physics'); | |
insert into Subjects (subject_name) values ('Programming'); | |
Truncate table Examinations; | |
insert into Examinations (student_id, subject_name) values ('1', 'Math'); | |
insert into Examinations (student_id, subject_name) values ('1', 'Physics'); | |
insert into Examinations (student_id, subject_name) values ('1', 'Programming'); | |
insert into Examinations (student_id, subject_name) values ('2', 'Programming'); | |
insert into Examinations (student_id, subject_name) values ('1', 'Physics'); | |
insert into Examinations (student_id, subject_name) values ('1', 'Math'); | |
insert into Examinations (student_id, subject_name) values ('13', 'Math'); | |
insert into Examinations (student_id, subject_name) values ('13', 'Programming'); | |
insert into Examinations (student_id, subject_name) values ('13', 'Physics'); | |
insert into Examinations (student_id, subject_name) values ('2', 'Math'); | |
insert into Examinations (student_id, subject_name) values ('1', 'Math'); |
答案
SELECT | |
s.student_id, s.student_name, sub.subject_name, IFNULL(grouped.attended_exams, 0) AS attended_exams | |
FROM | |
Students s | |
CROSS JOIN | |
Subjects sub | |
LEFT JOIN ( | |
SELECT student_id, subject_name, COUNT(*) AS attended_exams | |
FROM Examinations | |
GROUP BY student_id, subject_name | |
) grouped | |
ON s.student_id = grouped.student_id AND sub.subject_name = grouped.subject_name | |
ORDER BY s.student_id, sub.subject_name; |
# 8. 至少有 5 名直接下属的经理【中等】[标注]
问题描述
表: Employee | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| id | int | | |
| name | varchar | | |
| department | varchar | | |
| managerId | int | | |
+-------------+---------+ | |
id 是此表的主键(具有唯一值的列)。 | |
该表的每一行表示雇员的名字、他们的部门和他们的经理的id。 | |
如果managerId为空,则该员工没有经理。 | |
没有员工会成为自己的管理者。 | |
编写一个解决方案,找出至少有五个直接下属的经理。 | |
以 任意顺序 返回结果表。 | |
查询结果格式如下所示。 | |
示例 1: | |
输入: | |
Employee 表: | |
+-----+-------+------------+-----------+ | |
| id | name | department | managerId | | |
+-----+-------+------------+-----------+ | |
| 101 | John | A | Null | | |
| 102 | Dan | A | 101 | | |
| 103 | James | A | 101 | | |
| 104 | Amy | A | 101 | | |
| 105 | Anne | A | 101 | | |
| 106 | Ron | B | 101 | | |
+-----+-------+------------+-----------+ | |
输出: | |
+------+ | |
| name | | |
+------+ | |
| John | | |
+------+ |
建表语句
drop table employee; | |
Create table If Not Exists Employee (id int, name varchar(255), department varchar(255), managerId int); | |
Truncate table Employee; | |
insert into Employee (id, name, department, managerId) values ('101', 'John', 'A', null); | |
insert into Employee (id, name, department, managerId) values ('102', 'Dan', 'A', '101'); | |
insert into Employee (id, name, department, managerId) values ('103', 'James', 'A', '101'); | |
insert into Employee (id, name, department, managerId) values ('104', 'Amy', 'A', '101'); | |
insert into Employee (id, name, department, managerId) values ('105', 'Anne', 'A', '101'); | |
insert into Employee (id, name, department, managerId) values ('106', 'Ron', 'B', '101'); |
答案
select Employee.Name as Name | |
from ( | |
select ManagerId as Id | |
from Employee | |
group by ManagerId | |
having count(Id) >= 5 | |
) as Manager join Employee | |
on Manager.Id = Employee.Id |
# 9. 确认率【中等】
问题描述
表: Signups | |
+----------------+----------+ | |
| Column Name | Type | | |
+----------------+----------+ | |
| user_id | int | | |
| time_stamp | datetime | | |
+----------------+----------+ | |
User_id是该表的主键。 | |
每一行都包含ID为user_id的用户的注册时间信息。 | |
表: Confirmations | |
+----------------+----------+ | |
| Column Name | Type | | |
+----------------+----------+ | |
| user_id | int | | |
| time_stamp | datetime | | |
| action | ENUM | | |
+----------------+----------+ | |
(user_id, time_stamp)是该表的主键。 | |
user_id是一个引用到注册表的外键。 | |
action是类型为('confirmed', 'timeout')的ENUM | |
该表的每一行都表示ID为user_id的用户在time_stamp请求了一条确认消息,该确认消息要么被确认('confirmed'),要么被过期('timeout')。 | |
用户的 确认率 是 'confirmed' 消息的数量除以请求的确认消息的总数。没有请求任何确认消息的用户的确认率为 0 。确认率四舍五入到 小数点后两位 。 | |
编写一个SQL查询来查找每个用户的 确认率 。 | |
以 任意顺序 返回结果表。 | |
查询结果格式如下所示。 | |
示例1: | |
输入: | |
Signups 表: | |
+---------+---------------------+ | |
| user_id | time_stamp | | |
+---------+---------------------+ | |
| 3 | 2020-03-21 10:16:13 | | |
| 7 | 2020-01-04 13:57:59 | | |
| 2 | 2020-07-29 23:09:44 | | |
| 6 | 2020-12-09 10:39:37 | | |
+---------+---------------------+ | |
Confirmations 表: | |
+---------+---------------------+-----------+ | |
| user_id | time_stamp | action | | |
+---------+---------------------+-----------+ | |
| 3 | 2021-01-06 03:30:46 | timeout | | |
| 3 | 2021-07-14 14:00:00 | timeout | | |
| 7 | 2021-06-12 11:57:29 | confirmed | | |
| 7 | 2021-06-13 12:58:28 | confirmed | | |
| 7 | 2021-06-14 13:59:27 | confirmed | | |
| 2 | 2021-01-22 00:00:00 | confirmed | | |
| 2 | 2021-02-28 23:59:59 | timeout | | |
+---------+---------------------+-----------+ | |
输出: | |
+---------+-------------------+ | |
| user_id | confirmation_rate | | |
+---------+-------------------+ | |
| 6 | 0.00 | | |
| 3 | 0.00 | | |
| 7 | 1.00 | | |
| 2 | 0.50 | | |
+---------+-------------------+ | |
解释: | |
用户 6 没有请求任何确认消息。确认率为 0。 | |
用户 3 进行了 2 次请求,都超时了。确认率为 0。 | |
用户 7 提出了 3 个请求,所有请求都得到了确认。确认率为 1。 | |
用户 2 做了 2 个请求,其中一个被确认,另一个超时。确认率为 1 / 2 = 0.5。 |
建表语句
Create table If Not Exists Signups (user_id int, time_stamp datetime); | |
Create table If Not Exists Confirmations (user_id int, time_stamp datetime, action ENUM('confirmed','timeout')); | |
Truncate table Signups; | |
insert into Signups (user_id, time_stamp) values ('3', '2020-03-21 10:16:13'); | |
insert into Signups (user_id, time_stamp) values ('7', '2020-01-04 13:57:59'); | |
insert into Signups (user_id, time_stamp) values ('2', '2020-07-29 23:09:44'); | |
insert into Signups (user_id, time_stamp) values ('6', '2020-12-09 10:39:37'); | |
Truncate table Confirmations; | |
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-01-06 03:30:46', 'timeout'); | |
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-07-14 14:00:00', 'timeout'); | |
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-12 11:57:29', 'confirmed'); | |
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-13 12:58:28', 'confirmed'); | |
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-14 13:59:27', 'confirmed'); | |
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-01-22 00:00:00', 'confirmed'); | |
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-02-28 23:59:59', 'timeout'); |
答案
select s.user_id, ifnull(round(avg(c.action = 'confirmed'), 2), 0.00) confirmation_rate | |
from signups s | |
left join confirmations c on s.user_id = c.user_id | |
group by s.user_id; |
# 三、聚合函数
# 1. 有趣的电影【简单】
问题描述
表:cinema | |
+----------------+----------+ | |
| Column Name | Type | | |
+----------------+----------+ | |
| id | int | | |
| movie | varchar | | |
| description | varchar | | |
| rating | float | | |
+----------------+----------+ | |
id 是该表的主键(具有唯一值的列)。 | |
每行包含有关电影名称、类型和评级的信息。 | |
评级为 [0,10] 范围内的小数点后 2 位浮点数。 | |
编写解决方案,找出所有影片描述为 非 boring (不无聊) 的并且 id 为奇数 的影片。 | |
返回结果按 rating 降序排列。 | |
结果格式如下示例。 | |
示例 1: | |
输入: | |
+---------+-----------+--------------+-----------+ | |
| id | movie | description | rating | | |
+---------+-----------+--------------+-----------+ | |
| 1 | War | great 3D | 8.9 | | |
| 2 | Science | fiction | 8.5 | | |
| 3 | irish | boring | 6.2 | | |
| 4 | Ice song | Fantacy | 8.6 | | |
| 5 | House card| Interesting| 9.1 | | |
+---------+-----------+--------------+-----------+ | |
输出: | |
+---------+-----------+--------------+-----------+ | |
| id | movie | description | rating | | |
+---------+-----------+--------------+-----------+ | |
| 5 | House card| Interesting| 9.1 | | |
| 1 | War | great 3D | 8.9 | | |
+---------+-----------+--------------+-----------+ | |
解释: | |
我们有三部电影,它们的 id 是奇数:1、3 和 5。id = 3 的电影是 boring 的,所以我们不把它包括在答案中。 |
建表语句
Create table If Not Exists cinema (id int, movie varchar(255), description varchar(255), rating float(2, 1)); | |
Truncate table cinema; | |
insert into cinema (id, movie, description, rating) values ('1', 'War', 'great 3D', '8.9'); | |
insert into cinema (id, movie, description, rating) values ('2', 'Science', 'fiction', '8.5'); | |
insert into cinema (id, movie, description, rating) values ('3', 'irish', 'boring', '6.2'); | |
insert into cinema (id, movie, description, rating) values ('4', 'Ice song', 'Fantacy', '8.6'); | |
insert into cinema (id, movie, description, rating) values ('5', 'House card', 'Interesting', '9.1'); |
答案
select * | |
from cinema | |
where description <> 'boring' | |
and mod(id, 2) = 1 | |
order by rating desc; |
# 2. 平均售价【简单】
问题描述
表:Prices | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| product_id | int | | |
| start_date | date | | |
| end_date | date | | |
| price | int | | |
+---------------+---------+ | |
(product_id,start_date,end_date) 是 prices 表的主键(具有唯一值的列的组合)。 | |
prices 表的每一行表示的是某个产品在一段时期内的价格。 | |
每个产品的对应时间段是不会重叠的,这也意味着同一个产品的价格时段不会出现交叉。 | |
表:UnitsSold | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| product_id | int | | |
| purchase_date | date | | |
| units | int | | |
+---------------+---------+ | |
该表可能包含重复数据。 | |
该表的每一行表示的是每种产品的出售日期,单位和产品 id。 | |
编写解决方案以查找每种产品的平均售价。average_price 应该 四舍五入到小数点后两位。 | |
返回结果表 无顺序要求 。 | |
结果格式如下例所示。 | |
示例 1: | |
输入: | |
Prices table: | |
+------------+------------+------------+--------+ | |
| product_id | start_date | end_date | price | | |
+------------+------------+------------+--------+ | |
| 1 | 2019-02-17 | 2019-02-28 | 5 | | |
| 1 | 2019-03-01 | 2019-03-22 | 20 | | |
| 2 | 2019-02-01 | 2019-02-20 | 15 | | |
| 2 | 2019-02-21 | 2019-03-31 | 30 | | |
+------------+------------+------------+--------+ | |
UnitsSold table: | |
+------------+---------------+-------+ | |
| product_id | purchase_date | units | | |
+------------+---------------+-------+ | |
| 1 | 2019-02-25 | 100 | | |
| 1 | 2019-03-01 | 15 | | |
| 2 | 2019-02-10 | 200 | | |
| 2 | 2019-03-22 | 30 | | |
+------------+---------------+-------+ | |
输出: | |
+------------+---------------+ | |
| product_id | average_price | | |
+------------+---------------+ | |
| 1 | 6.96 | | |
| 2 | 16.96 | | |
+------------+---------------+ | |
解释: | |
平均售价 = 产品总价 / 销售的产品数量。 | |
产品 1 的平均售价 = ((100 * 5)+(15 * 20) )/ 115 = 6.96 | |
产品 2 的平均售价 = ((200 * 15)+(30 * 30) )/ 230 = 16.96 |
建表语句
Create table If Not Exists Prices (product_id int, start_date date, end_date date, price int); | |
Create table If Not Exists UnitsSold (product_id int, purchase_date date, units int); | |
Truncate table Prices; | |
insert into Prices (product_id, start_date, end_date, price) values ('1', '2019-02-17', '2019-02-28', '5'); | |
insert into Prices (product_id, start_date, end_date, price) values ('1', '2019-03-01', '2019-03-22', '20'); | |
insert into Prices (product_id, start_date, end_date, price) values ('2', '2019-02-01', '2019-02-20', '15'); | |
insert into Prices (product_id, start_date, end_date, price) values ('2', '2019-02-21', '2019-03-31', '30'); | |
Truncate table UnitsSold; | |
insert into UnitsSold (product_id, purchase_date, units) values ('1', '2019-02-25', '100'); | |
insert into UnitsSold (product_id, purchase_date, units) values ('1', '2019-03-01', '15'); | |
insert into UnitsSold (product_id, purchase_date, units) values ('2', '2019-02-10', '200'); | |
insert into UnitsSold (product_id, purchase_date, units) values ('2', '2019-03-22', '30'); |
答案
SELECT | |
product_id, | |
IFNULL(Round(SUM(sales) / SUM(units), 2), 0) AS average_price | |
FROM ( | |
SELECT | |
Prices.product_id AS product_id, | |
Prices.price * UnitsSold.units AS sales, | |
UnitsSold.units AS units | |
FROM Prices | |
LEFT JOIN UnitsSold ON Prices.product_id = UnitsSold.product_id | |
AND (UnitsSold.purchase_date BETWEEN Prices.start_date AND Prices.end_date) | |
) T | |
GROUP BY product_id |
# 3. 项目员工 Ⅰ【简单】
问题描述
项目表 Project: | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| project_id | int | | |
| employee_id | int | | |
+-------------+---------+ | |
主键为 (project_id, employee_id)。 | |
employee_id 是员工表 Employee 表的外键。 | |
员工表 Employee: | |
+------------------+---------+ | |
| Column Name | Type | | |
+------------------+---------+ | |
| employee_id | int | | |
| name | varchar | | |
| experience_years | int | | |
+------------------+---------+ | |
主键是 employee_id。 | |
请写一个 SQL 语句,查询每一个项目中员工的 平均 工作年限,精确到小数点后两位。 | |
查询结果的格式如下: | |
Project 表: | |
+-------------+-------------+ | |
| project_id | employee_id | | |
+-------------+-------------+ | |
| 1 | 1 | | |
| 1 | 2 | | |
| 1 | 3 | | |
| 2 | 1 | | |
| 2 | 4 | | |
+-------------+-------------+ | |
Employee 表: | |
+-------------+--------+------------------+ | |
| employee_id | name | experience_years | | |
+-------------+--------+------------------+ | |
| 1 | Khaled | 3 | | |
| 2 | Ali | 2 | | |
| 3 | John | 1 | | |
| 4 | Doe | 2 | | |
+-------------+--------+------------------+ | |
Result 表: | |
+-------------+---------------+ | |
| project_id | average_years | | |
+-------------+---------------+ | |
| 1 | 2.00 | | |
| 2 | 2.50 | | |
+-------------+---------------+ | |
第一个项目中,员工的平均工作年限是 (3 + 2 + 1) / 3 = 2.00;第二个项目中,员工的平均工作年限是 (3 + 2) / 2 = 2.50 |
建表语句
Create table If Not Exists Project (project_id int, employee_id int); | |
Create table If Not Exists Employee (employee_id int, name varchar(10), experience_years int); | |
Truncate table Project; | |
insert into Project (project_id, employee_id) values ('1', '1'); | |
insert into Project (project_id, employee_id) values ('1', '2'); | |
insert into Project (project_id, employee_id) values ('1', '3'); | |
insert into Project (project_id, employee_id) values ('2', '1'); | |
insert into Project (project_id, employee_id) values ('2', '4'); | |
Truncate table Employee; | |
insert into Employee (employee_id, name, experience_years) values ('1', 'Khaled', '3'); | |
insert into Employee (employee_id, name, experience_years) values ('2', 'Ali', '2'); | |
insert into Employee (employee_id, name, experience_years) values ('3', 'John', '1'); | |
insert into Employee (employee_id, name, experience_years) values ('4', 'Doe', '2'); |
答案
select T.project_id, round(avg(T.experience_years), 2) average_years | |
from (select p.project_id as project_id, e.experience_years as experience_years | |
from project p | |
left join employee e on p.employee_id = e.employee_id) T | |
group by T.project_id |
# 4. 各赛事的用户注册率【简单】
问题描述
用户表: Users | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| user_id | int | | |
| user_name | varchar | | |
+-------------+---------+ | |
user_id 是该表的主键(具有唯一值的列)。 | |
该表中的每行包括用户 ID 和用户名。 | |
注册表: Register | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| contest_id | int | | |
| user_id | int | | |
+-------------+---------+ | |
(contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 | |
该表中的每行包含用户的 ID 和他们注册的赛事。 | |
编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 | |
返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 | |
返回结果如下示例所示。 | |
示例 1: | |
输入: | |
Users 表: | |
+---------+-----------+ | |
| user_id | user_name | | |
+---------+-----------+ | |
| 6 | Alice | | |
| 2 | Bob | | |
| 7 | Alex | | |
+---------+-----------+ | |
Register 表: | |
+------------+---------+ | |
| contest_id | user_id | | |
+------------+---------+ | |
| 215 | 6 | | |
| 209 | 2 | | |
| 208 | 2 | | |
| 210 | 6 | | |
| 208 | 6 | | |
| 209 | 7 | | |
| 209 | 6 | | |
| 215 | 7 | | |
| 208 | 7 | | |
| 210 | 2 | | |
| 207 | 2 | | |
| 210 | 7 | | |
+------------+---------+ | |
输出: | |
+------------+------------+ | |
| contest_id | percentage | | |
+------------+------------+ | |
| 208 | 100.0 | | |
| 209 | 100.0 | | |
| 210 | 100.0 | | |
| 215 | 66.67 | | |
| 207 | 33.33 | | |
+------------+------------+ | |
解释: | |
所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 | |
Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% | |
Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33% |
建表语句
Create table If Not Exists Users (user_id int, user_name varchar(20)); | |
Create table If Not Exists Register (contest_id int, user_id int); | |
Truncate table Users; | |
insert into Users (user_id, user_name) values ('6', 'Alice'); | |
insert into Users (user_id, user_name) values ('2', 'Bob'); | |
insert into Users (user_id, user_name) values ('7', 'Alex'); | |
Truncate table Register; | |
insert into Register (contest_id, user_id) values ('215', '6'); | |
insert into Register (contest_id, user_id) values ('209', '2'); | |
insert into Register (contest_id, user_id) values ('208', '2'); | |
insert into Register (contest_id, user_id) values ('210', '6'); | |
insert into Register (contest_id, user_id) values ('208', '6'); | |
insert into Register (contest_id, user_id) values ('209', '7'); | |
insert into Register (contest_id, user_id) values ('209', '6'); | |
insert into Register (contest_id, user_id) values ('215', '7'); | |
insert into Register (contest_id, user_id) values ('208', '7'); | |
insert into Register (contest_id, user_id) values ('210', '2'); | |
insert into Register (contest_id, user_id) values ('207', '2'); | |
insert into Register (contest_id, user_id) values ('210', '7'); |
答案
select contest_id, round(count(user_id) * 100 / (select count(*) from users), 2) percentage | |
from Register | |
group by contest_id | |
order by percentage desc, contest_id; |
# 5. 查询结果的质量和占比【简单】
问题描述
Queries 表: | |
+-------------+---------+ | |
| Column Name | Type | | |
+-------------+---------+ | |
| query_name | varchar | | |
| result | varchar | | |
| position | int | | |
| rating | int | | |
+-------------+---------+ | |
此表可能有重复的行。 | |
此表包含了一些从数据库中收集的查询信息。 | |
“位置”(position)列的值为 1 到 500 。 | |
“评分”(rating)列的值为 1 到 5 。评分小于 3 的查询被定义为质量很差的查询。 | |
将查询结果的质量 quality 定义为: | |
各查询结果的评分与其位置之间比率的平均值。 | |
将劣质查询百分比 poor_query_percentage 为: | |
评分小于 3 的查询结果占全部查询结果的百分比。 | |
编写解决方案,找出每次的 query_name 、 quality 和 poor_query_percentage。 | |
quality 和 poor_query_percentage 都应 四舍五入到小数点后两位 。 | |
以 任意顺序 返回结果表。 | |
结果格式如下所示: | |
示例 1: | |
输入: | |
Queries table: | |
+------------+-------------------+----------+--------+ | |
| query_name | result | position | rating | | |
+------------+-------------------+----------+--------+ | |
| Dog | Golden Retriever | 1 | 5 | | |
| Dog | German Shepherd | 2 | 5 | | |
| Dog | Mule | 200 | 1 | | |
| Cat | Shirazi | 5 | 2 | | |
| Cat | Siamese | 3 | 3 | | |
| Cat | Sphynx | 7 | 4 | | |
+------------+-------------------+----------+--------+ | |
输出: | |
+------------+---------+-----------------------+ | |
| query_name | quality | poor_query_percentage | | |
+------------+---------+-----------------------+ | |
| Dog | 2.50 | 33.33 | | |
| Cat | 0.66 | 33.33 | | |
+------------+---------+-----------------------+ | |
解释: | |
Dog 查询结果的质量为 ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50 | |
Dog 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33 | |
Cat 查询结果的质量为 ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66 | |
Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33 |
建表语句
Create table If Not Exists Queries (query_name varchar(30), result varchar(50), position int, rating int); | |
Truncate table Queries; | |
insert into Queries (query_name, result, position, rating) values ('Dog', 'Golden Retriever', '1', '5'); | |
insert into Queries (query_name, result, position, rating) values ('Dog', 'German Shepherd', '2', '5'); | |
insert into Queries (query_name, result, position, rating) values ('Dog', 'Mule', '200', '1'); | |
insert into Queries (query_name, result, position, rating) values ('Cat', 'Shirazi', '5', '2'); | |
insert into Queries (query_name, result, position, rating) values ('Cat', 'Siamese', '3', '3'); | |
insert into Queries (query_name, result, position, rating) values ('Cat', 'Sphynx', '7', '4'); |
答案
select query_name, | |
round(avg(rating / position), 2) quality, | |
round(sum(if(rating < 3, 1, 0)) * 100 / count(*), 2) as poor_query_percentage | |
from queries | |
group by query_name having query_name is not null ; |
# 6. 每月交易 Ⅰ【中等】
问题描述
表:Transactions | |
+---------------+---------+ | |
| Column Name | Type | | |
+---------------+---------+ | |
| id | int | | |
| country | varchar | | |
| state | enum | | |
| amount | int | | |
| trans_date | date | | |
+---------------+---------+ | |
id 是这个表的主键。 | |
该表包含有关传入事务的信息。 | |
state 列类型为 ["approved", "declined"] 之一。 | |
编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。 | |
以 任意顺序 返回结果表。 | |
查询结果格式如下所示。 | |
示例 1: | |
输入: | |
Transactions table: | |
+------+---------+----------+--------+------------+ | |
| id | country | state | amount | trans_date | | |
+------+---------+----------+--------+------------+ | |
| 121 | US | approved | 1000 | 2018-12-18 | | |
| 122 | US | declined | 2000 | 2018-12-19 | | |
| 123 | US | approved | 2000 | 2019-01-01 | | |
| 124 | DE | approved | 2000 | 2019-01-07 | | |
+------+---------+----------+--------+------------+ | |
输出: | |
+----------+---------+-------------+----------------+--------------------+-----------------------+ | |
| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount | | |
+----------+---------+-------------+----------------+--------------------+-----------------------+ | |
| 2018-12 | US | 2 | 1 | 3000 | 1000 | | |
| 2019-01 | US | 1 | 1 | 2000 | 2000 | | |
| 2019-01 | DE | 1 | 1 | 2000 | 2000 | | |
+----------+---------+-------------+----------------+--------------------+-----------------------+ |
建表语句
Create table If Not Exists Transactions (id int, country varchar(4), state enum('approved', 'declined'), amount int, trans_date date); | |
Truncate table Transactions; | |
insert into Transactions (id, country, state, amount, trans_date) values ('121', 'US', 'approved', '1000', '2018-12-18'); | |
insert into Transactions (id, country, state, amount, trans_date) values ('122', 'US', 'declined', '2000', '2018-12-19'); | |
insert into Transactions (id, country, state, amount, trans_date) values ('123', 'US', 'approved', '2000', '2019-01-01'); | |
insert into Transactions (id, country, state, amount, trans_date) values ('124', 'DE', 'approved', '2000', '2019-01-07'); |
答案
select date_format(trans_date, '%Y-%m') as month, | |
country, | |
count(*) as trans_count, | |
sum(if(state = 'approved', 1, 0)) as approved_count, | |
sum(amount) as trans_total_amount, | |
sum(if(state = 'approved', amount, 0)) as approved_total_amount | |
from transactions | |
group by month, country; |
# 7. 即时食物配送 Ⅱ【中等】
问题描述
建表语句
答案
# 8. 游戏玩法分析 Ⅳ【中等】
问题描述
建表语句
答案
# 四、排序分组
# 1. 每位教师所教授的科目种类的数量【简单】
问题描述
建表语句
答案
# 2. 查询近 30 天活跃用户数【简单】
问题描述
建表语句
答案
# 3. 销售分析 Ⅲ【简单】
问题描述
建表语句
答案
# 4. 超过 5 名学生的课【简单】
问题描述
建表语句
答案
# 6. 求关注者的数量【简单】
问题描述
建表语句
答案
# 7. 只出现一次的最大数字【简单】
问题描述
建表语句
答案
# 8. 买下所有产品的客户【中等】
问题描述
建表语句
答案
# 五、高级查询和连接
# 1. 每位经理的下属员工数量【简单】
问题描述
建表语句
答案
# 2. 员工的直属部门【简单】
问题描述
建表语句
答案
# 3. 判断三角形【简单】
问题描述
建表语句
答案
# 4. 连续出现的数字【中等】
问题描述
建表语句
答案
# 5. 指定日期的产品价格【中等】
问题描述
建表语句
答案
# 6. 最后一个进入巴士的人【中等】
问题描述
建表语句
答案
# 7. 按分类统计薪水【中等】
问题描述
建表语句
答案
# 六、高级字符串函数 / 正则表达式 / 子句
# 1. 修复表中的名字【简单】
问题描述
建表语句
答案
# 2. 患某种疾病的患者【简单】
问题描述
建表语句
答案
# 3. 删除重复的电子邮箱【简单】
问题描述
建表语句
答案
# 4. 第二高的薪水【中等】
问题描述
建表语句
答案
# 5. 按日期分组销售产品【简单】
问题描述
建表语句
答案
# 6. 列出指定时间段内所有的下单产品【简单】
问题描述
建表语句
答案
# 7. 查找拥有有效邮箱的用户【简单】
问题描述
建表语句
答案