我是靠谱客的博主 无奈世界,最近开发中收集的这篇文章主要介绍Leetcode力扣数据库SQL(VIP)题目及解答全集511 游戏玩法分析I512 游戏玩法分析II534 游戏玩法分析III550 游戏玩法分析IV569 员工薪水中位数570 至少有5名直接下属的经理571 给定数字的频率查询中位数574 当选者578 查询回答率最高的问题579 查询员工的累积薪水580 统计各专业学生人数585 2016年的投资602 好友申请II:谁有最多的好友614 二级关注者615 平均工资:部门与公司比较618 学生地理信息报告1045 买下所有产品的客户107,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

511 游戏玩法分析I

 ##SQL架构
 Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
 Truncate table Activity
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
 insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
 ​
 ##活动表 Activity:
 +--------------+---------+
 | Column Name
| Type  
|
 +--------------+---------+
 | player_id  
| int     |
 | device_id  
| int     |
 | event_date   | date  
|
 | games_played | int     |
 +--------------+---------+
 表的主键是 (player_id, event_date)。
 这张表展示了一些游戏玩家在游戏平台上的行为活动。
 每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。
 ​
 写一条 SQL 查询语句获取每位玩家 第一次登陆平台的日期。
 查询结果的格式如下所示:
 ​
 ##Activity 表:
 +-----------+-----------+------------+--------------+
 | player_id | device_id | event_date | games_played |
 +-----------+-----------+------------+--------------+
 | 1         | 2         | 2016-03-01 | 5          
|
 | 1         | 2         | 2016-05-02 | 6          
|
 | 2         | 3         | 2017-06-25 | 1          
|
 | 3         | 1         | 2016-03-02 | 0          
|
 | 3         | 4         | 2018-07-03 | 5          
|
 +-----------+-----------+------------+--------------+
 ​
 ##Result 表:
 +-----------+-------------+
 | player_id | first_login |
 +-----------+-------------+
 | 1         | 2016-03-01
|
 | 2         | 2017-06-25
|
 | 3         | 2016-03-02
|
 +-----------+-------------+
 ​
 ### 答案:
 select player_id, min(event_date) first_login
 from Activity
 group by player_id;

512 游戏玩法分析II

 ##SQL架构
 Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
 Truncate table Activity
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '3', '2017-06-25', '1')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
 ​
 ##Table: Activity
 +--------------+---------+
 | Column Name
| Type  
|
 +--------------+---------+
 | player_id  
| int     |
 | device_id  
| int     |
 | event_date   | date  
|
 | games_played | int     |
 +--------------+---------+
 (player_id, event_date) 是这个表的两个主键
 这个表显示的是某些游戏玩家的游戏活动情况
 每一行是在某天使用某个设备登出之前登录并玩多个游戏(可能为0)的玩家的记录
 ​
 请编写一个 SQL 查询,描述每一个玩家首次登陆的设备名称
 ​
 查询结果格式在以下示例中:
 ​
 ##Activity table:
 +-----------+-----------+------------+--------------+
 | player_id | device_id | event_date | games_played |
 +-----------+-----------+------------+--------------+
 | 1         | 2         | 2016-03-01 | 5          
|
 | 1         | 2         | 2016-05-02 | 6          
|
 | 2         | 3         | 2017-06-25 | 1          
|
 | 3         | 1         | 2016-03-02 | 0          
|
 | 3         | 4         | 2018-07-03 | 5          
|
 +-----------+-----------+------------+--------------+
 ​
 ##Result table:
 +-----------+-----------+
 | player_id | device_id |
 +-----------+-----------+
 | 1         | 2         |
 | 2         | 3         |
 | 3         | 1         |
 +-----------+-----------+
 ​
 ### 答案:
 SELECT
   
player_id,
   
device_id
 FROM
   
(SELECT
       
player_id,
       
device_id,
       
ROW_NUMBER() OVER(PARTITION BY player_id ORDER BY event_date) AS rn
     FROM
       
Activity) AS a
 WHERE
   
rn = 1;

534 游戏玩法分析III

 ##SQL架构
 Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
 Truncate table Activity
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '3', '2017-06-25', '1')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
 ​
 Table: Activity
 ​
 +--------------+---------+
 | Column Name
| Type  
|
 +--------------+---------+
 | player_id  
| int     |
 | device_id  
| int     |
 | event_date   | date  
|
 | games_played | int     |
 +--------------+---------+
 (player_id,event_date)是此表的主键。
 这张表显示了某些游戏的玩家的活动情况。
 每一行是一个玩家的记录,他在某一天使用某个设备注销之前登录并玩了很多游戏(可能是 0 )。
 ​
 编写一个 SQL 查询,同时报告每组玩家和日期,以及玩家到目前为止玩了多少游戏。也就是说,在此日期之前玩家所玩的游戏总数。详细情况请查看示例。
 ​
 查询结果格式如下所示:
 ​
 Activity table:
 +-----------+-----------+------------+--------------+
 | player_id | device_id | event_date | games_played |
 +-----------+-----------+------------+--------------+
 | 1         | 2         | 2016-03-01 | 5          
|
 | 1         | 2         | 2016-05-02 | 6          
|
 | 1         | 3         | 2017-06-25 | 1          
|
 | 3         | 1         | 2016-03-02 | 0          
|
 | 3         | 4         | 2018-07-03 | 5          
|
 +-----------+-----------+------------+--------------+
 ​
 Result table:
 +-----------+------------+---------------------+
 | player_id | event_date | games_played_so_far |
 +-----------+------------+---------------------+
 | 1         | 2016-03-01 | 5                   |
 | 1         | 2016-05-02 | 11                
|
 | 1         | 2017-06-25 | 12                
|
 | 3         | 2016-03-02 | 0                   |
 | 3         | 2018-07-03 | 5                   |
 +-----------+------------+---------------------+
 对于 ID 为 1 的玩家,2016-05-02 共玩了 5+6=11 个游戏,2017-06-25 共玩了 5+6+1=12 个游戏。
 对于 ID 为 3 的玩家,2018-07-03 共玩了 0+5=5 个游戏。
 请注意,对于每个玩家,我们只关心玩家的登录日期。
 ​
 # 答案:
 窗口函数
 select player_id,event_date,  
 sum(games_played) over(partition by player_id order by event_date) as games_played_so_far
 from Activity
 ​
 自连接
 select t1.player_id,
        t1.event_date,
        sum(t2.games_played) games_played_so_far
 from Activity t1,Activity t2
 where t1.player_id=t2.player_id
   and t1.event_date>=t2.event_date
 group by t1.player_id,t1.event_date;

550 游戏玩法分析IV

 ##SQL架构
 Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
 Truncate table Activity
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
 insert into Activity (player_id, device_id, event_date, games_played) values ('1', '3', '2017-06-25', '1')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
 insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
 ​
 Table: Activity
 ​
 +--------------+---------+
 | Column Name
| Type  
|
 +--------------+---------+
 | player_id  
| int     |
 | device_id  
| int     |
 | event_date   | date  
|
 | games_played | int     |
 +--------------+---------+
 (player_id,event_date)是此表的主键。
 这张表显示了某些游戏的玩家的活动情况。
 每一行是一个玩家的记录,他在某一天使用某个设备注销之前登录并玩了很多游戏(可能是 0 )。
 ​
 编写一个 SQL 查询,报告在首次登录的第二天再次登录的玩家的比率,四舍五入到小数点后两位。换句话说,您需要计算从首次登录日期开始至少连续两天登录的玩家的数量,然后除以玩家总数。
 ​
 查询结果格式如下所示:
 ​
 Activity table:
 +-----------+-----------+------------+--------------+
 | player_id | device_id | event_date | games_played |
 +-----------+-----------+------------+--------------+
 | 1         | 2         | 2016-03-01 | 5          
|
 | 1         | 2         | 2016-03-02 | 6          
|
 | 2         | 3         | 2017-06-25 | 1          
|
 | 3         | 1         | 2016-03-02 | 0          
|
 | 3         | 4         | 2018-07-03 | 5          
|
 +-----------+-----------+------------+--------------+
 ​
 Result table:
 +-----------+
 | fraction
|
 +-----------+
 | 0.33    
|
 +-----------+
 只有 ID 为 1 的玩家在第一天登录后才重新登录,所以答案是 1/3 = 0.33
 ​
 # 答案:
 求出所有玩家首次登陆数据
 select player_id,min(event_date) first_date from activity group by player_id
 ​
 将所有玩家首次登陆数据作为临时表,并和所有数据表activity进行关联
 select *
 from activity a,
 (select player_id,min(event_date) first_date from activity group by player_id) b
 where a.player_id=b.player_id
 ​
 使用datediff函数计算玩家每次登陆和首次登陆的日期差,使用count+distinct函数获取所有玩家数
 select datediff(a.event_date,b.first_date),(select count(distinct(player_id)) from activity)
 from activity a,
 (select player_id,min(event_date) first_date from activity group by player_id) b
 where a.player_id=b.player_id
 ​
 使用case when then else过滤出所有符合条件的玩家,然后使用sum求和,计算出符合条件的玩家数目
 select sum(case when datediff(a.event_date,b.first_date)=1 then 1 else 0 end),(select count(distinct(player_id)) from activity)
 from activity a,
 (select player_id,min(event_date) first_date from activity group by player_id) b
 where a.player_id=b.player_id
 ​
 最后将符合条件的玩家数除以所有玩家数,并使用round函数,保留两位小数,并且按照例子上面的输出结果给列名取个别名即可
 select round(sum(case when datediff(a.event_date,b.first_date)=1 then 1 else 0 end)/(select count(distinct(player_id)) from activity),2) as fraction
 from activity a,
 (select player_id,min(event_date) first_date from activity group by player_id) b
 where a.player_id=b.player_id
 ​
 结果
 select round(sum(case when datediff(a.event_date,b.first_date)=1 then 1 else 0 end)/(select count(distinct(player_id)) from activity),2) as fraction
 from activity a,
 (select player_id,min(event_date) first_date from activity group by player_id) b
 where a.player_id=b.player_id

569 员工薪水中位数

 ##SQL架构
 Create table If Not Exists Employee (Id int, Company varchar(255), Salary int)
 Truncate table Employee
 insert into Employee (Id, Company, Salary) values ('1', 'A', '2341')
 insert into Employee (Id, Company, Salary) values ('2', 'A', '341')
 insert into Employee (Id, Company, Salary) values ('3', 'A', '15')
 insert into Employee (Id, Company, Salary) values ('4', 'A', '15314')
 insert into Employee (Id, Company, Salary) values ('5', 'A', '451')
 insert into Employee (Id, Company, Salary) values ('6', 'A', '513')
 insert into Employee (Id, Company, Salary) values ('7', 'B', '15')
 insert into Employee (Id, Company, Salary) values ('8', 'B', '13')
 insert into Employee (Id, Company, Salary) values ('9', 'B', '1154')
 insert into Employee (Id, Company, Salary) values ('10', 'B', '1345')
 insert into Employee (Id, Company, Salary) values ('11', 'B', '1221')
 insert into Employee (Id, Company, Salary) values ('12', 'B', '234')
 insert into Employee (Id, Company, Salary) values ('13', 'C', '2345')
 insert into Employee (Id, Company, Salary) values ('14', 'C', '2645')
 insert into Employee (Id, Company, Salary) values ('15', 'C', '2645')
 insert into Employee (Id, Company, Salary) values ('16', 'C', '2652')
 insert into Employee (Id, Company, Salary) values ('17', 'C', '65')
 ​
 Employee 表包含所有员工。Employee 表有三列:员工Id,公司名和薪水。
 +-----+------------+--------+
 |Id   | Company  
| Salary |
 +-----+------------+--------+
 |1  
| A        
| 2341   |
 |2  
| A        
| 341  
|
 |3  
| A        
| 15     |
 |4  
| A        
| 15314
|
 |5  
| A        
| 451  
|
 |6  
| A        
| 513  
|
 |7  
| B        
| 15     |
 |8  
| B        
| 13     |
 |9  
| B        
| 1154   |
 |10   | B        
| 1345   |
 |11   | B        
| 1221   |
 |12   | B        
| 234  
|
 |13   | C        
| 2345   |
 |14   | C        
| 2645   |
 |15   | C        
| 2645   |
 |16   | C        
| 2652   |
 |17   | C        
| 65     |
 +-----+------------+--------+
 请编写SQL查询来查找每个公司的薪水中位数。挑战点:你是否可以在不使用任何内置的SQL函数的情况下解决此问题。
 ​
 +-----+------------+--------+
 |Id   | Company  
| Salary |
 +-----+------------+--------+
 |5  
| A        
| 451  
|
 |6  
| A        
| 513  
|
 |12   | B        
| 234  
|
 |9  
| B        
| 1154   |
 |14   | C        
| 2645   |
 +-----+------------+--------+
 ​
 # 答案:
select Id, Company, Salary
from
(select *,
            row_number() over (partition by Company order by Salary) rnk,
            count(*) over (partition by Company) num
    from Employee) t
where rnk in (floor((num + 1) / 2), floor(num + 2) / 2);

570 至少有5名直接下属的经理

 ##SQL架构
 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', 'None')
 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')
 ​
 Employee 表包含所有员工和他们的经理。每个员工都有一个 Id,并且还有一列是经理的 Id。
 ​
 +------+----------+-----------+----------+
 |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       |
 +------+----------+-----------+----------+
 给定 Employee 表,请编写一个SQL查询来查找至少有5名直接下属的经理。对于上表,您的SQL查询应该返回:
 ​
 +-------+
 | Name
|
 +-------+
 | John
|
 +-------+
 ​
 # 答案:
 SELECT
   
Name
 FROM
   
Employee AS t1 JOIN
   
(SELECT
       
ManagerId
     FROM
       
Employee
     GROUP BY ManagerId
     HAVING COUNT(ManagerId) >= 5) AS t2
     ON t1.Id = t2.ManagerId
 ;

571 给定数字的频率查询中位数

 ##SQL架构
 Create table If Not Exists Numbers (Number int, Frequency int)
 Truncate table Numbers
 insert into Numbers (Number, Frequency) values ('0', '7')
 insert into Numbers (Number, Frequency) values ('1', '1')
 insert into Numbers (Number, Frequency) values ('2', '3')
 insert into Numbers (Number, Frequency) values ('3', '1')
 ​
 Numbers 表保存数字的值及其频率。
 ​
 +----------+-------------+
 |
Number
|
Frequency
|
 +----------+-------------|
 |  0       |  7        
|
 |  1       |  1        
|
 |  2       |  3        
|
 |  3       |  1        
|
 +----------+-------------+
 在此表中,数字为 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3,所以中位数是 (0 + 0) / 2 = 0。
 ​
 +--------+
 | median |
 +--------|
 | 0.0000 |
 +--------+
 请编写一个查询来查找所有数字的中位数并将结果命名为 median 。
 ​
 # 答案:
 select avg(number) median
from
    (select number,
         sum(frequency) over(order by number) asc_accumu,
         sum(frequency) over(order by number desc) desc_accumu,
         sum(frequency) over() total
     from Numbers) t1
where asc_accumu >= total / 2 and desc_accumu >= total / 2

574 当选者

 Create table If Not Exists Candidate (id int, Name varchar(255))
 Create table If Not Exists Vote (id int, CandidateId int)
 Truncate table Candidate
 insert into Candidate (id, Name) values ('1', 'A')
 insert into Candidate (id, Name) values ('2', 'B')
 insert into Candidate (id, Name) values ('3', 'C')
 insert into Candidate (id, Name) values ('4', 'D')
 insert into Candidate (id, Name) values ('5', 'E')
 Truncate table Vote
 insert into Vote (id, CandidateId) values ('1', '2')
 insert into Vote (id, CandidateId) values ('2', '4')
 insert into Vote (id, CandidateId) values ('3', '3')
 insert into Vote (id, CandidateId) values ('4', '2')
 insert into Vote (id, CandidateId) values ('5', '5')
 ​
 表: Candidate
 +-----+---------+
 | id
| Name  
|
 +-----+---------+
 | 1   | A       |
 | 2   | B       |
 | 3   | C       |
 | 4   | D       |
 | 5   | E       |
 +-----+---------+  
 ​
 表: Vote
 +-----+--------------+
 | id
| CandidateId
|
 +-----+--------------+
 | 1   |     2      
|
 | 2   |     4      
|
 | 3   |     3      
|
 | 4   |     2      
|
 | 5   |     5      
|
 +-----+--------------+
 id 是自动递增的主键,
 CandidateId 是 Candidate 表中的 id.
 请编写 sql 语句来找到当选者的名字,上面的例子将返回当选者 B.
 ​
 +------+
 | Name |
 +------+
 | B  
|
 +------+
 ​
 # 答案:
 获胜者是 Vote 表中出现最多次的 CandidateId。因此可以先按照 CandidateId 分组,然后按照每个分组的计数给分组排序,使用 limit 1 取第一名即可。获得第一名的 CandidateId 之后,与 Candidate 表连接即可取得获胜者的名字。
 ​
 select Name
 from (
   select CandidateId as id
   from Vote
   group by CandidateId
   order by count(id) desc
   limit 1
 ) as Winner join Candidate
 on Winner.id = Candidate.id

578 查询回答率最高的问题

 ##SQL架构
 Create table If Not Exists survey_log (id int, action varchar(255), question_id int, answer_id varchar(25), q_num int, timestamp int)
 Truncate table survey_log
 insert into survey_log (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '285', 'None', '1', '123')
 insert into survey_log (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'answer', '285', '124124', '1', '124')
 insert into survey_log (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '369', 'None', '2', '125')
 insert into survey_log (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'skip', '369', 'None', '2', '126')
 ​
 从 survey_log 表中获得回答率最高的问题,survey_log 表包含这些列:id, action, question_id, answer_id, q_num, timestamp。
 ​
 id 表示用户 id;action 有以下几种值:"show","answer","skip";当 action 值为 "answer" 时 answer_id 非空,而 action 值为 "show" 或者 "skip" 时 answer_id 为空;q_num 表示当前会话中问题的编号。
 ​
 请编写 SQL 查询来找到具有最高回答率的问题。
 示例:
 ​
 输入:
 +------+-----------+--------------+------------+-----------+------------+
 | id   | action  
| question_id
| answer_id
| q_num     | timestamp
|
 +------+-----------+--------------+------------+-----------+------------+
 | 5  
| show    
| 285        
| null       | 1         | 123      
|
 | 5  
| answer  
| 285        
| 124124     | 1         | 124      
|
 | 5  
| show    
| 369        
| null       | 2         | 125      
|
 | 5  
| skip    
| 369        
| null       | 2         | 126      
|
 +------+-----------+--------------+------------+-----------+------------+
 输出:
 +-------------+
 | survey_log
|
 +-------------+
 |    285    
|
 +-------------+
 解释:
 问题 285 的回答率为 1/1,而问题 369 回答率为 0/1,因此输出 285 。
 ​
 # 答案:
 使用了一个子查询生成临时表 tbl,但使用 if 语句后,其实可以直接计算回答率,而不需要进行子查询。最终设计的查询如下:
 ​
 ​
 select question_id as survey_log from survey_log
group by question_id
order by avg(action = 'answer') desc limit 1;

579 查询员工的累积薪水

##SQL架构
Create table If Not Exists Employee (Id int, Month int, Salary int)
Truncate table Employee
insert into Employee (Id, Month, Salary) values ('1', '1', '20')
insert into Employee (Id, Month, Salary) values ('2', '1', '20')
insert into Employee (Id, Month, Salary) values ('1', '2', '30')
insert into Employee (Id, Month, Salary) values ('2', '2', '30')
insert into Employee (Id, Month, Salary) values ('3', '2', '40')
insert into Employee (Id, Month, Salary) values ('1', '3', '40')
insert into Employee (Id, Month, Salary) values ('3', '3', '60')
insert into Employee (Id, Month, Salary) values ('1', '4', '60')
insert into Employee (Id, Month, Salary) values ('3', '4', '70')
Employee 表保存了一年内的薪水信息。
请你编写 SQL 语句,对于每个员工,查询他除最近一个月(即最大月)之外,剩下每个月的近三个月的累计薪水(不足三个月也要计算)。
结果请按 Id 升序,然后按 Month 降序显示。
 
示例:
输入:
| Id | Month | Salary |
|----|-------|--------|
| 1
| 1
| 20
|
| 2
| 1
| 20
|
| 1
| 2
| 30
|
| 2
| 2
| 30
|
| 3
| 2
| 40
|
| 1
| 3
| 40
|
| 3
| 3
| 60
|
| 1
| 4
| 60
|
| 3
| 4
| 70
|
输出:
| Id | Month | Salary |
|----|-------|--------|
| 1
| 3
| 90
|
| 1
| 2
| 50
|
| 1
| 1
| 20
|
| 2
| 1
| 20
|
| 3
| 3
| 100
|
| 3
| 2
| 40
|
解释:
员工 '1' 除去最近一个月(月份 '4'),有三个月的薪水记录:月份 '3' 薪水为 40,月份 '2' 薪水为 30,月份 '1' 薪水为 20。
所以近 3 个月的薪水累计分别为 (40 + 30 + 20) = 90,(30 + 20) = 50 和 20。
| Id | Month | Salary |
|----|-------|--------|
| 1
| 3
| 90
|
| 1
| 2
| 50
|
| 1
| 1
| 20
|
员工 '2' 除去最近的一个月(月份 '2')的话,只有月份 '1' 这一个月的薪水记录。
| Id | Month | Salary |
|----|-------|--------|
| 2
| 1
| 20
|
员工 '3' 除去最近一个月(月份 '4')后有两个月,分别为:月份 '3' 薪水为 60 和 月份 '2' 薪水为 40。所以各月的累计情况如下:
| Id | Month | Salary |
|----|-------|--------|
| 3
| 3
| 100
|
| 3
| 2
| 40
|
# 答案:
select Id, AccMonth as Month, sum(Salary) as Salary
from
(
select a.Id as Id, a.Month as AccMonth, b.Month as Month, b.Salary as Salary
from
(
select Employee.Id as Id, Employee.Month as Month
from Employee, (select Id, max(Month) as Month
from Employee
group by Id) as LastMonth
where Employee.Id = LastMonth.Id and Employee.Month != LastMonth.Month) as a
join Employee as b
on a.Id = b.Id and a.Month - b.Month <= 2 and a.Month - b.Month >= 0
) as acc
group by Id, AccMonth
order by Id, Month desc
or
SELECT t.id, t.`month`, t.salary FROM
        (SELECT id, `month`,
                 SUM(salary) over(PARTITION BY id ORDER BY `month` rows 2 PRECEDING) salary,
                 rank() over(PARTITION BY id ORDER BY `month` DESC) ranks
         FROM Employee) t
 WHERE t.ranks > 1

580 统计各专业学生人数

##SQL架构
CREATE TABLE IF NOT EXISTS student (student_id INT,student_name VARCHAR(45), gender VARCHAR(6), dept_id INT)
CREATE TABLE IF NOT EXISTS department (dept_id INT, dept_name VARCHAR(255))
Truncate table student
insert into student (student_id, student_name, gender, dept_id) values ('1', 'Jack', 'M', '1')
insert into student (student_id, student_name, gender, dept_id) values ('2', 'Jane', 'F', '1')
insert into student (student_id, student_name, gender, dept_id) values ('3', 'Mark', 'M', '2')
Truncate table department
insert into department (dept_id, dept_name) values ('1', 'Engineering')
insert into department (dept_id, dept_name) values ('2', 'Science')
insert into department (dept_id, dept_name) values ('3', 'Law')
一所大学有 2 个数据表,分别是 student 和 department ,这两个表保存着每个专业的学生数据和院系数据。
写一个查询语句,查询 department 表中每个专业的学生人数 (即使没有学生的专业也需列出)。
将你的查询结果按照学生人数降序排列。 如果有两个或两个以上专业有相同的学生数目,将这些部门按照部门名字的字典序从小到大排列。
student 表格如下:
| Column Name
| Type
|
|--------------|-----------|
| student_id
| Integer
|
| student_name | String
|
| gender
| Character |
| dept_id
| Integer
|
其中, student_id 是学生的学号, student_name 是学生的姓名, gender 是学生的性别, dept_id 是学生所属专业的专业编号。
department 表格如下:
| Column Name | Type
|
|-------------|---------|
| dept_id
| Integer |
| dept_name
| String
|
dept_id 是专业编号, dept_name 是专业名字。
这里是一个示例输入:
student 表格:
| student_id | student_name | gender | dept_id |
|------------|--------------|--------|---------|
| 1
| Jack
| M
| 1
|
| 2
| Jane
| F
| 1
|
| 3
| Mark
| M
| 2
|
department 表格:
| dept_id | dept_name
|
|---------|-------------|
| 1
| Engineering |
| 2
| Science
|
| 3
| Law
|
示例输出为:
| dept_name
| student_number |
|-------------|----------------|
| Engineering | 2
|
| Science
| 1
|
| Law
| 0
|
# 答案:
Select
dept_name
,count(distinct student_id) student_number
from
student s right join
department d on
s.dept_id = d.dept_id
group by
d.dept_id
order by
student_number desc
,dept_name

585 2016年的投资

##SQL架构
CREATE TABLE IF NOT EXISTS insurance (PID INTEGER(11), TIV_2015 NUMERIC(15,2), TIV_2016 NUMERIC(15,2), LAT NUMERIC(5,2), LON NUMERIC(5,2) )
Truncate table insurance
insert into insurance (PID, TIV_2015, TIV_2016, LAT, LON) values ('1', '10', '5', '10', '10')
insert into insurance (PID, TIV_2015, TIV_2016, LAT, LON) values ('2', '20', '20', '20', '20')
insert into insurance (PID, TIV_2015, TIV_2016, LAT, LON) values ('3', '10', '30', '20', '20')
insert into insurance (PID, TIV_2015, TIV_2016, LAT, LON) values ('4', '10', '40', '40', '40')
写一个查询语句,将 2016 年 (TIV_2016) 所有成功投资的金额加起来,保留 2 位小数。
对于一个投保人,他在 2016 年成功投资的条件是:
他在 2015 年的投保额 (TIV_2015) 至少跟一个其他投保人在 2015 年的投保额相同。
他所在的城市必须与其他投保人都不同(也就是说维度和经度不能跟其他任何一个投保人完全相同)。
输入格式:
表 insurance 格式如下:
| Column Name | Type
|
|-------------|---------------|
| PID
| INTEGER(11)
|
| TIV_2015
| NUMERIC(15,2) |
| TIV_2016
| NUMERIC(15,2) |
| LAT
| NUMERIC(5,2)
|
| LON
| NUMERIC(5,2)
|
PID 字段是投保人的投保编号, TIV_2015 是该投保人在2015年的总投保金额, TIV_2016 是该投保人在2016年的投保金额, LAT 是投保人所在城市的维度, LON 是投保人所在城市的经度。
样例输入
| PID | TIV_2015 | TIV_2016 | LAT | LON |
|-----|----------|----------|-----|-----|
| 1
| 10
| 5
| 10
| 10
|
| 2
| 20
| 20
| 20
| 20
|
| 3
| 10
| 30
| 20
| 20
|
| 4
| 10
| 40
| 40
| 40
|
样例输出
| TIV_2016 |
|----------|
| 45.00
|
解释
就如最后一个投保人,第一个投保人同时满足两个条件:
1. 他在 2015 年的投保金额 TIV_2015 为 '10' ,与第三个和第四个投保人在 2015 年的投保金额相同。
2. 他所在城市的经纬度是独一无二的。
第二个投保人两个条件都不满足。他在 2015 年的投资 TIV_2015 与其他任何投保人都不相同。
且他所在城市的经纬度与第三个投保人相同。基于同样的原因,第三个投保人投资失败。
所以返回的结果是第一个投保人和最后一个投保人的 TIV_2016 之和,结果是 45 。
# 答案:
SELECT SUM(distinct i1.TIV_2016) as TIV_2016
FROM insurance i1, insurance i2
WHERE i1.TIV_2015 = i2.TIV_2015
AND i1.PID != i2.PID
AND (i1.LAT,i1.LON) NOT IN (SELECT LAT,LON FROM insurance i WHERE i1.PID != i.PID)

602 好友申请II:谁有最多的好友

##SQL架构
SQL架构
Create table If Not Exists request_accepted ( requester_id INT NOT NULL, accepter_id INT NULL, accept_date DATE NULL)
Truncate table request_accepted
insert into request_accepted (requester_id, accepter_id, accept_date) values ('1', '2', '2016/06/03')
insert into request_accepted (requester_id, accepter_id, accept_date) values ('1', '3', '2016/06/08')
insert into request_accepted (requester_id, accepter_id, accept_date) values ('2', '3', '2016/06/08')
insert into request_accepted (requester_id, accepter_id, accept_date) values ('3', '4', '2016/06/09')
在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。
表 request_accepted 存储了所有好友申请通过的数据记录,其中, requester_id 和 accepter_id 都是用户的编号。
| requester_id | accepter_id | accept_date|
|--------------|-------------|------------|
| 1
| 2
| 2016_06-03 |
| 1
| 3
| 2016-06-08 |
| 2
| 3
| 2016-06-08 |
| 3
| 4
| 2016-06-09 |
写一个查询语句,求出谁拥有最多的好友和他拥有的好友数目。对于上面的样例数据,结果为:
| id | num |
|----|-----|
| 3
| 3
|
# 答案:
可以将 requester_id 和 accepter_id 联合起来,然后统计每个人出现的次数。
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted;
最终SQL
select ids as id, cnt as num
from
(
select ids, count(*) as cnt
from
(
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted
) as tbl1
group by ids
) as tbl2
order by cnt desc
limit 1
;

614 二级关注者

##SQL架构
Create table If Not Exists follow (followee varchar(255), follower varchar(255))
Truncate table follow
insert into follow (followee, follower) values ('A', 'B')
insert into follow (followee, follower) values ('B', 'C')
insert into follow (followee, follower) values ('B', 'D')
insert into follow (followee, follower) values ('D', 'E')
在 facebook 中,表 follow 会有 2 个字段: followee, follower ,分别表示被关注者和关注者。
请写一个 sql 查询语句,对每一个关注者,查询关注他的关注者的数目。
比方说:
+-------------+------------+
| followee
| follower
|
+-------------+------------+
|
A
|
B
|
|
B
|
C
|
|
B
|
D
|
|
D
|
E
|
+-------------+------------+
应该输出:
+-------------+------------+
| follower
| num
|
+-------------+------------+
|
B
|
2
|
|
D
|
1
|
+-------------+------------+
解释:
B 和 D 都在在 follower 字段中出现,作为被关注者,B 被 C 和 D 关注,D 被 E 关注。A 不在 follower 字段内,所以A不在输出列表中。
 
注意:
被关注者永远不会被他 / 她自己关注。
将结果按照字典序返回。
# 答案:
select
f.followee follower,
count(distinct followee) as num
from
follow f
where f.followee in (
select f1.follower
from follow f1
)
group by f.follwee
order by f.follwee
;

615 平均工资:部门与公司比较

##SQL架构
Create table If Not Exists salary (id int, employee_id int, amount int, pay_date date)
Create table If Not Exists employee (employee_id int, department_id int)
Truncate table salary
insert into salary (id, employee_id, amount, pay_date) values ('1', '1', '9000', '2017/03/31')
insert into salary (id, employee_id, amount, pay_date) values ('2', '2', '6000', '2017/03/31')
insert into salary (id, employee_id, amount, pay_date) values ('3', '3', '10000', '2017/03/31')
insert into salary (id, employee_id, amount, pay_date) values ('4', '1', '7000', '2017/02/28')
insert into salary (id, employee_id, amount, pay_date) values ('5', '2', '6000', '2017/02/28')
insert into salary (id, employee_id, amount, pay_date) values ('6', '3', '8000', '2017/02/28')
Truncate table employee
insert into employee (employee_id, department_id) values ('1', '1')
insert into employee (employee_id, department_id) values ('2', '2')
insert into employee (employee_id, department_id) values ('3', '2')
给如下两个表,写一个查询语句,求出在每一个工资发放日,每个部门的平均工资与公司的平均工资的比较结果 (高 / 低 / 相同)。
 
表: salary
| id | employee_id | amount | pay_date
|
|----|-------------|--------|------------|
| 1
| 1
| 9000
| 2017-03-31 |
| 2
| 2
| 6000
| 2017-03-31 |
| 3
| 3
| 10000
| 2017-03-31 |
| 4
| 1
| 7000
| 2017-02-28 |
| 5
| 2
| 6000
| 2017-02-28 |
| 6
| 3
| 8000
| 2017-02-28 |
 
employee_id 字段是表 employee 中 employee_id 字段的外键。
 
| employee_id | department_id |
|-------------|---------------|
| 1
| 1
|
| 2
| 2
|
| 3
| 2
|
 
对于如上样例数据,结果为:
| pay_month | department_id | comparison
|
|-----------|---------------|-------------|
| 2017-03
| 1
| higher
|
| 2017-03
| 2
| lower
|
| 2017-02
| 1
| same
|
| 2017-02
| 2
| same
|
 
解释
在三月,公司的平均工资是 (9000+6000+10000)/3 = 8333.33...
由于部门 '1' 里只有一个 employee_id 为 '1' 的员工,所以部门 '1' 的平均工资就是此人的工资 9000 。因为 9000 > 8333.33 ,所以比较结果是 'higher'。
 
第二个部门的平均工资为 employee_id 为 '2' 和 '3' 两个人的平均工资,为 (6000+10000)/2=8000 。因为 8000 < 8333.33 ,所以比较结果是 'lower' 。
 
在二月用同样的公式求平均工资并比较,比较结果为 'same' ,因为部门 '1' 和部门 '2' 的平均工资与公司的平均工资相同,都是 7000 。
# 答案:
1、计算公司每个月的平均工资
select avg(amount) as company_avg,
date_format(pay_date, '%Y-%m') as pay_month
from salary
group by date_format(pay_date, '%Y-%m')
;
2、计算每个部门每个月的平均工资
select department_id, avg(amount) as department_avg, date_format(pay_date, '%Y-%m') as pay_month
from salary
join employee on salary.employee_id = employee.employee_id
group by department_id, pay_month
;
3、将 2 中的表和之前的公司数据作比较并求出结果
select department_salary.pay_month, department_id,
case
when department_avg>company_avg then 'higher'
when department_avg<company_avg then 'lower'
else 'same'
end as comparison
from
(
select department_id, avg(amount) as department_avg, date_format(pay_date, '%Y-%m') as pay_month
from salary join employee on salary.employee_id = employee.employee_id
group by department_id, pay_month
) as department_salary
join
(
select avg(amount) as company_avg,
date_format(pay_date, '%Y-%m') as pay_month from salary group by date_format(pay_date, '%Y-%m')
) as company_salary
on department_salary.pay_month = company_salary.pay_month
;

618 学生地理信息报告

##SQL架构
Create table If Not Exists student (name varchar(50), continent varchar(7))
Truncate table student
insert into student (name, continent) values ('Jane', 'America')
insert into student (name, continent) values ('Pascal', 'Europe')
insert into student (name, continent) values ('Xi', 'Asia')
insert into student (name, continent) values ('Jack', 'America')
一所美国大学有来自亚洲、欧洲和美洲的学生,他们的地理信息存放在如下 student 表中。
| name
| continent |
|--------|-----------|
| Jack
| America
|
| Pascal | Europe
|
| Xi
| Asia
|
| Jane
| America
|
 
写一个查询语句实现对大洲(continent)列的 透视表 操作,使得每个学生按照姓名的字母顺序依次排列在对应的大洲下面。输出的标题应依次为美洲(America)、亚洲(Asia)和欧洲(Europe)。
 
对于样例输入,它的对应输出是:
| America | Asia | Europe |
|---------|------|--------|
| Jack
| Xi
| Pascal |
| Jane
|
|
|
 
进阶:如果不能确定哪个大洲的学生数最多,你可以写出一个查询去生成上述学生报告吗?
# 答案:
SELECT
America, Asia, Europe
FROM
(SELECT @as:=0, @am:=0, @eu:=0) t,
(SELECT
@as:=@as + 1 AS asid, NAME AS Asia
FROM
student
WHERE
continent = 'Asia'
ORDER BY Asia) AS t1
RIGHT JOIN
(SELECT
@am:=@am + 1 AS amid, NAME AS America
FROM
student
WHERE
continent = 'America'
ORDER BY America) AS t2 ON asid = amid
LEFT JOIN
(SELECT
@eu:=@eu + 1 AS euid, NAME AS Europe
FROM
student
WHERE
continent = 'Europe'
ORDER BY Europe) AS t3 ON amid = euid
;
或者
select
    max(case when continent = 'America' then name end) as America,
    max(case when continent = 'Asia' then name end) as Asia,
    max(case when continent = 'Europe' then name end) as Europe
from
(
    select *, row_number() over (partition by continent order by name) rk
    from student
) t
group by rk

1045 买下所有产品的客户

##SQL架构
Create table If Not Exists Customer (customer_id int, product_key int)
Create table Product (product_key int)
Truncate table Customer
insert into Customer (customer_id, product_key) values ('1', '5')
insert into Customer (customer_id, product_key) values ('2', '6')
insert into Customer (customer_id, product_key) values ('3', '5')
insert into Customer (customer_id, product_key) values ('3', '6')
insert into Customer (customer_id, product_key) values ('1', '6')
Truncate table Product
insert into Product (product_key) values ('5')
insert into Product (product_key) values ('6')
Customer 表:
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| customer_id | int
|
| product_key | int
|
+-------------+---------+
product_key 是 Customer 表的外键。
Product 表:
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| product_key | int
|
+-------------+---------+
product_key 是这张表的主键。
 
写一条 SQL 查询语句,从 Customer 表中查询购买了 Product 表中所有产品的客户的 id。
示例:
Customer 表:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1
| 5
|
| 2
| 6
|
| 3
| 5
|
| 3
| 6
|
| 1
| 6
|
+-------------+-------------+
Product 表:
+-------------+
| product_key |
+-------------+
| 5
|
| 6
|
+-------------+
Result 表:
+-------------+
| customer_id |
+-------------+
| 1
|
| 3
|
+-------------+
购买了所有产品(5 和 6)的客户的 id 是 1 和 3 。
# 答案:
select
customer_id
from
Customer
group by customer_id
having count(distinct product_key) in
(
select count(distinct product_key) from Product
);

1070 产品销售分析III

##SQL架构
Create table Sales (sale_id int, product_id int, year int, quantity int, price int)
Create table 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')
销售表 Sales:
+-------------+-------+
| Column Name | Type
|
+-------------+-------+
| sale_id
| int
|
| product_id
| int
|
| year
| int
|
| quantity
| int
|
| price
| int
|
+-------------+-------+
sale_id 是此表的主键。
product_id 是产品表的外键。
请注意,价格是按每单位计的。
产品表 Product:
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| product_id
| int
|
| product_name | varchar |
+--------------+---------+
product_id 是此表的主键。
 
编写一个 SQL 查询,选出每个销售产品的 第一年 的 产品 id、年份、数量 和 价格。
查询结果格式如下:
Sales table:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1
| 100
| 2008 | 10
| 5000
|
| 2
| 100
| 2009 | 12
| 5000
|
| 7
| 200
| 2011 | 15
| 9000
|
+---------+------------+------+----------+-------+
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100
| Nokia
|
| 200
| Apple
|
| 300
| Samsung
|
+------------+--------------+
Result table:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100
| 2008
| 10
| 5000
|
| 200
| 2011
| 15
| 9000
|
+------------+------------+----------+-------+
# 答案:
select
product_id,
year first_year,
quantity, price
from sales
where (product_id, year) in
(
select
product_id,
min(year)
from sales
group by product_id)
;
推荐使用窗口函数
SELECT product_id, year AS first_year, quantity, price
FROM (
SELECT *, RANK() OVER(PARTITION BY product_id ORDER BY year) AS year_rnk
FROM Sales
) AS temp
WHERE year_rnk = 1

1077 项目员工

##SQL架构
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', '3')
insert into Employee (employee_id, name, experience_years) values ('4', 'Doe', '2')
项目表 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
| 3
|
| 4
| Doe
| 2
|
+-------------+--------+------------------+
Result 表:
+-------------+---------------+
| project_id
| employee_id
|
+-------------+---------------+
| 1
| 1
|
| 1
| 3
|
| 2
| 1
|
+-------------+---------------+
employee_id 为 1 和 3 的员工在 project_id 为 1 的项目中拥有最丰富的经验。在 project_id 为 2 的项目中,employee_id 为 1 的员工拥有最丰富的经验。
解法一:
with tmp as (
select
p.project_id,
p.employee_id,
e.experience_years
from project p
join employee e on
p.employee_id = e.employee_id
)
select project_id, employee_id
from tmp
where (project_id, experience_years) in (
select project_id, max(experience_years)
from tmp
group by project_id
);
解法二:
select project_id, employee_id
from (
select
p.project_id,
p.employee_id,
e.experience_years,
rank() over(
partition by p.project_id
order by e.experience_years desc
) as rnk
from project p
join employee e on
p.employee_id = e.employee_id
) tmp
where rnk=1;

1083 销售分析II

##SQL架构
Create table If Not Exists Product (product_id int, product_name varchar(10), unit_price int)
Create table If Not Exists Sales (seller_id int, product_id int, buyer_id int, sale_date date, quantity int, price int)
Truncate table Product
insert into Product (product_id, product_name, unit_price) values ('1', 'S8', '1000')
insert into Product (product_id, product_name, unit_price) values ('2', 'G4', '800')
insert into Product (product_id, product_name, unit_price) values ('3', 'iPhone', '1400')
Truncate table Sales
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '1', '1', '2019-01-21', '2', '2000')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '2', '2', '2019-02-17', '1', '800')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('2', '1', '3', '2019-06-02', '1', '800')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('3', '3', '3', '2019-05-13', '2', '2800')
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 查询,查询购买了 S8 手机却没有购买 iPhone 的买家。注意这里 S8 和 iPhone 是 Product 表中的产品。
查询结果格式如下图表示:
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
| 1
| 3
| 2019-06-02 | 1
| 800
|
| 3
| 3
| 3
| 2019-05-13 | 2
| 2800
|
+-----------+------------+----------+------------+----------+-------+
Result table:
+-------------+
| buyer_id
|
+-------------+
| 1
|
+-------------+
id 为 1 的买家购买了一部 S8,但是却没有购买 iPhone,而 id 为 3 的买家却同时购买了这 2 部手机。
# 答案:
SELECT S.buyer_id
FROM Sales S JOIN Product P
ON S.product_id = P.product_id
GROUP BY S.buyer_id
HAVING COUNT(IF(P.product_name = 'S8',TRUE, NULL)) > 0 AND COUNT(IF(P.product_name = 'iPhone',TRUE, NULL)) = 0

1097 游戏玩法分析V

##SQL架构
Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-01', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')
Activity 活动记录表
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| player_id
| int
|
| device_id
| int
|
| event_date
| date
|
| games_played | int
|
+--------------+---------+
(player_id,event_date)是此表的主键
这张表显示了某些游戏的玩家的活动情况
每一行表示一个玩家的记录,在某一天使用某个设备注销之前,登录并玩了很多游戏(可能是 0)
 
玩家的 安装日期 定义为该玩家的第一个登录日。
玩家的 第一天留存率 定义为:假定安装日期为 X 的玩家的数量为 N ,其中在 X 之后的一天重新登录的玩家数量为 M ,M/N 就是第一天留存率,四舍五入到小数点后两位。
编写一个 SQL 查询,报告所有安装日期、当天安装游戏的玩家数量和玩家的第一天留存率。
 
查询结果格式如下所示:
Activity 表:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1
| 2
| 2016-03-01 | 5
|
| 1
| 2
| 2016-03-02 | 6
|
| 2
| 3
| 2017-06-25 | 1
|
| 3
| 1
| 2016-03-01 | 0
|
| 3
| 4
| 2016-07-03 | 5
|
+-----------+-----------+------------+--------------+
Result 表:
+------------+----------+----------------+
| install_dt | installs | Day1_retention |
+------------+----------+----------------+
| 2016-03-01 | 2
| 0.50
|
| 2017-06-25 | 1
| 0.00
|
+------------+----------+----------------+
玩家 1 和 3 在 2016-03-01 安装了游戏,但只有玩家 1 在 2016-03-02 重新登录,所以 2016-03-01 的第一天留存率是 1/2=0.50
玩家 2 在 2017-06-25 安装了游戏,但在 2017-06-26 没有重新登录,因此 2017-06-25 的第一天留存率为 0/1=0.00
# 答案:
select a1.event_date as install_dt, count(*) as installs,
round(avg(a2.event_date is not null),2) as Day1_retention
from (select player_id, min(event_date) as event_date
from activity
group by player_id) a1
left join activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date,a1.event_date)=1
group by a1.event_date
-- 用窗口函数选出注册日期(最小记录日期)
-- 直接对 当前日期与注册日期的差值 为1的用户进行计数/所有的用户 ,按注册日期进行分组
-- is not null的判断 , 字段值为null的话为0, 字段值不为null的话 返回 1 然后对所有参加计算的数为分母求比例。
select
first_date as install_dt,
count(distinct player_id) as installs,
round(sum(if(datediff(event_date,first_date)=1,1,0))/count(distinct player_id),2)
as Day1_retention
from(select
player_id
,event_date
,min(event_date) over(partition by player_id) as first_date
from activity)tm
group by first_date
;

1098 小众书籍

##SQL架构
Create table If Not Exists Books (book_id int, name varchar(50), available_from date)
Create table If Not Exists Orders (order_id int, book_id int, quantity int, dispatch_date date)
Truncate table Books
insert into Books (book_id, name, available_from) values ('1', 'Kalila And Demna', '2010-01-01')
insert into Books (book_id, name, available_from) values ('2', '28 Letters', '2012-05-12')
insert into Books (book_id, name, available_from) values ('3', 'The Hobbit', '2019-06-10')
insert into Books (book_id, name, available_from) values ('4', '13 Reasons Why', '2019-06-01')
insert into Books (book_id, name, available_from) values ('5', 'The Hunger Games', '2008-09-21')
Truncate table Orders
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('1', '1', '2', '2018-07-26')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('2', '1', '1', '2018-11-05')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('3', '3', '8', '2019-06-11')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('4', '4', '6', '2019-06-05')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('5', '4', '5', '2019-06-20')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('6', '5', '9', '2009-02-02')
insert into Orders (order_id, book_id, quantity, dispatch_date) values ('7', '5', '8', '2010-04-13')
书籍表 Books:
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| book_id
| int
|
| name
| varchar |
| available_from | date
|
+----------------+---------+
book_id 是这个表的主键。
订单表 Orders:
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| order_id
| int
|
| book_id
| int
|
| quantity
| int
|
| dispatch_date
| date
|
+----------------+---------+
order_id 是这个表的主键。
book_id
是 Books 表的外键。
 
你需要写一段 SQL 命令,筛选出过去一年中订单总量 少于10本 的 书籍 。
注意:不考虑 上架(available from)距今 不满一个月 的书籍。并且 假设今天是 2019-06-23 。
 
下面是样例输出结果:
Books 表:
+---------+--------------------+----------------+
| book_id | name
| available_from |
+---------+--------------------+----------------+
| 1
| "Kalila And Demna" | 2010-01-01
|
| 2
| "28 Letters"
| 2012-05-12
|
| 3
| "The Hobbit"
| 2019-06-10
|
| 4
| "13 Reasons Why"
| 2019-06-01
|
| 5
| "The Hunger Games" | 2008-09-21
|
+---------+--------------------+----------------+
Orders 表:
+----------+---------+----------+---------------+
| order_id | book_id | quantity | dispatch_date |
+----------+---------+----------+---------------+
| 1
| 1
| 2
| 2018-07-26
|
| 2
| 1
| 1
| 2018-11-05
|
| 3
| 3
| 8
| 2019-06-11
|
| 4
| 4
| 6
| 2019-06-05
|
| 5
| 4
| 5
| 2019-06-20
|
| 6
| 5
| 9
| 2009-02-02
|
| 7
| 5
| 8
| 2010-04-13
|
+----------+---------+----------+---------------+
Result 表:
+-----------+--------------------+
| book_id
| name
|
+-----------+--------------------+
| 1
| "Kalila And Demna" |
| 2
| "28 Letters"
|
| 5
| "The Hunger Games" |
+-----------+--------------------+
# 答案:
select b.book_id, name
from books b left join orders o
on b.book_id = o.book_id and dispatch_date >= '2018-06-23'
where available_from < '2019-05-23'
group by b.book_id
having ifnull(sum(quantity), 0) < 10;

1107 每日新用户统计

##SQL架构
Create table If Not Exists Traffic (user_id int, activity ENUM('login', 'logout', 'jobs', 'groups', 'homepage'), activity_date date)
Truncate table Traffic
insert into Traffic (user_id, activity, activity_date) values ('1', 'login', '2019-05-01')
insert into Traffic (user_id, activity, activity_date) values ('1', 'homepage', '2019-05-01')
insert into Traffic (user_id, activity, activity_date) values ('1', 'logout', '2019-05-01')
insert into Traffic (user_id, activity, activity_date) values ('2', 'login', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('2', 'logout', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('3', 'login', '2019-01-01')
insert into Traffic (user_id, activity, activity_date) values ('3', 'jobs', '2019-01-01')
insert into Traffic (user_id, activity, activity_date) values ('3', 'logout', '2019-01-01')
insert into Traffic (user_id, activity, activity_date) values ('4', 'login', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('4', 'groups', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('4', 'logout', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('5', 'login', '2019-03-01')
insert into Traffic (user_id, activity, activity_date) values ('5', 'logout', '2019-03-01')
insert into Traffic (user_id, activity, activity_date) values ('5', 'login', '2019-06-21')
insert into Traffic (user_id, activity, activity_date) values ('5', 'logout', '2019-06-21')
Traffic 表:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user_id
| int
|
| activity
| enum
|
| activity_date | date
|
+---------------+---------+
该表没有主键,它可能有重复的行。
activity 列是 ENUM 类型,可能取 ('login', 'logout', 'jobs', 'groups', 'homepage') 几个值之一。
 
编写一个 SQL 查询,以查询从今天起最多 90 天内,每个日期该日期首次登录的用户数。假设今天是 2019-06-30.
查询结果格式如下例所示:
Traffic 表:
+---------+----------+---------------+
| user_id | activity | activity_date |
+---------+----------+---------------+
| 1
| login
| 2019-05-01
|
| 1
| homepage | 2019-05-01
|
| 1
| logout
| 2019-05-01
|
| 2
| login
| 2019-06-21
|
| 2
| logout
| 2019-06-21
|
| 3
| login
| 2019-01-01
|
| 3
| jobs
| 2019-01-01
|
| 3
| logout
| 2019-01-01
|
| 4
| login
| 2019-06-21
|
| 4
| groups
| 2019-06-21
|
| 4
| logout
| 2019-06-21
|
| 5
| login
| 2019-03-01
|
| 5
| logout
| 2019-03-01
|
| 5
| login
| 2019-06-21
|
| 5
| logout
| 2019-06-21
|
+---------+----------+---------------+
Result 表:
+------------+-------------+
| login_date | user_count
|
+------------+-------------+
| 2019-05-01 | 1
|
| 2019-06-21 | 2
|
+------------+-------------+
请注意,我们只关心用户数非零的日期.
ID 为 5 的用户第一次登陆于 2019-03-01,因此他不算在 2019-06-21 的的统计内。
# 答案:
1、先找出所有用户第一次登录的信息
2、筛选出距今90天内的信息
3、按照日期聚合计数
select first_date login_date, count(user_id) user_count
from
(select user_id, activity, min(activity_date) first_date from Traffic
where activity = 'login' group by user_id) t1
where datediff("2019-06-30", first_date) <= 90
group by first_date

1112 每位学生的最高成绩

Create table If Not Exists Enrollments (student_id int, course_id int, grade int)
Truncate table Enrollments
insert into Enrollments (student_id, course_id, grade) values ('2', '2', '95')
insert into Enrollments (student_id, course_id, grade) values ('2', '3', '95')
insert into Enrollments (student_id, course_id, grade) values ('1', '1', '90')
insert into Enrollments (student_id, course_id, grade) values ('1', '2', '99')
insert into Enrollments (student_id, course_id, grade) values ('3', '1', '80')
insert into Enrollments (student_id, course_id, grade) values ('3', '2', '75')
insert into Enrollments (student_id, course_id, grade) values ('3', '3', '82')
表:Enrollments
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| student_id
| int
|
| course_id
| int
|
| grade
| int
|
+---------------+---------+
(student_id, course_id) 是该表的主键。
 
编写一个 SQL 查询,查询每位学生获得的最高成绩和它所对应的科目,若科目成绩并列,取 course_id 最小的一门。查询结果需按 student_id 增序进行排序。
查询结果格式如下所示:
Enrollments 表:
+------------+-------------------+
| student_id | course_id | grade |
+------------+-----------+-------+
| 2
| 2
| 95
|
| 2
| 3
| 95
|
| 1
| 1
| 90
|
| 1
| 2
| 99
|
| 3
| 1
| 80
|
| 3
| 2
| 75
|
| 3
| 3
| 82
|
+------------+-----------+-------+
Result 表:
+------------+-------------------+
| student_id | course_id | grade |
+------------+-----------+-------+
| 1
| 2
| 99
|
| 2
| 2
| 95
|
| 3
| 3
| 82
|
+------------+-----------+-------+
# 答案:
1、窗口函数
select
t1.student_id,
t1.course_id,
t1.grade
from (
select *, rank() over(parititon by student_id order by grade desc, course_id) as r
from Enrollments
) t1
where t1.r = 1
2、子查询
select
student_id,
min(course_id) as course_id,
grade
from Enrollments
where (student_id, grade) in
(select student_id, max(grade) from Enrollments group by student_id)
group by student_id
order by student_id;

1126 查询活跃业务

##SQL架构
Create table If Not Exists Events (business_id int, event_type varchar(10), occurences int)
Truncate table Events
insert into Events (business_id, event_type, occurences) values ('1', 'reviews', '7')
insert into Events (business_id, event_type, occurences) values ('3', 'reviews', '3')
insert into Events (business_id, event_type, occurences) values ('1', 'ads', '11')
insert into Events (business_id, event_type, occurences) values ('2', 'ads', '7')
insert into Events (business_id, event_type, occurences) values ('3', 'ads', '6')
insert into Events (business_id, event_type, occurences) values ('1', 'page views', '3')
insert into Events (business_id, event_type, occurences) values ('2', 'page views', '12')
事件表:Events
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| business_id
| int
|
| event_type
| varchar |
| occurences
| int
|
+---------------+---------+
此表的主键是 (business_id, event_type)。
表中的每一行记录了某种类型的事件在某些业务中多次发生的信息。
 
写一段 SQL 来查询所有活跃的业务。
如果一个业务的某个事件类型的发生次数大于此事件类型在所有业务中的平均发生次数,并且该业务至少有两个这样的事件类型,那么该业务就可被看做是活跃业务。
查询结果格式如下所示:
Events table:
+-------------+------------+------------+
| business_id | event_type | occurences |
+-------------+------------+------------+
| 1
| reviews
| 7
|
| 3
| reviews
| 3
|
| 1
| ads
| 11
|
| 2
| ads
| 7
|
| 3
| ads
| 6
|
| 1
| page views | 3
|
| 2
| page views | 12
|
+-------------+------------+------------+
结果表
+-------------+
| business_id |
+-------------+
| 1
|
+-------------+
'reviews'、 'ads' 和 'page views' 的总平均发生次数分别是 (7+3)/2=5, (11+7+6)/3=8, (3+12)/2=7.5。
id 为 1 的业务有 7 个 'reviews' 事件(大于 5)和 11 个 'ads' 事件(大于 8),所以它是活跃业务。
# 答案:
select businessid
from Events as e
join (
select event_type, avg(occurences) as eventAvg
from Events
group by event_type
) as e1
on e.event_type = e1.event_type
where e.occurences > e1.eventAvg;

1127 用户购买平台

##SQL架构
Create table If Not Exists Spending (user_id int, spend_date date, platform ENUM('desktop', 'mobile'), amount int)
Truncate table Spending
insert into Spending (user_id, spend_date, platform, amount) values ('1', '2019-07-01', 'mobile', '100')
insert into Spending (user_id, spend_date, platform, amount) values ('1', '2019-07-01', 'desktop', '100')
insert into Spending (user_id, spend_date, platform, amount) values ('2', '2019-07-01', 'mobile', '100')
insert into Spending (user_id, spend_date, platform, amount) values ('2', '2019-07-02', 'mobile', '100')
insert into Spending (user_id, spend_date, platform, amount) values ('3', '2019-07-01', 'desktop', '100')
insert into Spending (user_id, spend_date, platform, amount) values ('3', '2019-07-02', 'desktop', '100')
支出表: Spending
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| user_id
| int
|
| spend_date
| date
|
| platform
| enum
|
| amount
| int
|
+-------------+---------+
这张表记录了用户在一个在线购物网站的支出历史,该在线购物平台同时拥有桌面端('desktop')和手机端('mobile')的应用程序。
这张表的主键是 (user_id, spend_date, platform)。
平台列 platform 是一种 ENUM ,类型为('desktop', 'mobile')。
 
写一段 SQL 来查找每天 仅 使用手机端用户、仅 使用桌面端用户和 同时 使用桌面端和手机端的用户人数和总支出金额。
查询结果格式如下例所示:
Spending table:
+---------+------------+----------+--------+
| user_id | spend_date | platform | amount |
+---------+------------+----------+--------+
| 1
| 2019-07-01 | mobile
| 100
|
| 1
| 2019-07-01 | desktop
| 100
|
| 2
| 2019-07-01 | mobile
| 100
|
| 2
| 2019-07-02 | mobile
| 100
|
| 3
| 2019-07-01 | desktop
| 100
|
| 3
| 2019-07-02 | desktop
| 100
|
+---------+------------+----------+--------+
Result table:
+------------+----------+--------------+-------------+
| spend_date | platform | total_amount | total_users |
+------------+----------+--------------+-------------+
| 2019-07-01 | desktop
| 100
| 1
|
| 2019-07-01 | mobile
| 100
| 1
|
| 2019-07-01 | both
| 200
| 1
|
| 2019-07-02 | desktop
| 100
| 1
|
| 2019-07-02 | mobile
| 100
| 1
|
| 2019-07-02 | both
| 0
| 0
|
+------------+----------+--------------+-------------+
在 2019-07-01, 用户1 同时 使用桌面端和手机端购买, 用户2 仅 使用了手机端购买,而用户3 仅 使用了桌面端购买。
在 2019-07-02, 用户2 仅 使用了手机端购买, 用户3 仅 使用了桌面端购买,且没有用户 同时 使用桌面端和手机端购买。
# 答案:
select
spend_date,
b.platform,
sum(if(a.platform = b.platform, amount, 0)) as total_amount,
count(if(a.platform = b.platform, 1, null)) as total_users
from(
select
spend_date,
user_id,
if(count(distinct platform) = 2, 'both', platform) as platform,
sum(amount) as amount
from spending
group by user_id, spend_date
) a,(
select 'desktop' as platform union
select 'mobile' as platform union
select 'both' as platform
) b
group by spend_date, platform;

1132 报告的记录II

##SQL架构
Create table If Not Exists Actions (user_id int, post_id int, action_date date, action ENUM('view', 'like', 'reaction', 'comment', 'report', 'share'), extra varchar(10))
create table if not exists Removals (post_id int, remove_date date)
Truncate table Actions
insert into Actions (user_id, post_id, action_date, action, extra) values ('1', '1', '2019-07-01', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('1', '1', '2019-07-01', 'like', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('1', '1', '2019-07-01', 'share', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('2', '2', '2019-07-04', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('2', '2', '2019-07-04', 'report', 'spam')
insert into Actions (user_id, post_id, action_date, action, extra) values ('3', '4', '2019-07-04', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('3', '4', '2019-07-04', 'report', 'spam')
insert into Actions (user_id, post_id, action_date, action, extra) values ('4', '3', '2019-07-02', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('4', '3', '2019-07-02', 'report', 'spam')
insert into Actions (user_id, post_id, action_date, action, extra) values ('5', '2', '2019-07-03', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('5', '2', '2019-07-03', 'report', 'racism')
insert into Actions (user_id, post_id, action_date, action, extra) values ('5', '5', '2019-07-03', 'view', 'None')
insert into Actions (user_id, post_id, action_date, action, extra) values ('5', '5', '2019-07-03', 'report', 'racism')
Truncate table Removals
insert into Removals (post_id, remove_date) values ('2', '2019-07-20')
insert into Removals (post_id, remove_date) values ('3', '2019-07-18')
动作表: Actions
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user_id
| int
|
| post_id
| int
|
| action_date
| date
|
| action
| enum
|
| extra
| varchar |
+---------------+---------+
这张表没有主键,并有可能存在重复的行。
action 列的类型是 ENUM,可能的值为 ('view', 'like', 'reaction', 'comment', 'report', 'share')。
extra 列拥有一些可选信息,例如:报告理由(a reason for report)或反应类型(a type of reaction)等。
移除表: Removals
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| post_id
| int
|
| remove_date
| date
|
+---------------+---------+
这张表的主键是 post_id。
这张表的每一行表示一个被移除的帖子,原因可能是由于被举报或被管理员审查。
 
编写一段 SQL 来查找:在被报告为垃圾广告的帖子中,被移除的帖子的每日平均占比,四舍五入到小数点后 2 位。
查询结果的格式如下:
Actions table:
+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra
|
+---------+---------+-------------+--------+--------+
| 1
| 1
| 2019-07-01
| view
| null
|
| 1
| 1
| 2019-07-01
| like
| null
|
| 1
| 1
| 2019-07-01
| share
| null
|
| 2
| 2
| 2019-07-04
| view
| null
|
| 2
| 2
| 2019-07-04
| report | spam
|
| 3
| 4
| 2019-07-04
| view
| null
|
| 3
| 4
| 2019-07-04
| report | spam
|
| 4
| 3
| 2019-07-02
| view
| null
|
| 4
| 3
| 2019-07-02
| report | spam
|
| 5
| 2
| 2019-07-03
| view
| null
|
| 5
| 2
| 2019-07-03
| report | racism |
| 5
| 5
| 2019-07-03
| view
| null
|
| 5
| 5
| 2019-07-03
| report | racism |
+---------+---------+-------------+--------+--------+
Removals table:
+---------+-------------+
| post_id | remove_date |
+---------+-------------+
| 2
| 2019-07-20
|
| 3
| 2019-07-18
|
+---------+-------------+
Result table:
+-----------------------+
| average_daily_percent |
+-----------------------+
| 75.00
|
+-----------------------+
2019-07-04 的垃圾广告移除率是 50%,因为有两张帖子被报告为垃圾广告,但只有一个得到移除。
2019-07-02 的垃圾广告移除率是 100%,因为有一张帖子被举报为垃圾广告并得到移除。
其余几天没有收到垃圾广告的举报,因此平均值为:(50 + 100) / 2 = 75%
注意,输出仅需要一个平均值即可,我们并不关注移除操作的日期。
# 答案:
select round(avg(proportion) * 100, 2) as average_daily_percent
from (
select actions.action_date, count(distinct removals.post_id) / count(distinct actions.post_id) as proportion
from actions
left join removals
on actions.post_id = removals.post_id
where extra = 'spam'
group by actions.action_date
) a

1141 查询近30天活跃用户数

##SQL架构
Create table If Not Exists Activity (user_id int, session_id int, activity_date date, activity_type ENUM('open_session', 'end_session', 'scroll_down', 'send_message'))
Truncate table Activity
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'open_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'scroll_down')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'end_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-20', 'open_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'send_message')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'end_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'open_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'send_message')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'end_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'open_session')
insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'end_session')
活动记录表:Activity
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user_id
| int
|
| session_id
| int
|
| activity_date | date
|
| activity_type | enum
|
+---------------+---------+
该表是用户在社交网站的活动记录。
该表没有主键,可能包含重复数据。
activity_type 字段为以下四种值 ('open_session', 'end_session', 'scroll_down', 'send_message')。
每个 session_id 只属于一个用户。
 
请写SQL查询出截至 2019-07-27(包含2019-07-27),近 30天的每日活跃用户数(当天只要有一条活动记录,即为活跃用户)。
查询结果示例如下:
Activity table:
+---------+------------+---------------+---------------+
| user_id | session_id | activity_date | activity_type |
+---------+------------+---------------+---------------+
| 1
| 1
| 2019-07-20
| open_session
|
| 1
| 1
| 2019-07-20
| scroll_down
|
| 1
| 1
| 2019-07-20
| end_session
|
| 2
| 4
| 2019-07-20
| open_session
|
| 2
| 4
| 2019-07-21
| send_message
|
| 2
| 4
| 2019-07-21
| end_session
|
| 3
| 2
| 2019-07-21
| open_session
|
| 3
| 2
| 2019-07-21
| send_message
|
| 3
| 2
| 2019-07-21
| end_session
|
| 4
| 3
| 2019-06-25
| open_session
|
| 4
| 3
| 2019-06-25
| end_session
|
+---------+------------+---------------+---------------+
Result table:
+------------+--------------+
| day
| active_users |
+------------+--------------+
| 2019-07-20 | 2
|
| 2019-07-21 | 2
|
+------------+--------------+
非活跃用户的记录不需要展示。
# 答案:
方法一:GROUP BY 和 DISTINCT
思路
使用 COUNT 函数计算用户的数量。因为该表没有主键,可能包含重复数据,所以需要在此基础上使用 DISTINCT 去重:COUNT(DISTINCT user_id)。
统计截至 2019-07-27,近 30 天的每日活跃用户,所以需要使用 WHERE 过滤数据,可以使用两种办法(注意是截至不是截止):
计算出第一天,使用 BETWEEN :WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'。
使用 datediff() 函数,计算当天与最后一天的差值:WHERE datediff('2019-07-27',activity_date) < 30。
使用 GROUP BY 按天聚合。
select activity_date as day, count(distinct user_id) as active_users
from activity
where datediff('2019-07-27', activity_date) < 30
group by activity_date;

1158 市场分析I

##SQL架构
Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10))
Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)
Create table If Not Exists Items (item_id int, item_brand varchar(10))
Truncate table Users
insert into Users (user_id, join_date, favorite_brand) values ('1', '2018-01-01', 'Lenovo')
insert into Users (user_id, join_date, favorite_brand) values ('2', '2018-02-09', 'Samsung')
insert into Users (user_id, join_date, favorite_brand) values ('3', '2018-01-19', 'LG')
insert into Users (user_id, join_date, favorite_brand) values ('4', '2018-05-21', 'HP')
Truncate table Orders
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2018-08-02', '2', '1', '3')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2018-08-04', '1', '4', '2')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2018-08-04', '1', '3', '4')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4')
Truncate table Items
insert into Items (item_id, item_brand) values ('1', 'Samsung')
insert into Items (item_id, item_brand) values ('2', 'Lenovo')
insert into Items (item_id, item_brand) values ('3', 'LG')
insert into Items (item_id, item_brand) values ('4', 'HP')
Table: Users
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| user_id
| int
|
| join_date
| date
|
| favorite_brand | varchar |
+----------------+---------+
此表主键是 user_id,表中描述了购物网站的用户信息,用户可以在此网站上进行商品买卖。
Table: Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| order_date
| date
|
| item_id
| int
|
| buyer_id
| int
|
| seller_id
| int
|
+---------------+---------+
此表主键是 order_id,外键是 item_id 和(buyer_id,seller_id)。
Table: Item
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| item_id
| int
|
| item_brand
| varchar |
+---------------+---------+
此表主键是 item_id。
 
请写出一条SQL语句以查询每个用户的注册日期和在 2019 年作为买家的订单总数。
查询结果格式如下:
Users table:
+---------+------------+----------------+
| user_id | join_date
| favorite_brand |
+---------+------------+----------------+
| 1
| 2018-01-01 | Lenovo
|
| 2
| 2018-02-09 | Samsung
|
| 3
| 2018-01-19 | LG
|
| 4
| 2018-05-21 | HP
|
+---------+------------+----------------+
Orders table:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1
| 2019-08-01 | 4
| 1
| 2
|
| 2
| 2018-08-02 | 2
| 1
| 3
|
| 3
| 2019-08-03 | 3
| 2
| 3
|
| 4
| 2018-08-04 | 1
| 4
| 2
|
| 5
| 2018-08-04 | 1
| 3
| 4
|
| 6
| 2019-08-05 | 2
| 2
| 4
|
+----------+------------+---------+----------+-----------+
Items table:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1
| Samsung
|
| 2
| Lenovo
|
| 3
| LG
|
| 4
| HP
|
+---------+------------+
Result table:
+-----------+------------+----------------+
| buyer_id
| join_date
| orders_in_2019 |
+-----------+------------+----------------+
| 1
| 2018-01-01 | 1
|
| 2
| 2018-02-09 | 2
|
| 3
| 2018-01-19 | 0
|
| 4
| 2018-05-21 | 0
|
+-----------+------------+----------------+
# 答案:
select Users_user_id as buyer_id, join_date, ifnull(UserBuy.cnt, 0) as orders_in_2019
from Users
left join (
select buyer_id, count(order_id) cnt
from Orders
where order_date between '2019--01-01' and '2019-12-31'
group by buyer_id
) UserBuy
on Users.user_id = UserBuy.buyer_id;

1159 市场分析II

##SQL架构
Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10))
Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)
Create table If Not Exists Items (item_id int, item_brand varchar(10))
Truncate table Users
insert into Users (user_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo')
insert into Users (user_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung')
insert into Users (user_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG')
insert into Users (user_id, join_date, favorite_brand) values ('4', '2019-05-21', 'HP')
Truncate table Orders
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2019-08-02', '2', '1', '3')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2019-08-04', '1', '4', '2')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2019-08-04', '1', '3', '4')
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4')
Truncate table Items
insert into Items (item_id, item_brand) values ('1', 'Samsung')
insert into Items (item_id, item_brand) values ('2', 'Lenovo')
insert into Items (item_id, item_brand) values ('3', 'LG')
insert into Items (item_id, item_brand) values ('4', 'HP')
表: Users
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| user_id
| int
|
| join_date
| date
|
| favorite_brand | varchar |
+----------------+---------+
user_id 是该表的主键
表中包含一位在线购物网站用户的个人信息,用户可以在该网站出售和购买商品。
表: Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| order_date
| date
|
| item_id
| int
|
| buyer_id
| int
|
| seller_id
| int
|
+---------------+---------+
order_id 是该表的主键
item_id 是 Items 表的外键
buyer_id 和 seller_id 是 Users 表的外键
表: Items
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| item_id
| int
|
| item_brand
| varchar |
+---------------+---------+
item_id 是该表的主键
 
写一个 SQL 查询确定每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。如果一个用户卖出少于两件商品,查询的结果是 no 。
题目保证没有一个用户在一天中卖出超过一件商品
下面是查询结果格式的例子:
Users table:
+---------+------------+----------------+
| user_id | join_date
| favorite_brand |
+---------+------------+----------------+
| 1
| 2019-01-01 | Lenovo
|
| 2
| 2019-02-09 | Samsung
|
| 3
| 2019-01-19 | LG
|
| 4
| 2019-05-21 | HP
|
+---------+------------+----------------+
Orders table:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1
| 2019-08-01 | 4
| 1
| 2
|
| 2
| 2019-08-02 | 2
| 1
| 3
|
| 3
| 2019-08-03 | 3
| 2
| 3
|
| 4
| 2019-08-04 | 1
| 4
| 2
|
| 5
| 2019-08-04 | 1
| 3
| 4
|
| 6
| 2019-08-05 | 2
| 2
| 4
|
+----------+------------+---------+----------+-----------+
Items table:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1
| Samsung
|
| 2
| Lenovo
|
| 3
| LG
|
| 4
| HP
|
+---------+------------+
Result table:
+-----------+--------------------+
| seller_id | 2nd_item_fav_brand |
+-----------+--------------------+
| 1
| no
|
| 2
| yes
|
| 3
| yes
|
| 4
| no
|
+-----------+--------------------+
id 为 1 的用户的查询结果是 no,因为他什么也没有卖出
id为 2 和 3 的用户的查询结果是 yes,因为他们卖出的第二件商品的品牌是他们自己最喜爱的品牌
id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品牌不是他最喜爱的品牌
# 答案:
select user_id as seller_id, if (r2.item_brand is null || r2.item_brand != favorite_brand, "no", "yes") as 2nd_item_fav_brand
from users
left join (
select r1.seller_id, items.item_brand from (
select
@rk := if (@seller = a.seller_id, @rk + 1, 1) as rank,
@seller := a.seller_id as seller_id,
a.item_id
from (
select seller_id, item_id
from orders
order by seller_id, order_date
) a, (select @seller := -1, @rk := 0) b) r1
join items
on r1.item_id = items.item_id
where r1.rank = 2
) r2 on user_id = r2.seller_id;

1164 指定日期的产品价格

##SQL架构
Create table If Not Exists Products (product_id int, new_price int, change_date date)
Truncate table Products
insert into Products (product_id, new_price, change_date) values ('1', '20', '2019-08-14')
insert into Products (product_id, new_price, change_date) values ('2', '50', '2019-08-14')
insert into Products (product_id, new_price, change_date) values ('1', '30', '2019-08-15')
insert into Products (product_id, new_price, change_date) values ('1', '35', '2019-08-16')
insert into Products (product_id, new_price, change_date) values ('2', '65', '2019-08-17')
insert into Products (product_id, new_price, change_date) values ('3', '20', '2019-08-18')
产品数据表: Products
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| product_id
| int
|
| new_price
| int
|
| change_date
| date
|
+---------------+---------+
这张表的主键是 (product_id, change_date)。
这张表的每一行分别记录了 某产品 在某个日期 更改后 的新价格。
 
写一段 SQL来查找在 2019-08-16 时全部产品的价格,假设所有产品在修改前的价格都是 10。
查询结果格式如下例所示:
Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1
| 20
| 2019-08-14
|
| 2
| 50
| 2019-08-14
|
| 1
| 30
| 2019-08-15
|
| 1
| 35
| 2019-08-16
|
| 2
| 65
| 2019-08-17
|
| 3
| 20
| 2019-08-18
|
+------------+-----------+-------------+
Result table:
+------------+-------+
| product_id | price |
+------------+-------+
| 2
| 50
|
| 1
| 35
|
| 3
| 10
|
+------------+-------+
# 答案:
select p1.product_id, ifnull(p2.new_price, 10) as price
from (
select distinct product_id
from products
) as p1 -- 所有的产品
left join (
select product_id, new_price
from products
where (product_id, change_date) in (
select product_id, max(change_date)
from products
where change_date <= '2019-08-16'
group by product_id
)
) as p2 -- 在2019-08-16 之前有过修改的产品和最新的价格
on p1.product_id = p2.product_id;

1174 即时食物配送II

##SQL架构
Create table If Not Exists Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)
Truncate table Delivery
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('1', '1', '2019-08-01', '2019-08-02')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('2', '2', '2019-08-02', '2019-08-02')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('3', '1', '2019-08-11', '2019-08-12')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('4', '3', '2019-08-24', '2019-08-24')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('5', '3', '2019-08-21', '2019-08-22')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('6', '2', '2019-08-11', '2019-08-13')
insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('7', '4', '2019-08-09', '2019-08-09')
配送表: Delivery
+-----------------------------+---------+
| Column Name
| Type
|
+-----------------------------+---------+
| delivery_id
| int
|
| customer_id
| int
|
| order_date
| date
|
| customer_pref_delivery_date | date
|
+-----------------------------+---------+
delivery_id 是表的主键。
该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。
 
如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。
「首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。
写一条 SQL 查询语句获取即时订单在所有用户的首次订单中的比例。保留两位小数。
 
查询结果如下所示:
Delivery 表:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1
| 1
| 2019-08-01 | 2019-08-02
|
| 2
| 2
| 2019-08-02 | 2019-08-02
|
| 3
| 1
| 2019-08-11 | 2019-08-12
|
| 4
| 3
| 2019-08-24 | 2019-08-24
|
| 5
| 3
| 2019-08-21 | 2019-08-22
|
| 6
| 2
| 2019-08-11 | 2019-08-13
|
| 7
| 4
| 2019-08-09 | 2019-08-09
|
+-------------+-------------+------------+-----------------------------+
Result 表:
+----------------------+
| immediate_percentage |
+----------------------+
| 50.00
|
+----------------------+
1 号顾客的 1 号订单是首次订单,并且是计划订单。
2 号顾客的 2 号订单是首次订单,并且是即时订单。
3 号顾客的 5 号订单是首次订单,并且是计划订单。
4 号顾客的 7 号订单是首次订单,并且是即时订单。
因此,一半顾客的首次订单是即时的。
# 答案:
select round(
sum(order_date = customer_pref_delivery_date) * 100 /
count(*),
2
) as immediate_percentage
from Delivery
where (customer_id, order_date) in (
select customer_id, min(order_date)
from delivery
group by customer_id
);

1193 每月交易I

##SQL架构
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')
Table: Transactions
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| country
| varchar |
| state
| enum
|
| amount
| int
|
| trans_date
| date
|
+---------------+---------+
id 是这个表的主键。
该表包含有关传入事务的信息。
state 列类型为 “[”批准“,”拒绝“] 之一。
 
编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。
查询结果格式如下所示:
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 |
+------+---------+----------+--------+------------+
Result table:
+----------+---------+-------------+----------------+--------------------+-----------------------+
| 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
|
+----------+---------+-------------+----------------+--------------------+-----------------------+
# 答案:
select date_format(trans_date, '%Y-m') as month,
country,
count(*) as trans_count,
count(if(state = 'apporved', 1, null)) as approved_count,
sum(amount) as trans_total_amount,
sum(if(state = 'approved', amount, 0)) as apporved__total_amount
from Transactions
group by month, country;

1194 竞标赛优胜者

##SQL架构
Create table If Not Exists Players (player_id int, group_id int)
Create table If Not Exists Matches (match_id int, first_player int, second_player int, first_score int, second_score int)
Truncate table Players
insert into Players (player_id, group_id) values ('10', '2')
insert into Players (player_id, group_id) values ('15', '1')
insert into Players (player_id, group_id) values ('20', '3')
insert into Players (player_id, group_id) values ('25', '1')
insert into Players (player_id, group_id) values ('30', '1')
insert into Players (player_id, group_id) values ('35', '2')
insert into Players (player_id, group_id) values ('40', '3')
insert into Players (player_id, group_id) values ('45', '1')
insert into Players (player_id, group_id) values ('50', '2')
Truncate table Matches
insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('1', '15', '45', '3', '0')
insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('2', '30', '25', '1', '2')
insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('3', '30', '15', '2', '0')
insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('4', '40', '20', '5', '2')
insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('5', '35', '50', '1', '1')
Players 玩家表
+-------------+-------+
| Column Name | Type
|
+-------------+-------+
| player_id
| int
|
| group_id
| int
|
+-------------+-------+
player_id 是此表的主键。
此表的每一行表示每个玩家的组。
Matches 赛事表
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| match_id
| int
|
| first_player
| int
|
| second_player | int
|
| first_score
| int
|
| second_score
| int
|
+---------------+---------+
match_id 是此表的主键。
每一行是一场比赛的记录,first_player 和 second_player 表示该场比赛的球员 ID。
first_score 和 second_score 分别表示 first_player 和 second_player 的得分。
你可以假设,在每一场比赛中,球员都属于同一组。
 
每组的获胜者是在组内累积得分最高的选手。如果平局,player_id 最小 的选手获胜。
编写一个 SQL 查询来查找每组中的获胜者。
查询结果格式如下所示
Players 表:
+-----------+------------+
| player_id | group_id
|
+-----------+------------+
| 15
| 1
|
| 25
| 1
|
| 30
| 1
|
| 45
| 1
|
| 10
| 2
|
| 35
| 2
|
| 50
| 2
|
| 20
| 3
|
| 40
| 3
|
+-----------+------------+
Matches 表:
+------------+--------------+---------------+-------------+--------------+
| match_id
| first_player | second_player | first_score | second_score |
+------------+--------------+---------------+-------------+--------------+
| 1
| 15
| 45
| 3
| 0
|
| 2
| 30
| 25
| 1
| 2
|
| 3
| 30
| 15
| 2
| 0
|
| 4
| 40
| 20
| 5
| 2
|
| 5
| 35
| 50
| 1
| 1
|
+------------+--------------+---------------+-------------+--------------+
Result 表:
+-----------+------------+
| group_id
| player_id
|
+-----------+------------+
| 1
| 15
|
| 2
| 35
|
| 3
| 40
|
+-----------+------------+
# 答案:
select p1.product_id, ifnull(p2.new_price, 10) as price
from (
select distinct product_id
from products
) as p1 -- 所有的产品
left join (
select product_id, new_price
from products
where (product_id, change_date) in (
select product_id, max(change_date)
from products
where change_date <= '2019-08-16'
group by product_id
)
) as p2 -- 在 2019-08-16 之前有过修改的产品和最新的价格
on p1.product_id = p2.product_id;

1204 最后一个能进入电梯的人

##SQL架构
Create table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)
Truncate table Queue
insert into Queue (person_id, person_name, weight, turn) values ('5', 'George Washington', '250', '1')
insert into Queue (person_id, person_name, weight, turn) values ('4', 'Thomas Jefferson', '175', '5')
insert into Queue (person_id, person_name, weight, turn) values ('3', 'John Adams', '350', '2')
insert into Queue (person_id, person_name, weight, turn) values ('6', 'Thomas Jefferson', '400', '3')
insert into Queue (person_id, person_name, weight, turn) values ('1', 'James Elephant', '500', '6')
insert into Queue (person_id, person_name, weight, turn) values ('2', 'Will Johnliams', '200', '4')
表: Queue
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| person_id
| int
|
| person_name | varchar |
| weight
| int
|
| turn
| int
|
+-------------+---------+
person_id 是这个表的主键。
该表展示了所有等待电梯的人的信息。
表中 person_id 和 turn 列将包含从 1 到 n 的所有数字,其中 n 是表中的行数。
 
电梯最大载重量为 1000。
写一条 SQL 查询语句查找最后一个能进入电梯且不超过重量限制的 person_name 。题目确保队列中第一位的人可以进入电梯 。
查询结果如下所示 :
Queue 表
+-----------+-------------------+--------+------+
| person_id | person_name
| weight | turn |
+-----------+-------------------+--------+------+
| 5
| George Washington | 250
| 1
|
| 3
| John Adams
| 350
| 2
|
| 6
| Thomas Jefferson
| 400
| 3
|
| 2
| Will Johnliams
| 200
| 4
|
| 4
| Thomas Jefferson
| 175
| 5
|
| 1
| James Elephant
| 500
| 6
|
+-----------+-------------------+--------+------+
Result 表
+-------------------+
| person_name
|
+-------------------+
| Thomas Jefferson
|
+-------------------+
为了简化,Queue 表按 turn 列由小到大排序。
上例中 George Washington(id 5), John Adams(id 3) 和 Thomas Jefferson(id 6) 将可以进入电梯,因为他们的体重和为 250 + 350 + 400 = 1000。
Thomas Jefferson(id 6) 是最后一个体重合适并进入电梯的人。
# 答案:
SELECT a.person_name
FROM (
SELECT person_name, @pre := @pre + weight AS weight
FROM Queue, (SELECT @pre := 0) tmp
ORDER BY turn
) a
WHERE a.weight <= 1000
ORDER BY a.weight DESC
LIMIT 1;

1205 每月交易II

##SQL架构
Create table If Not Exists Transactions (id int, country varchar(4), state enum('approved', 'declined'), amount int, trans_date date)
Create table If Not Exists Chargebacks (trans_id int, trans_date date)
Truncate table Transactions
insert into Transactions (id, country, state, amount, trans_date) values ('101', 'US', 'approved', '1000', '2019-05-18')
insert into Transactions (id, country, state, amount, trans_date) values ('102', 'US', 'declined', '2000', '2019-05-19')
insert into Transactions (id, country, state, amount, trans_date) values ('103', 'US', 'approved', '3000', '2019-06-10')
insert into Transactions (id, country, state, amount, trans_date) values ('104', 'US', 'declined', '4000', '2019-06-13')
insert into Transactions (id, country, state, amount, trans_date) values ('105', 'US', 'approved', '5000', '2019-06-15')
Truncate table Chargebacks
insert into Chargebacks (trans_id, trans_date) values ('102', '2019-05-29')
insert into Chargebacks (trans_id, trans_date) values ('101', '2019-06-30')
insert into Chargebacks (trans_id, trans_date) values ('105', '2019-09-18')
Transactions 记录表
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| id
| int
|
| country
| varchar |
| state
| enum
|
| amount
| int
|
| trans_date
| date
|
+----------------+---------+
id 是这个表的主键。
该表包含有关传入事务的信息。
状态列是类型为 [approved(已批准)、declined(已拒绝)] 的枚举。
Chargebacks 表
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| trans_id
| int
|
| trans_date
| date
|
+----------------+---------+
退单包含有关放置在事务表中的某些事务的传入退单的基本信息。
trans_id 是 transactions 表的 id 列的外键。
每项退单都对应于之前进行的交易,即使未经批准。
 
编写一个 SQL 查询,以查找每个月和每个国家/地区的信息:已批准交易的数量及其总金额、退单的数量及其总金额。
注意:在您的查询中,只需显示给定月份和国家,忽略所有为零的行。
查询结果格式如下所示:
Transactions 表:
+-----+---------+----------+--------+------------+
| id
| country | state
| amount | trans_date |
+-----+---------+----------+--------+------------+
| 101 | US
| approved | 1000
| 2019-05-18 |
| 102 | US
| declined | 2000
| 2019-05-19 |
| 103 | US
| approved | 3000
| 2019-06-10 |
| 104 | US
| declined | 4000
| 2019-06-13 |
| 105 | US
| approved | 5000
| 2019-06-15 |
+-----+---------+----------+--------+------------+
Chargebacks 表:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 102
| 2019-05-29 |
| 101
| 2019-06-30 |
| 105
| 2019-09-18 |
+----------+------------+
Result 表:
+---------+---------+----------------+-----------------+------------------+-------------------+
| month
| country | approved_count | approved_amount | chargeback_count | chargeback_amount |
+---------+---------+----------------+-----------------+------------------+-------------------+
| 2019-05 | US
| 1
| 1000
| 1
| 2000
|
| 2019-06 | US
| 2
| 8000
| 1
| 1000
|
| 2019-09 | US
| 0
| 0
| 1
| 5000
|
+---------+---------+----------------+-----------------+------------------+-------------------+
# 答案:
SELECT month, country,
COUNT(IF(tag=1, 1, NULL)) AS approved_count,
SUM(IF(tag=1, amount, 0)) AS approved_amount,
COUNT(IF(tag=0, 1, NULL)) AS chargeback_count,
SUM(IF(tag=0, amount, 0)) AS chargeback_amount
FROM (
SELECT country, amount, 1 AS tag,
date_format(trans_date, '%Y-%m') AS month
FROM Transactions
WHERE state='approved'
UNION ALL
SELECT country, amount, 0 AS tag,
date_format(c.trans_date, '%Y-%m') AS month
FROM Transactions AS t RIGHT OUTER JOIN Chargebacks AS c
ON t.id = c.trans_id
) AS temp
GROUP BY
month, country;

1212 查询球队积分

##SQL架构
Create table If Not Exists Teams (team_id int, team_name varchar(30))
Create table If Not Exists Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)
Truncate table Teams
insert into Teams (team_id, team_name) values ('10', 'Leetcode FC')
insert into Teams (team_id, team_name) values ('20', 'NewYork FC')
insert into Teams (team_id, team_name) values ('30', 'Atlanta FC')
insert into Teams (team_id, team_name) values ('40', 'Chicago FC')
insert into Teams (team_id, team_name) values ('50', 'Toronto FC')
Truncate table Matches
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('1', '10', '20', '3', '0')
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('2', '30', '10', '2', '2')
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('3', '10', '50', '5', '1')
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('4', '20', '30', '1', '0')
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('5', '50', '30', '1', '0')
Table: Teams
+---------------+----------+
| Column Name
| Type
|
+---------------+----------+
| team_id
| int
|
| team_name
| varchar
|
+---------------+----------+
此表的主键是 team_id,表中的每一行都代表一支独立足球队。
Table: Matches
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| match_id
| int
|
| host_team
| int
|
| guest_team
| int
|
| host_goals
| int
|
| guest_goals
| int
|
+---------------+---------+
此表的主键是 match_id,表中的每一行都代表一场已结束的比赛,比赛的主客队分别由它们自己的 id 表示,他们的进球由 host_goals 和 guest_goals 分别表示。
 
积分规则如下:
赢一场得三分;
平一场得一分;
输一场不得分。
写出一条SQL语句以查询每个队的 team_id,team_name 和 num_points。结果根据 num_points 降序排序,如果有两队积分相同,那么这两队按 team_id  升序排序。
查询结果格式如下:
Teams table:
+-----------+--------------+
| team_id
| team_name
|
+-----------+--------------+
| 10
| Leetcode FC
|
| 20
| NewYork FC
|
| 30
| Atlanta FC
|
| 40
| Chicago FC
|
| 50
| Toronto FC
|
+-----------+--------------+
Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id
| host_team
| guest_team
| host_goals
| guest_goals
|
+------------+--------------+---------------+-------------+--------------+
| 1
| 10
| 20
| 3
| 0
|
| 2
| 30
| 10
| 2
| 2
|
| 3
| 10
| 50
| 5
| 1
|
| 4
| 20
| 30
| 1
| 0
|
| 5
| 50
| 30
| 1
| 0
|
+------------+--------------+---------------+-------------+--------------+
Result table:
+------------+--------------+---------------+
| team_id
| team_name
| num_points
|
+------------+--------------+---------------+
| 10
| Leetcode FC
| 7
|
| 20
| NewYork FC
| 3
|
| 50
| Toronto FC
| 3
|
| 30
| Atlanta FC
| 1
|
| 40
| Chicago FC
| 0
|
+------------+--------------+---------------+
# 答案:
SELECT t.team_id, t.team_name, IFNULL(score,0) num_points
FROM
(
SELECT team_id, SUM(score) score
FROM (
SELECT host_team team_id,
SUM(CASE
WHEN host_goals>guest_goals THEN 3
WHEN host_goals<guest_goals THEN 0
ELSE 1
END) score
FROM matches
GROUP BY host_team
UNION ALL
SELECT guest_team team_id,
SUM(CASE
WHEN host_goals>guest_goals THEN 0
WHEN host_goals<guest_goals THEN 3
ELSE 1
END) score
FROM matches
GROUP BY guest_team
) b
GROUP BY team_id
) a
RIGHT JOIN teams t ON t.team_id=a.team_id
ORDER BY num_points DESC, t.team_id;

1225 报告系统状态的连续日期

##SQL架构
Create table If Not Exists Failed (fail_date date)
Create table If Not Exists Succeeded (success_date date)
Truncate table Failed
insert into Failed (fail_date) values ('2018-12-28')
insert into Failed (fail_date) values ('2018-12-29')
insert into Failed (fail_date) values ('2019-01-04')
insert into Failed (fail_date) values ('2019-01-05')
Truncate table Succeeded
insert into Succeeded (success_date) values ('2018-12-30')
insert into Succeeded (success_date) values ('2018-12-31')
insert into Succeeded (success_date) values ('2019-01-01')
insert into Succeeded (success_date) values ('2019-01-02')
insert into Succeeded (success_date) values ('2019-01-03')
insert into Succeeded (success_date) values ('2019-01-06')
Table: Failed
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| fail_date
| date
|
+--------------+---------+
该表主键为 fail_date。
该表包含失败任务的天数.
Table: Succeeded
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| success_date | date
|
+--------------+---------+
该表主键为 success_date。
该表包含成功任务的天数.
 
系统 每天 运行一个任务。每个任务都独立于先前的任务。任务的状态可以是失败或是成功。
编写一个 SQL 查询 2019-01-01 到 2019-12-31 期间任务连续同状态 period_state 的起止日期(start_date 和 end_date)。即如果任务失败了,就是失败状态的起止日期,如果任务成功了,就是成功状态的起止日期。
最后结果按照起始日期 start_date 排序
查询结果样例如下所示:
Failed table:
+-------------------+
| fail_date
|
+-------------------+
| 2018-12-28
|
| 2018-12-29
|
| 2019-01-04
|
| 2019-01-05
|
+-------------------+
Succeeded table:
+-------------------+
| success_date
|
+-------------------+
| 2018-12-30
|
| 2018-12-31
|
| 2019-01-01
|
| 2019-01-02
|
| 2019-01-03
|
| 2019-01-06
|
+-------------------+
Result table:
+--------------+--------------+--------------+
| period_state | start_date
| end_date
|
+--------------+--------------+--------------+
| succeeded
| 2019-01-01
| 2019-01-03
|
| failed
| 2019-01-04
| 2019-01-05
|
| succeeded
| 2019-01-06
| 2019-01-06
|
+--------------+--------------+--------------+
结果忽略了 2018 年的记录,因为我们只关心从 2019-01-01 到 2019-12-31 的记录
从 2019-01-01 到 2019-01-03 所有任务成功,系统状态为 "succeeded"。
从 2019-01-04 到 2019-01-05 所有任务失败,系统状态为 "failed"。
从 2019-01-06 到 2019-01-06 所有任务成功,系统状态为 "succeeded"。
# 答案:
select type as period_state, min(date) as start_date, max(date) as end_date
from
(
select type, date, subdate(date,row_number()over(partition by type order by date)) as diff
from
(
select 'failed' as type, fail_date as date from Failed
union all
select 'succeeded' as type, success_date as date from Succeeded
) a
)a
where date between '2019-01-01' and '2019-12-31'
group by type,diff
order by start_date

1264 页面推荐

##SQL架构
Create table If Not Exists Friendship (user1_id int, user2_id int)
Create table If Not Exists Likes (user_id int, page_id int)
Truncate table Friendship
insert into Friendship (user1_id, user2_id) values ('1', '2')
insert into Friendship (user1_id, user2_id) values ('1', '3')
insert into Friendship (user1_id, user2_id) values ('1', '4')
insert into Friendship (user1_id, user2_id) values ('2', '3')
insert into Friendship (user1_id, user2_id) values ('2', '4')
insert into Friendship (user1_id, user2_id) values ('2', '5')
insert into Friendship (user1_id, user2_id) values ('6', '1')
Truncate table Likes
insert into Likes (user_id, page_id) values ('1', '88')
insert into Likes (user_id, page_id) values ('2', '23')
insert into Likes (user_id, page_id) values ('3', '24')
insert into Likes (user_id, page_id) values ('4', '56')
insert into Likes (user_id, page_id) values ('5', '11')
insert into Likes (user_id, page_id) values ('6', '33')
insert into Likes (user_id, page_id) values ('2', '77')
insert into Likes (user_id, page_id) values ('3', '77')
insert into Likes (user_id, page_id) values ('6', '88')
朋友关系列表: Friendship
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user1_id
| int
|
| user2_id
| int
|
+---------------+---------+
这张表的主键是 (user1_id, user2_id)。
这张表的每一行代表着 user1_id 和 user2_id 之间存在着朋友关系。
 
喜欢列表: Likes
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| user_id
| int
|
| page_id
| int
|
+-------------+---------+
这张表的主键是 (user_id, page_id)。
这张表的每一行代表着 user_id 喜欢 page_id。
 
写一段 SQL  向user_id = 1 的用户,推荐其朋友们喜欢的页面。不要推荐该用户已经喜欢的页面。
你返回的结果中不应当包含重复项。
返回结果的格式如下例所示:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1
| 2
|
| 1
| 3
|
| 1
| 4
|
| 2
| 3
|
| 2
| 4
|
| 2
| 5
|
| 6
| 1
|
+----------+----------+
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1
| 88
|
| 2
| 23
|
| 3
| 24
|
| 4
| 56
|
| 5
| 11
|
| 6
| 33
|
| 2
| 77
|
| 3
| 77
|
| 6
| 88
|
+---------+---------+
Result table:
+------------------+
| recommended_page |
+------------------+
| 23
|
| 24
|
| 56
|
| 33
|
| 77
|
+------------------+
用户1 同 用户2, 3, 4, 6 是朋友关系。
推荐页面为: 页面23 来自于 用户2, 页面24 来自于 用户3, 页面56 来自于 用户3 以及 页面33 来自于 用户6。
页面77 同时被 用户2 和 用户3 推荐。
页面88 没有被推荐,因为 用户1 已经喜欢了它。
# 答案:
SELECT DISTINCT page_id AS recommended_page
FROM Likes
WHERE user_id IN (
SELECT (
CASE
WHEN user1_id = 1 then user2_id
WHEN user2_id = 1 then user1_id
END
) AS user_id
FROM Friendship
WHERE user1_id = 1 OR user2_id = 1
)
AND page_id NOT IN (
SELECT page_id FROM Likes WHERE user_id = 1
)

1270 向公司CEO汇报工作的所有人

##SQL架构
Create table If Not Exists Employees (employee_id int, employee_name varchar(30), manager_id int)
Truncate table Employees
insert into Employees (employee_id, employee_name, manager_id) values ('1', 'Boss', '1')
insert into Employees (employee_id, employee_name, manager_id) values ('3', 'Alice', '3')
insert into Employees (employee_id, employee_name, manager_id) values ('2', 'Bob', '1')
insert into Employees (employee_id, employee_name, manager_id) values ('4', 'Daniel', '2')
insert into Employees (employee_id, employee_name, manager_id) values ('7', 'Luis', '4')
insert into Employees (employee_id, employee_name, manager_id) values ('8', 'John', '3')
insert into Employees (employee_id, employee_name, manager_id) values ('9', 'Angela', '8')
insert into Employees (employee_id, employee_name, manager_id) values ('77', 'Robert', '1')
员工表:Employees
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| employee_id
| int
|
| employee_name | varchar |
| manager_id
| int
|
+---------------+---------+
employee_id 是这个表的主键。
这个表中每一行中,employee_id 表示职工的 ID,employee_name 表示职工的名字,manager_id 表示该职工汇报工作的直线经理。
这个公司 CEO 是 employee_id = 1 的人。
 
用 SQL 查询出所有直接或间接向公司 CEO 汇报工作的职工的 employee_id 。
由于公司规模较小,经理之间的间接关系不超过 3 个经理。
可以以任何顺序返回无重复项的结果。
查询结果示例如下:
Employees table:
+-------------+---------------+------------+
| employee_id | employee_name | manager_id |
+-------------+---------------+------------+
| 1
| Boss
| 1
|
| 3
| Alice
| 3
|
| 2
| Bob
| 1
|
| 4
| Daniel
| 2
|
| 7
| Luis
| 4
|
| 8
| Jhon
| 3
|
| 9
| Angela
| 8
|
| 77
| Robert
| 1
|
+-------------+---------------+------------+
Result table:
+-------------+
| employee_id |
+-------------+
| 2
|
| 77
|
| 4
|
| 7
|
+-------------+
公司 CEO 的 employee_id 是 1.
employee_id 是 2 和 77 的职员直接汇报给公司 CEO。
employee_id 是 4 的职员间接汇报给公司 CEO 4 --> 2 --> 1 。
employee_id 是 7 的职员间接汇报给公司 CEO 7 --> 4 --> 2 --> 1 。
employee_id 是 3, 8 ,9 的职员不会直接或间接的汇报给公司 CEO。
# 答案:
SELECT e1.employee_id
FROM Employees e1
JOIN Employees e2 ON e1.manager_id = e2.employee_id
JOIN Employees e3 ON e2.manager_id = e3.employee_id
WHERE e1.employee_id != 1 AND e3.manager_id = 1;

1285 找到连续区间开始和结束数字

##SQL架构
Create table If Not Exists Logs (log_id int)
Truncate table Logs
insert into Logs (log_id) values ('1')
insert into Logs (log_id) values ('2')
insert into Logs (log_id) values ('3')
insert into Logs (log_id) values ('7')
insert into Logs (log_id) values ('8')
insert into Logs (log_id) values ('10')
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| log_id
| int
|
+---------------+---------+
id 是上表的主键。
上表的每一行包含日志表中的一个 ID。
 
后来一些 ID 从 Logs 表中删除。编写一个 SQL 查询得到 Logs 表中的连续区间的开始数字和结束数字。
将查询表按照 start_id 排序。
查询结果格式如下面的例子:
Logs 表:
+------------+
| log_id
|
+------------+
| 1
|
| 2
|
| 3
|
| 7
|
| 8
|
| 10
|
+------------+
结果表:
+------------+--------------+
| start_id
| end_id
|
+------------+--------------+
| 1
| 3
|
| 7
| 8
|
| 10
| 10
|
+------------+--------------+
结果表应包含 Logs 表中的所有区间。
从 1 到 3 在表中。
从 4 到 6 不在表中。
从 7 到 8 在表中。
9 不在表中。
10 在表中。
答案:
SELECT a.log_id AS START_ID ,MIN(b.log_id) AS END_ID FROM
(SELECT CAST(log_id AS SIGNED) AS log_id FROM LOGS WHERE log_id-1 NOT IN (SELECT * FROM LOGS)) a,
(SELECT CAST(log_id AS SIGNED) AS log_id FROM LOGS WHERE log_id+1 NOT IN (SELECT * FROM LOGS)) b
WHERE b.log_id >= a.log_id
GROUP BY a.log_id

1308 不同性别每日分数总计

##SQL架构
Create table If Not Exists Scores (player_name varchar(20), gender varchar(1), day date, score_points int)
Truncate table Scores
insert into Scores (player_name, gender, day, score_points) values ('Aron', 'F', '2020-01-01', '17')
insert into Scores (player_name, gender, day, score_points) values ('Alice', 'F', '2020-01-07', '23')
insert into Scores (player_name, gender, day, score_points) values ('Bajrang', 'M', '2020-01-07', '7')
insert into Scores (player_name, gender, day, score_points) values ('Khali', 'M', '2019-12-25', '11')
insert into Scores (player_name, gender, day, score_points) values ('Slaman', 'M', '2019-12-30', '13')
insert into Scores (player_name, gender, day, score_points) values ('Joe', 'M', '2019-12-31', '3')
insert into Scores (player_name, gender, day, score_points) values ('Jose', 'M', '2019-12-18', '2')
insert into Scores (player_name, gender, day, score_points) values ('Priya', 'F', '2019-12-31', '23')
insert into Scores (player_name, gender, day, score_points) values ('Priyanka', 'F', '2019-12-30', '17')
表: Scores
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| player_name
| varchar |
| gender
| varchar |
| day
| date
|
| score_points
| int
|
+---------------+---------+
(gender, day)是该表的主键
一场比赛是在女队和男队之间举行的
该表的每一行表示一个名叫 (player_name) 性别为 (gender) 的参赛者在某一天获得了 (score_points) 的分数
如果参赛者是女性,那么 gender 列为 'F',如果参赛者是男性,那么 gender 列为 'M'
 
写一条SQL语句查询每种性别在每一天的总分,并按性别和日期对查询结果排序
下面是查询结果格式的例子:
Scores表:
+-------------+--------+------------+--------------+
| player_name | gender | day
| score_points |
+-------------+--------+------------+--------------+
| Aron
| F
| 2020-01-01 | 17
|
| Alice
| F
| 2020-01-07 | 23
|
| Bajrang
| M
| 2020-01-07 | 7
|
| Khali
| M
| 2019-12-25 | 11
|
| Slaman
| M
| 2019-12-30 | 13
|
| Joe
| M
| 2019-12-31 | 3
|
| Jose
| M
| 2019-12-18 | 2
|
| Priya
| F
| 2019-12-31 | 23
|
| Priyanka
| F
| 2019-12-30 | 17
|
+-------------+--------+------------+--------------+
结果表:
+--------+------------+-------+
| gender | day
| total |
+--------+------------+-------+
| F
| 2019-12-30 | 17
|
| F
| 2019-12-31 | 40
|
| F
| 2020-01-01 | 57
|
| F
| 2020-01-07 | 80
|
| M
| 2019-12-18 | 2
|
| M
| 2019-12-25 | 13
|
| M
| 2019-12-30 | 26
|
| M
| 2019-12-31 | 29
|
| M
| 2020-01-07 | 36
|
+--------+------------+-------+
女性队伍:
第一天是 2019-12-30,Priyanka 获得 17 分,队伍的总分是 17 分
第二天是 2019-12-31, Priya 获得 23 分,队伍的总分是 40 分
第三天是 2020-01-01, Aron 获得 17 分,队伍的总分是 57 分
第四天是 2020-01-07, Alice 获得 23 分,队伍的总分是 80 分
男性队伍:
第一天是 2019-12-18, Jose 获得 2 分,队伍的总分是 2 分
第二天是 2019-12-25, Khali 获得 11 分,队伍的总分是 13 分
第三天是 2019-12-30, Slaman 获得 13 分,队伍的总分是 26 分
第四天是 2019-12-31, Joe 获得 3 分,队伍的总分是 29 分
第五天是 2020-01-07, Bajrang 获得 7 分,队伍的总分是 36 分
# 答案:
1、连接法
SELECT s1.gender, s1.day, SUM(s2.score_points) AS total
FROM Scores AS s1 JOIN Scores AS s2
ON s1.gender = s2.gender AND s1.day >= s2.day
GROUP BY s1.gender, s1.day
ORDER BY s1.gender, s1.day;
2、窗口函数
SELECT gender, day, SUM(score_points) OVER (PARTITION BY gender ORDER BY day) AS total
FROM Scores;

1321 餐馆营业额变化增长

##SQL架构
Create table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)
Truncate table Customer
insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-01', '100')
insert into Customer (customer_id, name, visited_on, amount) values ('2', 'Daniel', '2019-01-02', '110')
insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-03', '120')
insert into Customer (customer_id, name, visited_on, amount) values ('4', 'Khaled', '2019-01-04', '130')
insert into Customer (customer_id, name, visited_on, amount) values ('5', 'Winston', '2019-01-05', '110')
insert into Customer (customer_id, name, visited_on, amount) values ('6', 'Elvis', '2019-01-06', '140')
insert into Customer (customer_id, name, visited_on, amount) values ('7', 'Anna', '2019-01-07', '150')
insert into Customer (customer_id, name, visited_on, amount) values ('8', 'Maria', '2019-01-08', '80')
insert into Customer (customer_id, name, visited_on, amount) values ('9', 'Jaze', '2019-01-09', '110')
insert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-10', '130')
insert into Customer (customer_id, name, visited_on, amount) values ('3', 'Jade', '2019-01-10', '150')
表: Customer
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| name
| varchar |
| visited_on
| date
|
| amount
| int
|
+---------------+---------+
(customer_id, visited_on) 是该表的主键
该表包含一家餐馆的顾客交易数据
visited_on 表示 (customer_id) 的顾客在 visited_on 那天访问了餐馆
amount 是一个顾客某一天的消费总额
 
你是餐馆的老板,现在你想分析一下可能的营业额变化增长(每天至少有一位顾客)
写一条 SQL 查询计算以 7 天(某日期 + 该日期前的 6 天)为一个时间段的顾客消费平均值
查询结果格式的例子如下:
查询结果按 visited_on 排序
average_amount 要 保留两位小数,日期数据的格式为 ('YYYY-MM-DD')
 
Customer 表:
+-------------+--------------+--------------+-------------+
| customer_id | name
| visited_on
| amount
|
+-------------+--------------+--------------+-------------+
| 1
| Jhon
| 2019-01-01
| 100
|
| 2
| Daniel
| 2019-01-02
| 110
|
| 3
| Jade
| 2019-01-03
| 120
|
| 4
| Khaled
| 2019-01-04
| 130
|
| 5
| Winston
| 2019-01-05
| 110
|
| 6
| Elvis
| 2019-01-06
| 140
|
| 7
| Anna
| 2019-01-07
| 150
|
| 8
| Maria
| 2019-01-08
| 80
|
| 9
| Jaze
| 2019-01-09
| 110
|
| 1
| Jhon
| 2019-01-10
| 130
|
| 3
| Jade
| 2019-01-10
| 150
|
+-------------+--------------+--------------+-------------+
结果表:
+--------------+--------------+----------------+
| visited_on
| amount
| average_amount |
+--------------+--------------+----------------+
| 2019-01-07
| 860
| 122.86
|
| 2019-01-08
| 840
| 120
|
| 2019-01-09
| 840
| 120
|
| 2019-01-10
| 1000
| 142.86
|
+--------------+--------------+----------------+
第一个七天消费平均值从 2019-01-01 到 2019-01-07 是 (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86
第二个七天消费平均值从 2019-01-02 到 2019-01-08 是 (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120
第三个七天消费平均值从 2019-01-03 到 2019-01-09 是 (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120
第四个七天消费平均值从 2019-01-04 到 2019-01-10 是 (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86
# 答案:
SELECT
a.visited_on,
SUM(b.amount) AS amount,
ROUND(SUM(b.amount) / 7, 2) AS average_amount
FROM
(SELECT DISTINCT visited_on FROM customer) a JOIN customer b
ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6
WHERE
a.visited_on >= (SELECT MIN(visited_on) FROM customer ) + 6
GROUP BY
a.visited_on

1341 电影评分

##SQL架构
Create table If Not Exists Movies (movie_id int, title varchar(30))
Create table If Not Exists Users (user_id int, name varchar(30))
Create table If Not Exists MovieRating (movie_id int, user_id int, rating int, created_at date)
Truncate table Movies
insert into Movies (movie_id, title) values ('1', 'Avengers')
insert into Movies (movie_id, title) values ('2', 'Frozen 2')
insert into Movies (movie_id, title) values ('3', 'Joker')
Truncate table Users
insert into Users (user_id, name) values ('1', 'Daniel')
insert into Users (user_id, name) values ('2', 'Monica')
insert into Users (user_id, name) values ('3', 'Maria')
insert into Users (user_id, name) values ('4', 'James')
Truncate table MovieRating
insert into MovieRating (movie_id, user_id, rating, created_at) values ('1', '1', '3', '2020-01-12')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('1', '2', '4', '2020-02-11')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('1', '3', '2', '2020-02-12')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('1', '4', '1', '2020-01-01')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('2', '1', '5', '2020-02-17')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('2', '2', '2', '2020-02-01')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('2', '3', '2', '2020-03-01')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('3', '1', '3', '2020-02-22')
insert into MovieRating (movie_id, user_id, rating, created_at) values ('3', '2', '4', '2020-02-25')
表:Movies
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| movie_id
| int
|
| title
| varchar |
+---------------+---------+
movie_id 是这个表的主键。
title 是电影的名字。
表:Users
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user_id
| int
|
| name
| varchar |
+---------------+---------+
user_id 是表的主键。
表:Movie_Rating
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| movie_id
| int
|
| user_id
| int
|
| rating
| int
|
| created_at
| date
|
+---------------+---------+
(movie_id, user_id) 是这个表的主键。
这个表包含用户在其评论中对电影的评分 rating 。
created_at 是用户的点评日期。
 
请你编写一组 SQL 查询:
查找评论电影数量最多的用户名。
如果出现平局,返回字典序较小的用户名。
查找在 2020 年 2 月 平均评分最高 的电影名称。
如果出现平局,返回字典序较小的电影名称。
字典序 ,即按字母在字典中出现顺序对字符串排序,字典序较小则意味着排序靠前。
查询分两行返回,查询结果格式如下例所示:
Movies 表:
+-------------+--------------+
| movie_id
|
title
|
+-------------+--------------+
| 1
| Avengers
|
| 2
| Frozen 2
|
| 3
| Joker
|
+-------------+--------------+
Users 表:
+-------------+--------------+
| user_id
|
name
|
+-------------+--------------+
| 1
| Daniel
|
| 2
| Monica
|
| 3
| Maria
|
| 4
| James
|
+-------------+--------------+
MovieRating 表:
+-------------+--------------+--------------+-------------+
| movie_id
| user_id
| rating
| created_at
|
+-------------+--------------+--------------+-------------+
| 1
| 1
| 3
| 2020-01-12
|
| 1
| 2
| 4
| 2020-02-11
|
| 1
| 3
| 2
| 2020-02-12
|
| 1
| 4
| 1
| 2020-01-01
|
| 2
| 1
| 5
| 2020-02-17
|
| 2
| 2
| 2
| 2020-02-01
|
| 2
| 3
| 2
| 2020-03-01
|
| 3
| 1
| 3
| 2020-02-22
|
| 3
| 2
| 4
| 2020-02-25
|
+-------------+--------------+--------------+-------------+
Result 表:
+--------------+
| results
|
+--------------+
| Daniel
|
| Frozen 2
|
+--------------+
Daniel 和 Monica 都点评了 3 部电影("Avengers", "Frozen 2" 和 "Joker") 但是 Daniel 字典序比较小。
Frozen 2 和 Joker 在 2 月的评分都是 3.5,但是 Frozen 2 的字典序比较小。
# 答案:
(select u.name `results` from Users u left join MovieRating m on u.user_id = m.user_id group by m.user_id order by count(m.user_id) desc,u.name asc limit 1)
union
(select m.title `results` from Movies m left join MovieRating mr on m.movie_id = mr.movie_id where created_at between '2020-02-01' and '2020-02-29' group by mr.movie_id order by avg(mr.rating) desc,m.title asc limit 1)

1355 活动参与者

##SQL架构
Create table If Not Exists Friends (id int, name varchar(30), activity varchar(30))
Create table If Not Exists Activities (id int, name varchar(30))
Truncate table Friends
insert into Friends (id, name, activity) values ('1', 'Jonathan D.', 'Eating')
insert into Friends (id, name, activity) values ('2', 'Jade W.', 'Singing')
insert into Friends (id, name, activity) values ('3', 'Victor J.', 'Singing')
insert into Friends (id, name, activity) values ('4', 'Elvis Q.', 'Eating')
insert into Friends (id, name, activity) values ('5', 'Daniel A.', 'Eating')
insert into Friends (id, name, activity) values ('6', 'Bob B.', 'Horse Riding')
Truncate table Activities
insert into Activities (id, name) values ('1', 'Eating')
insert into Activities (id, name) values ('2', 'Singing')
insert into Activities (id, name) values ('3', 'Horse Riding')
表: Friends
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| name
| varchar |
| activity
| varchar |
+---------------+---------+
id 是朋友的 id 和该表的主键
name 是朋友的名字
activity 是朋友参加的活动的名字
表: Activities
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| name
| varchar |
+---------------+---------+
id 是该表的主键
name 是活动的名字
 
写一条 SQL 查询那些既没有最多,也没有最少参与者的活动的名字
可以以任何顺序返回结果,Activities 表的每项活动的参与者都来自 Friends 表
注意:名称相同 id 不同的参与者算作两个人
下面是查询结果格式的例子:
Friends 表:
+------+--------------+---------------+
| id
| name
| activity
|
+------+--------------+---------------+
| 1
| Jonathan D.
| Eating
|
| 2
| Jade W.
| Singing
|
| 3
| Victor J.
| Singing
|
| 4
| Elvis Q.
| Eating
|
| 5
| Daniel A.
| Eating
|
| 6
| Bob B.
| Horse Riding
|
+------+--------------+---------------+
Activities 表:
+------+--------------+
| id
| name
|
+------+--------------+
| 1
| Eating
|
| 2
| Singing
|
| 3
| Horse Riding |
+------+--------------+
Result 表:
+--------------+
| activity
|
+--------------+
| Singing
|
+--------------+
Eating 活动有三个人参加, 是最多人参加的活动 (Jonathan D. , Elvis Q. and Daniel A.)
Horse Riding 活动有一个人参加, 是最少人参加的活动 (Bob B.)
Singing 活动有两个人参加 (Victor J. and Jade W.)
# 答案:
select activity as ACTIVITY
from friends
group by activity
having count(*) > any(
select count(*) from friends group by activity
) and count(*) < any(
select count(*) from friends group by activity
)
# 举例说明
any表示有任何一个满足就返回true,all表示全部都满足才返回true
比如
select * from student where 班级=’01’ and age > all (select age from student where 班级=’02’);
就是说,查询出01班中,年龄大于 02班所有人的同学
相当于
select * from student where 班级=’01’ and age > (select max(age) from student where 班级=’02’);
而
select * from student where 班级=’01’ and age > any (select age from student where 班级=’02’);
就是说,查询出01班中,年龄大于 02班任意一个 的 同学
相当于
select * from student where 班级=’01’ and age > (select min(age) from student where 班级=’02’);

1364 顾客的可信联系人数量

##SQL架构
Create table If Not Exists Customers (customer_id int, customer_name varchar(20), email varchar(30))
Create table If Not Exists Contacts (user_id int, contact_name varchar(20), contact_email varchar(30))
Create table If Not Exists Invoices (invoice_id int, price int, user_id int)
Truncate table Customers
insert into Customers (customer_id, customer_name, email) values ('1', 'Alice', 'alice@leetcode.com')
insert into Customers (customer_id, customer_name, email) values ('2', 'Bob', 'bob@leetcode.com')
insert into Customers (customer_id, customer_name, email) values ('13', 'John', 'john@leetcode.com')
insert into Customers (customer_id, customer_name, email) values ('6', 'Alex', 'alex@leetcode.com')
Truncate table Contacts
insert into Contacts (user_id, contact_name, contact_email) values ('1', 'Bob', 'bob@leetcode.com')
insert into Contacts (user_id, contact_name, contact_email) values ('1', 'John', 'john@leetcode.com')
insert into Contacts (user_id, contact_name, contact_email) values ('1', 'Jal', 'jal@leetcode.com')
insert into Contacts (user_id, contact_name, contact_email) values ('2', 'Omar', 'omar@leetcode.com')
insert into Contacts (user_id, contact_name, contact_email) values ('2', 'Meir', 'meir@leetcode.com')
insert into Contacts (user_id, contact_name, contact_email) values ('6', 'Alice', 'alice@leetcode.com')
Truncate table Invoices
insert into Invoices (invoice_id, price, user_id) values ('77', '100', '1')
insert into Invoices (invoice_id, price, user_id) values ('88', '200', '1')
insert into Invoices (invoice_id, price, user_id) values ('99', '300', '2')
insert into Invoices (invoice_id, price, user_id) values ('66', '400', '2')
insert into Invoices (invoice_id, price, user_id) values ('55', '500', '13')
insert into Invoices (invoice_id, price, user_id) values ('44', '60', '6')
顾客表:Customers
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| customer_name | varchar |
| email
| varchar |
+---------------+---------+
customer_id 是这张表的主键。
此表的每一行包含了某在线商店顾客的姓名和电子邮件。
 
联系方式表:Contacts
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user_id
| id
|
| contact_name
| varchar |
| contact_email | varchar |
+---------------+---------+
(user_id, contact_email) 是这张表的主键。
此表的每一行表示编号为 user_id 的顾客的某位联系人的姓名和电子邮件。
此表包含每位顾客的联系人信息,但顾客的联系人不一定存在于顾客表中。
 
发票表:Invoices
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| invoice_id
| int
|
| price
| int
|
| user_id
| int
|
+--------------+---------+
invoice_id 是这张表的主键。
此表的每一行分别表示编号为 user_id 的顾客拥有有一张编号为 invoice_id、价格为 price 的发票。
 
为每张发票 invoice_id 编写一个SQL查询以查找以下内容:
customer_name:与发票相关的顾客名称。
price:发票的价格。
contacts_cnt:该顾客的联系人数量。
trusted_contacts_cnt:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于客户表中)。
将查询的结果按照 invoice_id 排序。
查询结果的格式如下例所示:
Customers table:
+-------------+---------------+--------------------+
| customer_id | customer_name | email
|
+-------------+---------------+--------------------+
| 1
| Alice
| alice@leetcode.com |
| 2
| Bob
| bob@leetcode.com
|
| 13
| John
| john@leetcode.com
|
| 6
| Alex
| alex@leetcode.com
|
+-------------+---------------+--------------------+
Contacts table:
+-------------+--------------+--------------------+
| user_id
| contact_name | contact_email
|
+-------------+--------------+--------------------+
| 1
| Bob
| bob@leetcode.com
|
| 1
| John
| john@leetcode.com
|
| 1
| Jal
| jal@leetcode.com
|
| 2
| Omar
| omar@leetcode.com
|
| 2
| Meir
| meir@leetcode.com
|
| 6
| Alice
| alice@leetcode.com |
+-------------+--------------+--------------------+
Invoices table:
+------------+-------+---------+
| invoice_id | price | user_id |
+------------+-------+---------+
| 77
| 100
| 1
|
| 88
| 200
| 1
|
| 99
| 300
| 2
|
| 66
| 400
| 2
|
| 55
| 500
| 13
|
| 44
| 60
| 6
|
+------------+-------+---------+
Result table:
+------------+---------------+-------+--------------+----------------------+
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
+------------+---------------+-------+--------------+----------------------+
| 44
| Alex
| 60
| 1
| 1
|
| 55
| John
| 500
| 0
| 0
|
| 66
| Bob
| 400
| 2
| 0
|
| 77
| Alice
| 100
| 3
| 2
|
| 88
| Alice
| 200
| 3
| 2
|
| 99
| Bob
| 300
| 2
| 0
|
+------------+---------------+-------+--------------+----------------------+
Alice 有三位联系人,其中两位(Bob 和 John)是可信联系人。
Bob 有两位联系人, 他们中的任何一位都不是可信联系人。
Alex 只有一位联系人(Alice),并是一位可信联系人。
John 没有任何联系人。
# 答案:
SELECT invoices.invoice_id, customers.customer_name, invoices.price, COUNT(contacts.user_id) AS contacts_cnt, COUNT(c2.email) AS trusted_contacts_cnt
FROM Invoices invoices
INNER JOIN Customers customers ON invoices.user_id = customers.customer_id
LEFT JOIN Contacts contacts ON customers.customer_id = contacts.user_id
LEFT JOIN Customers c2 ON contacts.contact_email = c2.email
GROUP BY invoices.invoice_id

1369 获取最近第二次的活动

##SQL架构
Create table If Not Exists UserActivity (username varchar(30), activity varchar(30), startDate date, endDate date)
Truncate table UserActivity
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Travel', '2020-02-12', '2020-02-20')
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Dancing', '2020-02-21', '2020-02-23')
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Travel', '2020-02-24', '2020-02-28')
insert into UserActivity (username, activity, startDate, endDate) values ('Bob', 'Travel', '2020-02-11', '2020-02-18')
表: UserActivity
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| username
| varchar |
| activity
| varchar |
| startDate
| Date
|
| endDate
| Date
|
+---------------+---------+
该表不包含主键
该表包含每个用户在一段时间内进行的活动的信息
名为 username 的用户在 startDate 到 endDate 日内有一次活动
 
写一条SQL查询展示每一位用户 最近第二次 的活动
如果用户仅有一次活动,返回该活动
一个用户不能同时进行超过一项活动,以 任意 顺序返回结果
下面是查询结果格式的例子:
UserActivity 表:
+------------+--------------+-------------+-------------+
| username
| activity
| startDate
| endDate
|
+------------+--------------+-------------+-------------+
| Alice
| Travel
| 2020-02-12
| 2020-02-20
|
| Alice
| Dancing
| 2020-02-21
| 2020-02-23
|
| Alice
| Travel
| 2020-02-24
| 2020-02-28
|
| Bob
| Travel
| 2020-02-11
| 2020-02-18
|
+------------+--------------+-------------+-------------+
Result 表:
+------------+--------------+-------------+-------------+
| username
| activity
| startDate
| endDate
|
+------------+--------------+-------------+-------------+
| Alice
| Dancing
| 2020-02-21
| 2020-02-23
|
| Bob
| Travel
| 2020-02-11
| 2020-02-18
|
+------------+--------------+-------------+-------------+
Alice 最近一次的活动是从 2020-02-24 到 2020-02-28 的旅行, 在此之前的 2020-02-21 到 2020-02-23 她进行了舞蹈
Bob 只有一条记录,我们就取这条记录
# 答案:
select u.*
from UserActivity u
where (u.username, u.startDate) in
(
select a.username, max(a.startDate)
from UserActivity a
where (a.username, a.startDate) not in (
select b.username, max(b.startDate)
from UserActivity b
group by b.username
having count(b.username) > 1
)
group by a.username
)

1384 按年度列出销售总额

##SQL架构
Create table If Not Exists Product (product_id int, product_name varchar(30))
Create table If Not Exists Sales (product_id varchar(30), period_start date, period_end date, average_daily_sales int)
Truncate table Product
insert into Product (product_id, product_name) values ('1', 'LC Phone ')
insert into Product (product_id, product_name) values ('2', 'LC T-Shirt')
insert into Product (product_id, product_name) values ('3', 'LC Keychain')
Truncate table Sales
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('1', '2019-01-25', '2019-02-28', '100')
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('2', '2018-12-01', '2020-01-01', '10')
insert into Sales (product_id, period_start, period_end, average_daily_sales) values ('3', '2019-12-01', '2020-01-31', '1')
 Product 表:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| product_id
| int
|
| product_name
| varchar |
+---------------+---------+
product_id 是这张表的主键。
product_name 是产品的名称。
 
Sales 表:
+---------------------+---------+
| Column Name
| Type
|
+---------------------+---------+
| product_id
| int
|
| period_start
| date
|
| period_end
| date
|
| average_daily_sales | int
|
+---------------------+---------+
product_id 是这张表的主键。
period_start 和 period_end 是该产品销售期的起始日期和结束日期,且这两个日期包含在销售期内。
average_daily_sales 列存储销售期内该产品的日平均销售额。
 
编写一段 SQL 查询每个产品每年的总销售额,并包含 product_id, product_name 以及 report_year 等信息。
销售年份的日期介于 2018 年到 2020 年之间。你返回的结果需要按 product_id 和 report_year 排序。
查询结果格式如下例所示:
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 1
| LC Phone
|
| 2
| LC T-Shirt
|
| 3
| LC Keychain
|
+------------+--------------+
Sales table:
+------------+--------------+-------------+---------------------+
| product_id | period_start | period_end
| average_daily_sales |
+------------+--------------+-------------+---------------------+
| 1
| 2019-01-25
| 2019-02-28
| 100
|
| 2
| 2018-12-01
| 2020-01-01
| 10
|
| 3
| 2019-12-01
| 2020-01-31
| 1
|
+------------+--------------+-------------+---------------------+
Result table:
+------------+--------------+-------------+--------------+
| product_id | product_name | report_year | total_amount |
+------------+--------------+-------------+--------------+
| 1
| LC Phone
|
2019
| 3500
|
| 2
| LC T-Shirt
|
2018
| 310
|
| 2
| LC T-Shirt
|
2019
| 3650
|
| 2
| LC T-Shirt
|
2020
| 10
|
| 3
| LC Keychain
|
2019
| 31
|
| 3
| LC Keychain
|
2020
| 31
|
+------------+--------------+-------------+--------------+
LC Phone 在 2019-01-25 至 2019-02-28 期间销售,该产品销售时间总计35天。销售总额 35*100 = 3500。
LC T-shirt 在 2018-12-01 至 2020-01-01 期间销售,该产品在2018年、2019年、2020年的销售时间分别是31天、365天、1天,2018年、2019年、2020年的销售总额分别是31*10=310、365*10=3650、1*10=10。
LC Keychain 在 2019-12-01 至 2020-01-31 期间销售,该产品在2019年、2020年的销售时间分别是:31天、31天,2019年、2020年的销售总额分别是31*1=31、31*1=31。
# 答案:
1、列名区分大小写
2、product_id要返回字符串,sales表的product_id好像是字符串类型的,和题目描述不一样
3、report_year要返回字符串,所以不能用year(),只能用date_format()
4、最后product_id排序要根据字典序,而不是数字顺序,这个真的违反一般习惯
select
s.PRODUCT_ID, PRODUCT_NAME, date_format(bound, '%Y') REPORT_YEAR,
(datediff(
if(bound < period_end, bound, period_end),
if(makedate(year(bound), 1) > period_start, makedate(year(bound), 1), period_start)
) + 1) * average_daily_sales TOTAL_AMOUNT
from product p join (
select '2018-12-31' bound
union all
select '2019-12-31' bound
union all
select '2020-12-31' bound
) bounds join sales s
on
p.product_id = s.product_id
and year(bound) between year(period_start) and year(period_end)
order by s.product_id, report_year

1393 股票的资本损益

##SQL架构
Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int)
Truncate table Stocks
insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Buy', '1', '1000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '2', '10')
insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Sell', '5', '9000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Buy', '17', '30000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '3', '1010')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '4', '1000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '5', '500')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '6', '1000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Sell', '29', '7000')
insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '10', '10000')
Stocks 表:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| stock_name
| varchar |
| operation
| enum
|
| operation_day | int
|
| price
| int
|
+---------------+---------+
(stock_name, day) 是这张表的主键
operation 列使用的是一种枚举类型,包括:('Sell','Buy')
此表的每一行代表了名为 stock_name 的某支股票在 operation_day 这一天的操作价格。
保证股票的每次'Sell'操作前,都有相应的'Buy'操作。
 
编写一个SQL查询来报告每支股票的资本损益。
股票的资本损益是一次或多次买卖股票后的全部收益或损失。
以任意顺序返回结果即可。
SQL查询结果的格式如下例所示:
Stocks 表:
+---------------+-----------+---------------+--------+
| stock_name
| operation | operation_day | price
|
+---------------+-----------+---------------+--------+
| Leetcode
| Buy
| 1
| 1000
|
| Corona Masks
| Buy
| 2
| 10
|
| Leetcode
| Sell
| 5
| 9000
|
| Handbags
| Buy
| 17
| 30000
|
| Corona Masks
| Sell
| 3
| 1010
|
| Corona Masks
| Buy
| 4
| 1000
|
| Corona Masks
| Sell
| 5
| 500
|
| Corona Masks
| Buy
| 6
| 1000
|
| Handbags
| Sell
| 29
| 7000
|
| Corona Masks
| Sell
| 10
| 10000
|
+---------------+-----------+---------------+--------+
Result 表:
+---------------+-------------------+
| stock_name
| capital_gain_loss |
+---------------+-------------------+
| Corona Masks
| 9500
|
| Leetcode
| 8000
|
| Handbags
| -23000
|
+---------------+-------------------+
Leetcode 股票在第一天以1000美元的价格买入,在第五天以9000美元的价格卖出。资本收益=9000-1000=8000美元。
Handbags 股票在第17天以30000美元的价格买入,在第29天以7000美元的价格卖出。资本损失=7000-30000=-23000美元。
Corona Masks 股票在第1天以10美元的价格买入,在第3天以1010美元的价格卖出。在第4天以1000美元的价格再次购买,在第5天以500美元的价格出售。最后,它在第6天以1000美元的价格被买走,在第10天以10000美元的价格被卖掉。资本损益是每次('Buy'->'Sell')操作资本收益或损失的和=(1010-10)+(500-1000)+(10000-1000)=1000-500+9000=9500美元。
# 答案:
SELECT stock_name, SUM(IF(operation='Sell', price, -price)) AS capital_gain_loss
FROM Stocks
GROUP BY stock_name

1398 购买产品A和产品B却没有购买产品C的顾客

##SQL架构
Create table If Not Exists Customers (customer_id int, customer_name varchar(30))
Create table If Not Exists Orders (order_id int, customer_id int, product_name varchar(30))
Truncate table Customers
insert into Customers (customer_id, customer_name) values ('1', 'Daniel')
insert into Customers (customer_id, customer_name) values ('2', 'Diana')
insert into Customers (customer_id, customer_name) values ('3', 'Elizabeth')
insert into Customers (customer_id, customer_name) values ('4', 'Jhon')
Truncate table Orders
insert into Orders (order_id, customer_id, product_name) values ('10', '1', 'A')
insert into Orders (order_id, customer_id, product_name) values ('20', '1', 'B')
insert into Orders (order_id, customer_id, product_name) values ('30', '1', 'D')
insert into Orders (order_id, customer_id, product_name) values ('40', '1', 'C')
insert into Orders (order_id, customer_id, product_name) values ('50', '2', 'A')
insert into Orders (order_id, customer_id, product_name) values ('60', '3', 'A')
insert into Orders (order_id, customer_id, product_name) values ('70', '3', 'B')
insert into Orders (order_id, customer_id, product_name) values ('80', '3', 'D')
insert into Orders (order_id, customer_id, product_name) values ('90', '4', 'C')
 Customers 表:
+---------------------+---------+
| Column Name
| Type
|
+---------------------+---------+
| customer_id
| int
|
| customer_name
| varchar |
+---------------------+---------+
customer_id 是这张表的主键。
customer_name 是顾客的名称。
 
Orders 表:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| customer_id
| int
|
| product_name
| varchar |
+---------------+---------+
order_id 是这张表的主键。
customer_id 是购买了名为 "product_name" 产品顾客的id。
 
请你设计 SQL 查询来报告购买了产品 A 和产品 B 却没有购买产品 C 的顾客的 ID 和姓名( customer_id 和 customer_name ),我们将基于此结果为他们推荐产品 C 。
您返回的查询结果需要按照 customer_id 排序。
 
查询结果如下例所示。
Customers table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1
| Daniel
|
| 2
| Diana
|
| 3
| Elizabeth
|
| 4
| Jhon
|
+-------------+---------------+
Orders table:
+------------+--------------+---------------+
| order_id
| customer_id
| product_name
|
+------------+--------------+---------------+
| 10
|
1
|
A
|
| 20
|
1
|
B
|
| 30
|
1
|
D
|
| 40
|
1
|
C
|
| 50
|
2
|
A
|
| 60
|
3
|
A
|
| 70
|
3
|
B
|
| 80
|
3
|
D
|
| 90
|
4
|
C
|
+------------+--------------+---------------+
Result table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 3
| Elizabeth
|
+-------------+---------------+
只有 customer_id 为 3 的顾客购买了产品 A 和产品 B ,却没有购买产品 C 。
# 答案:
SELECT
c.customer_id, c.customer_name
FROM
Orders o LEFT JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.customer_id
HAVING
SUM(product_name = 'A') * SUM(product_name = 'B') > 0
AND SUM(product_name = 'C') = 0
ORDER BY c.customer_id

1412 查询成绩处于中游的学生

##SQL架构
表: Student
+---------------------+---------+
| Column Name
| Type
|
+---------------------+---------+
| student_id
| int
|
| student_name
| varchar |
+---------------------+---------+
student_id 是该表主键.
student_name 学生名字.
 
表: Exam
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| exam_id
| int
|
| student_id
| int
|
| score
| int
|
+---------------+---------+
(exam_id, student_id) 是该表主键.
学生 student_id 在测验 exam_id 中得分为 score.
 
成绩处于中游的学生是指至少参加了一次测验, 且得分既不是最高分也不是最低分的学生。
写一个 SQL 语句,找出在 所有 测验中都处于中游的学生 (student_id, student_name)。
不要返回从来没有参加过测验的学生。返回结果表按照 student_id 排序。
查询结果格式如下。
 
Student 表:
+-------------+---------------+
| student_id
| student_name
|
+-------------+---------------+
| 1
| Daniel
|
| 2
| Jade
|
| 3
| Stella
|
| 4
| Jonathan
|
| 5
| Will
|
+-------------+---------------+
Exam 表:
+------------+--------------+-----------+
| exam_id
| student_id
| score
|
+------------+--------------+-----------+
| 10
|
1
|
70
|
| 10
|
2
|
80
|
| 10
|
3
|
90
|
| 20
|
1
|
80
|
| 30
|
1
|
70
|
| 30
|
3
|
80
|
| 30
|
4
|
90
|
| 40
|
1
|
60
|
| 40
|
2
|
70
|
| 40
|
4
|
80
|
+------------+--------------+-----------+
Result 表:
+-------------+---------------+
| student_id
| student_name
|
+-------------+---------------+
| 2
| Jade
|
+-------------+---------------+
对于测验 1: 学生 1 和 3 分别获得了最低分和最高分。
对于测验 2: 学生 1 既获得了最高分, 也获得了最低分。
对于测验 3 和 4: 学生 1 和 4 分别获得了最低分和最高分。
学生 2 和 5 没有在任一场测验中获得了最高分或者最低分。
因为学生 5 从来没有参加过任何测验, 所以他被排除于结果表。
由此, 我们仅仅返回学生 2 的信息。
# 答案:
思路:把成绩正着排一下,再倒着排一下,只要这两列都不是1,那就是中游的数据
select t1.student_id,s.student_name
from
(
select *,
if(dense_rank() over(partition by exam_id order by score desc)=1,1,0) d_rank,
if(dense_rank() over(partition by exam_id order by score )=1,1,0) a_rank
from Exam
) t1 left join Student s on t1.student_id =s.student_id
group by t1.student_id
having sum(d_rank)=0 and sum(a_rank)=0
order by student_id

1440 计算布尔表达式的值

##SQL架构
Create Table If Not Exists Variables (name varchar(3), value int)
Create Table If Not Exists Expressions (left_operand varchar(3), operator ENUM('>', '<', '='), right_operand varchar(3))
Truncate table Variables
insert into Variables (name, value) values ('x', '66')
insert into Variables (name, value) values ('y', '77')
Truncate table Expressions
insert into Expressions (left_operand, operator, right_operand) values ('x', '>', 'y')
insert into Expressions (left_operand, operator, right_operand) values ('x', '<', 'y')
insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'y')
insert into Expressions (left_operand, operator, right_operand) values ('y', '>', 'x')
insert into Expressions (left_operand, operator, right_operand) values ('y', '<', 'x')
insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'x')
表 Variables:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| name
| varchar |
| value
| int
|
+---------------+---------+
name 是该表主键.
该表包含了存储的变量及其对应的值.
 
表 Expressions:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| left_operand
| varchar |
| operator
| enum
|
| right_operand | varchar |
+---------------+---------+
(left_operand, operator, right_operand) 是该表主键.
该表包含了需要计算的布尔表达式.
operator 是枚举类型, 取值于('<', '>', '=')
left_operand 和 right_operand 的值保证存在于 Variables 表单中.
 
写一个 SQL 查询,  以计算表 Expressions 中的布尔表达式.
返回的结果表没有顺序要求.
查询结果格式如下例所示.
Variables 表:
+------+-------+
| name | value |
+------+-------+
| x
| 66
|
| y
| 77
|
+------+-------+
Expressions 表:
+--------------+----------+---------------+
| left_operand | operator | right_operand |
+--------------+----------+---------------+
| x
| >
| y
|
| x
| <
| y
|
| x
| =
| y
|
| y
| >
| x
|
| y
| <
| x
|
| x
| =
| x
|
+--------------+----------+---------------+
Result 表:
+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x
| >
| y
| false |
| x
| <
| y
| true
|
| x
| =
| y
| false |
| y
| >
| x
| true
|
| y
| <
| x
| false |
| x
| =
| x
| true
|
+--------------+----------+---------------+-------+
如上所示, 你需要通过使用 Variables 表来找到 Expressions 表中的每一个布尔表达式的值.
# 答案:
select e.left_operand,e.operator,e.right_operand,
case e.operator
when '>' then if(v1.value>v2.value,'true','false')
when '<' then if(v1.value<v2.value,'true','false')
else
if(v1.value=v2.value,'true','false')
end value
from Expressions e
left join Variables v1 on v1.name = e.left_operand
left join Variables v2 on v2.name = e.right_operand

1445 苹果和橘子

#SQL架构
Create table If Not Exists Sales (sale_date date, fruit ENUM('apples', 'oranges'), sold_num int)
Truncate table Sales
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-01', 'apples', '10')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-01', 'oranges', '8')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-02', 'apples', '15')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-02', 'oranges', '15')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-03', 'apples', '20')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-03', 'oranges', '0')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-04', 'apples', '15')
insert into Sales (sale_date, fruit, sold_num) values ('2020-05-04', 'oranges', '16')
表: Sales
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| sale_date
| date
|
| fruit
| enum
|
| sold_num
| int
|
+---------------+---------+
(sale_date,fruit) 是该表主键.
该表包含了每一天中"苹果" 和 "桔子"的销售情况.
 
写一个 SQL 查询, 报告每一天 苹果 和 桔子 销售的数目的差异.
返回的结果表, 按照格式为 ('YYYY-MM-DD') 的 sale_date 排序.
查询结果表如下例所示:
 
Sales 表:
+------------+------------+-------------+
| sale_date
| fruit
| sold_num
|
+------------+------------+-------------+
| 2020-05-01 | apples
| 10
|
| 2020-05-01 | oranges
| 8
|
| 2020-05-02 | apples
| 15
|
| 2020-05-02 | oranges
| 15
|
| 2020-05-03 | apples
| 20
|
| 2020-05-03 | oranges
| 0
|
| 2020-05-04 | apples
| 15
|
| 2020-05-04 | oranges
| 16
|
+------------+------------+-------------+
Result 表:
+------------+--------------+
| sale_date
| diff
|
+------------+--------------+
| 2020-05-01 | 2
|
| 2020-05-02 | 0
|
| 2020-05-03 | 20
|
| 2020-05-04 | -1
|
+------------+--------------+
在 2020-05-01, 卖了 10 个苹果 和 8 个桔子 (差异为 10 - 8 = 2).
在 2020-05-02, 卖了 15 个苹果 和 15 个桔子 (差异为 15 - 15 = 0).
在 2020-05-03, 卖了 20 个苹果 和 0 个桔子 (差异为 20 - 0 = 20).
在 2020-05-04, 卖了 15 个苹果 和 16 个桔子 (差异为 15 - 16 = -1).
# 答案:
SELECT sale_date,SUM(IF(fruit = 'apples',1,-1) * sold_num) diff
FROM Sales
GROUP BY sale_date
ORDER BY sale_date

1454 活跃用户

##SQL架构
Create table If Not Exists Accounts (id int, name varchar(10))
Create table If Not Exists Logins (id int, login_date date)
Truncate table Accounts
insert into Accounts (id, name) values ('1', 'Winston')
insert into Accounts (id, name) values ('7', 'Jonathan')
Truncate table Logins
insert into Logins (id, login_date) values ('7', '2020-05-30')
insert into Logins (id, login_date) values ('1', '2020-05-30')
insert into Logins (id, login_date) values ('7', '2020-05-31')
insert into Logins (id, login_date) values ('7', '2020-06-01')
insert into Logins (id, login_date) values ('7', '2020-06-02')
insert into Logins (id, login_date) values ('7', '2020-06-02')
insert into Logins (id, login_date) values ('7', '2020-06-03')
insert into Logins (id, login_date) values ('1', '2020-06-07')
insert into Logins (id, login_date) values ('7', '2020-06-10')
表 Accounts:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| name
| varchar |
+---------------+---------+
id 是该表主键.
该表包含账户 id 和账户的用户名.
 
表 Logins:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| login_date
| date
|
+---------------+---------+
该表无主键, 可能包含重复项.
该表包含登录用户的账户 id 和登录日期. 用户也许一天内登录多次.
 
写一个 SQL 查询,  找到活跃用户的 id 和 name.
活跃用户是指那些至少连续 5 天登录账户的用户.
返回的结果表按照 id 排序.
结果表格式如下例所示:
Accounts 表:
+----+----------+
| id | name
|
+----+----------+
| 1
| Winston
|
| 7
| Jonathan |
+----+----------+
Logins 表:
+----+------------+
| id | login_date |
+----+------------+
| 7
| 2020-05-30 |
| 1
| 2020-05-30 |
| 7
| 2020-05-31 |
| 7
| 2020-06-01 |
| 7
| 2020-06-02 |
| 7
| 2020-06-02 |
| 7
| 2020-06-03 |
| 1
| 2020-06-07 |
| 7
| 2020-06-10 |
+----+------------+
Result 表:
+----+----------+
| id | name
|
+----+----------+
| 7
| Jonathan |
+----+----------+
id = 1 的用户 Winston 仅仅在不同的 2 天内登录了 2 次, 所以, Winston 不是活跃用户.
id = 7 的用户 Jonathon 在不同的 6 天内登录了 7 次, , 6 天中有 5 天是连续的, 所以, Jonathan 是活跃用户.
# 答案:
SELECT DISTINCT
Logins.id,
Accounts.name
FROM
(
SELECT
id,
reference_dt,
COUNT(1) cnt
FROM
(
SELECT DISTINCT
id,
login_date,
DATE_SUB(login_date, INTERVAL DENSE_RANK() OVER ( PARTITION BY id ORDER BY login_date ASC ) DAY) reference_dt
FROM
Logins
) Logins
GROUP BY
id,
reference_dt
) Logins
INNER JOIN
Accounts
ON Logins.id = Accounts.id
WHERE
cnt >= 5
ORDER BY
id

1459 矩形面积

##SQL架构
Create table If Not Exists Points (id int, x_value int, y_value int)
Truncate table Points
insert into Points (id, x_value, y_value) values ('1', '2', '7')
insert into Points (id, x_value, y_value) values ('2', '4', '8')
insert into Points (id, x_value, y_value) values ('3', '2', '10')
表: Points
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| id
| int
|
| x_value
| int
|
| y_value
| int
|
+---------------+---------+
id 是该表主键
每个点都用二维坐标 (x_value, y_value) 表示
 
写一个 SQL 语句,报告由表中任意两点可以形成的所有 边与坐标轴平行 且 面积不为零 的矩形。
结果表中的每一行包含三列 (p1, p2, area) 如下:
p1 和 p2 是矩形两个对角的 id
矩形的面积由列 area 表示
请按照面积 area 大小降序排列;如果面积相同的话, 则按照 p1 升序排序;若仍相同,则按 p2 升序排列。
查询结果如下例所示:
Points 表:
+----------+-------------+-------------+
| id
| x_value
| y_value
|
+----------+-------------+-------------+
| 1
| 2
| 7
|
| 2
| 4
| 8
|
| 3
| 2
| 10
|
+----------+-------------+-------------+
Result 表:
+----------+-------------+-------------+
| p1
| p2
| area
|
+----------+-------------+-------------+
| 2
| 3
| 4
|
| 1
| 2
| 2
|
+----------+-------------+-------------+
p1 = 2 且 p2 = 3 时, 面积等于 |4-2| * |8-10| = 4
p1 = 1 且 p2 = 2 时, 面积等于 ||2-4| * |7-8| = 2
p1 = 1 且 p2 = 3 时, 是不可能为矩形的, 面积等于 0
# 答案:
select tmp.id1 P1,tmp.id2 P2,abs(tmp.xv2-tmp.xv1)*abs(tmp.yv2-tmp.yv1) AREA
from
(select p1.id id1, p1.x_value xv1,p1.y_value yv1, p2.id id2,p2.x_value xv2,p2.y_value yv2
from Points p1
cross join Points p2
where p1.x_value<>p2.x_value and p1.y_value<>p2.y_value
) tmp
where tmp.id1<tmp.id2
order by area desc,p1 asc,p2 asc

1468 计算税后工资

##SQL
Create table If Not Exists Salaries (company_id int, employee_id int, employee_name varchar(13), salary int)
Truncate table Salaries
insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '1', 'Tony', '2000')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '2', 'Pronub', '21300')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '3', 'Tyrrox', '10800')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '1', 'Pam', '300')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '7', 'Bassem', '450')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('2', '9', 'Hermione', '700')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '7', 'Bocaben', '100')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '2', 'Ognjen', '2200')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '13', 'Nyancat', '3300')
insert into Salaries (company_id, employee_id, employee_name, salary) values ('3', '15', 'Morninngcat', '7777')
Salaries 表:
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| company_id
| int
|
| employee_id
| int
|
| employee_name | varchar |
| salary
| int
|
+---------------+---------+
(company_id, employee_id) 是这个表的主键
这个表包括员工的company id, id, name 和 salary
 
写一条查询 SQL 来查找每个员工的税后工资
每个公司的税率计算依照以下规则
如果这个公司员工最高工资不到 1000 ,税率为 0%
如果这个公司员工最高工资在 1000 到 10000 之间,税率为 24%
如果这个公司员工最高工资大于 10000 ,税率为 49%
按任意顺序返回结果,税后工资结果取整
 
结果表格式如下例所示:
Salaries 表:
+------------+-------------+---------------+--------+
| company_id | employee_id | employee_name | salary |
+------------+-------------+---------------+--------+
| 1
| 1
| Tony
| 2000
|
| 1
| 2
| Pronub
| 21300
|
| 1
| 3
| Tyrrox
| 10800
|
| 2
| 1
| Pam
| 300
|
| 2
| 7
| Bassem
| 450
|
| 2
| 9
| Hermione
| 700
|
| 3
| 7
| Bocaben
| 100
|
| 3
| 2
| Ognjen
| 2200
|
| 3
| 13
| Nyancat
| 3300
|
| 3
| 15
| Morninngcat
| 7777
|
+------------+-------------+---------------+--------+
Result 表:
+------------+-------------+---------------+--------+
| company_id | employee_id | employee_name | salary |
+------------+-------------+---------------+--------+
| 1
| 1
| Tony
| 1020
|
| 1
| 2
| Pronub
| 10863
|
| 1
| 3
| Tyrrox
| 5508
|
| 2
| 1
| Pam
| 300
|
| 2
| 7
| Bassem
| 450
|
| 2
| 9
| Hermione
| 700
|
| 3
| 7
| Bocaben
| 76
|
| 3
| 2
| Ognjen
| 1672
|
| 3
| 13
| Nyancat
| 2508
|
| 3
| 15
| Morninngcat
| 5911
|
+------------+-------------+---------------+--------+
对于公司 1 ,最高工资是 21300 ,其每个员工的税率为 49%
对于公司 2 ,最高工资是 700 ,其每个员工税率为 0%
对于公司 3 ,最高工资是 7777 ,其每个员工税率是 24%
税后工资计算 = 工资 - ( 税率 / 100)*工资
对于上述案例,Morninngcat 的税后工资 = 7777 - 7777 * ( 24 / 100) = 7777 - 1866.48 = 5910.52 ,取整为 5911
# 答案:
select
s.company_id,
s.employee_id,
s.employee_name,
round(s.salary*(1-companytaxrate.taxrate)) as salary
from salaries s join
(select
company_id,
case
when max(salary)<1000 then 0
when max(salary) between 1000 and 10000 then 0.24
when max(salary)>10000 then 0.49
end as taxrate
from salaries
group by company_id) as companytaxrate on s.company_id=companytaxrate.company_id;

1479 周内每天的销售情况

##SQL架构
Create table If Not Exists Orders (order_id int, customer_id int, order_date date, item_id varchar(30), quantity int)
Create table If Not Exists Items (item_id varchar(30), item_name varchar(30), item_category varchar(30))
Truncate table Orders
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('1', '1', '2020-06-01', '1', '10')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('2', '1', '2020-06-08', '2', '10')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('3', '2', '2020-06-02', '1', '5')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('4', '3', '2020-06-03', '3', '5')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('5', '4', '2020-06-04', '4', '1')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('6', '4', '2020-06-05', '5', '5')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('7', '5', '2020-06-05', '1', '10')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('8', '5', '2020-06-14', '4', '5')
insert into Orders (order_id, customer_id, order_date, item_id, quantity) values ('9', '5', '2020-06-21', '3', '5')
Truncate table Items
insert into Items (item_id, item_name, item_category) values ('1', 'LC Alg. Book', 'Book')
insert into Items (item_id, item_name, item_category) values ('2', 'LC DB. Book', 'Book')
insert into Items (item_id, item_name, item_category) values ('3', 'LC SmarthPhone', 'Phone')
insert into Items (item_id, item_name, item_category) values ('4', 'LC Phone 2020', 'Phone')
insert into Items (item_id, item_name, item_category) values ('5', 'LC SmartGlass', 'Glasses')
insert into Items (item_id, item_name, item_category) values ('6', 'LC T-Shirt XL', 'T-shirt')
表:Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| customer_id
| int
|
| order_date
| date
|
| item_id
| varchar |
| quantity
| int
|
+---------------+---------+
(order_id, item_id) 是该表主键
该表包含了订单信息
order_date 是id为 item_id 的商品被id为 customer_id 的消费者订购的日期.
表:Items
+---------------------+---------+
| Column Name
| Type
|
+---------------------+---------+
| item_id
| varchar |
| item_name
| varchar |
| item_category
| varchar |
+---------------------+---------+
item_id 是该表主键
item_name 是商品的名字
item_category 是商品的类别
 
你是企业主,想要获得分类商品和周内每天的销售报告。
写一个SQL语句,报告 周内每天 每个商品类别下订购了多少单位。
返回结果表单 按商品类别排序 。
查询结果格式如下例所示:
 
Orders 表:
+------------+--------------+-------------+--------------+-------------+
| order_id
| customer_id
| order_date
| item_id
| quantity
|
+------------+--------------+-------------+--------------+-------------+
| 1
| 1
| 2020-06-01
| 1
| 10
|
| 2
| 1
| 2020-06-08
| 2
| 10
|
| 3
| 2
| 2020-06-02
| 1
| 5
|
| 4
| 3
| 2020-06-03
| 3
| 5
|
| 5
| 4
| 2020-06-04
| 4
| 1
|
| 6
| 4
| 2020-06-05
| 5
| 5
|
| 7
| 5
| 2020-06-05
| 1
| 10
|
| 8
| 5
| 2020-06-14
| 4
| 5
|
| 9
| 5
| 2020-06-21
| 3
| 5
|
+------------+--------------+-------------+--------------+-------------+
Items 表:
+------------+----------------+---------------+
| item_id
| item_name
| item_category |
+------------+----------------+---------------+
| 1
| LC Alg. Book
| Book
|
| 2
| LC DB. Book
| Book
|
| 3
| LC SmarthPhone | Phone
|
| 4
| LC Phone 2020
| Phone
|
| 5
| LC SmartGlass
| Glasses
|
| 6
| LC T-Shirt XL
| T-Shirt
|
+------------+----------------+---------------+
Result 表:
+------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| Category
| Monday
| Tuesday
| Wednesday | Thursday
| Friday
| Saturday
| Sunday
|
+------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| Book
| 20
| 5
| 0
| 0
| 10
| 0
| 0
|
| Glasses
| 0
| 0
| 0
| 0
| 5
| 0
| 0
|
| Phone
| 0
| 0
| 5
| 1
| 0
| 0
| 10
|
| T-Shirt
| 0
| 0
| 0
| 0
| 0
| 0
| 0
|
+------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
在周一(2020-06-01, 2020-06-08),Book分类(ids: 1, 2)下,总共销售了20个单位(10 + 10)
在周二(2020-06-02),Book分类(ids: 1, 2)下,总共销售了5个单位
在周三(2020-06-03),Phone分类(ids: 3, 4)下,总共销售了5个单位
在周四(2020-06-04),Phone分类(ids: 3, 4)下,总共销售了1个单位
在周五(2020-06-05),Book分类(ids: 1, 2)下,总共销售了10个单位,Glasses分类(ids: 5)下,总共销售了5个单位
在周六, 没有商品销售
在周天(2020-06-14, 2020-06-21),Phone分类(ids: 3, 4)下,总共销售了10个单位(5 + 5)
没有销售 T-Shirt 类别的商品
# 答案:
select distinct b.item_category as Category,
ifnull(sum(case when dayofweek(a.order_date) = 2 then a.quantity end),0) Monday,
ifnull(sum(case when dayofweek(a.order_date) = 3 then a.quantity end),0) Tuesday,
ifnull(sum(case when dayofweek(a.order_date) = 4 then a.quantity end),0) Wednesday,
ifnull(sum(case when dayofweek(a.order_date) = 5 then a.quantity end),0) Thursday,
ifnull(sum(case when dayofweek(a.order_date) = 6 then a.quantity end),0) Friday,
ifnull(sum(case when dayofweek(a.order_date) = 7 then a.quantity end),0) Saturday,
ifnull(sum(case when dayofweek(a.order_date) = 1 then a.quantity end),0) Sunday
from Orders a right join Items b
on a.item_id = b.item_id
group by Category
order by Category

1501 可以放心投资的国家

##SQL架构
Create table If Not Exists Person (id int, name varchar(15), phone_number varchar(11))
Create table If Not Exists Country (name varchar(15), country_code varchar(3))
Create table If Not Exists Calls (caller_id int, callee_id int, duration int)
Truncate table Person
insert into Person (id, name, phone_number) values ('3', 'Jonathan', '051-1234567')
insert into Person (id, name, phone_number) values ('12', 'Elvis', '051-7654321')
insert into Person (id, name, phone_number) values ('1', 'Moncef', '212-1234567')
insert into Person (id, name, phone_number) values ('2', 'Maroua', '212-6523651')
insert into Person (id, name, phone_number) values ('7', 'Meir', '972-1234567')
insert into Person (id, name, phone_number) values ('9', 'Rachel', '972-0011100')
Truncate table Country
insert into Country (name, country_code) values ('Peru', '051')
insert into Country (name, country_code) values ('Israel', '972')
insert into Country (name, country_code) values ('Morocco', '212')
insert into Country (name, country_code) values ('Germany', '049')
insert into Country (name, country_code) values ('Ethiopia', '251')
Truncate table Calls
insert into Calls (caller_id, callee_id, duration) values ('1', '9', '33')
insert into Calls (caller_id, callee_id, duration) values ('2', '9', '4')
insert into Calls (caller_id, callee_id, duration) values ('1', '2', '59')
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '102')
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '330')
insert into Calls (caller_id, callee_id, duration) values ('12', '3', '5')
insert into Calls (caller_id, callee_id, duration) values ('7', '9', '13')
insert into Calls (caller_id, callee_id, duration) values ('7', '1', '3')
insert into Calls (caller_id, callee_id, duration) values ('9', '7', '1')
insert into Calls (caller_id, callee_id, duration) values ('1', '7', '7')
表 Person:
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| id
| int
|
| name
| varchar |
| phone_number
| varchar |
+----------------+---------+
id 是该表主键.
该表每一行包含一个人的名字和电话号码.
电话号码的格式是:'xxx-yyyyyyy', 其中xxx是国家码(3个字符), yyyyyyy是电话号码(7个字符), x和y都表示数字. 同时, 国家码和电话号码都可以包含前导0.
表 Country:
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| name
| varchar |
| country_code
| varchar |
+----------------+---------+
country_code是该表主键.
该表每一行包含国家名和国家码. country_code的格式是'xxx', x是数字.
 
表 Calls:
+-------------+------+
| Column Name | Type |
+-------------+------+
| caller_id
| int
|
| callee_id
| int
|
| duration
| int
|
+-------------+------+
该表无主键, 可能包含重复行.
每一行包含呼叫方id, 被呼叫方id和以分钟为单位的通话时长. caller_id != callee_id
一家电信公司想要投资新的国家. 该公司想要投资的国家是:  该国的平均通话时长要严格地大于全球平均通话时长.
写一段 SQL,  找到所有该公司可以投资的国家.
返回的结果表没有顺序要求.
查询的结果格式如下例所示.
Person 表:
+----+----------+--------------+
| id | name
| phone_number |
+----+----------+--------------+
| 3
| Jonathan | 051-1234567
|
| 12 | Elvis
| 051-7654321
|
| 1
| Moncef
| 212-1234567
|
| 2
| Maroua
| 212-6523651
|
| 7
| Meir
| 972-1234567
|
| 9
| Rachel
| 972-0011100
|
+----+----------+--------------+
Country 表:
+----------+--------------+
| name
| country_code |
+----------+--------------+
| Peru
| 051
|
| Israel
| 972
|
| Morocco
| 212
|
| Germany
| 049
|
| Ethiopia | 251
|
+----------+--------------+
Calls 表:
+-----------+-----------+----------+
| caller_id | callee_id | duration |
+-----------+-----------+----------+
| 1
| 9
| 33
|
| 2
| 9
| 4
|
| 1
| 2
| 59
|
| 3
| 12
| 102
|
| 3
| 12
| 330
|
| 12
| 3
| 5
|
| 7
| 9
| 13
|
| 7
| 1
| 3
|
| 9
| 7
| 1
|
| 1
| 7
| 7
|
+-----------+-----------+----------+
Result 表:
+----------+
| country
|
+----------+
| Peru
|
+----------+
国家Peru的平均通话时长是 (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667
国家Israel的平均通话时长是 (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500
国家Morocco的平均通话时长是 (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000
全球平均通话时长 = (2 * (33 + 4 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000
所以, Peru是唯一的平均通话时长大于全球平均通话时长的国家, 也是唯一的推荐投资的国家.
SELECT c.name AS country
FROM Calls, Person, Country c
WHERE (caller_id = id OR callee_id = id) AND country_code = LEFT(phone_number, 3)
GROUP BY country_code
HAVING AVG(duration) > (SELECT AVG(duration) FROM Calls);

1532 最近三笔订单

##SQL架构
Create table If Not Exists Customers (customer_id int, name varchar(10))
Create table If Not Exists Orders (order_id int, order_date date, customer_id int, cost int)
Truncate table Customers
insert into Customers (customer_id, name) values ('1', 'Winston')
insert into Customers (customer_id, name) values ('2', 'Jonathan')
insert into Customers (customer_id, name) values ('3', 'Annabelle')
insert into Customers (customer_id, name) values ('4', 'Marwan')
insert into Customers (customer_id, name) values ('5', 'Khaled')
Truncate table Orders
insert into Orders (order_id, order_date, customer_id, cost) values ('1', '2020-07-31', '1', '30')
insert into Orders (order_id, order_date, customer_id, cost) values ('2', '2020-7-30', '2', '40')
insert into Orders (order_id, order_date, customer_id, cost) values ('3', '2020-07-31', '3', '70')
insert into Orders (order_id, order_date, customer_id, cost) values ('4', '2020-07-29', '4', '100')
insert into Orders (order_id, order_date, customer_id, cost) values ('5', '2020-06-10', '1', '1010')
insert into Orders (order_id, order_date, customer_id, cost) values ('6', '2020-08-01', '2', '102')
insert into Orders (order_id, order_date, customer_id, cost) values ('7', '2020-08-01', '3', '111')
insert into Orders (order_id, order_date, customer_id, cost) values ('8', '2020-08-03', '1', '99')
insert into Orders (order_id, order_date, customer_id, cost) values ('9', '2020-08-07', '2', '32')
insert into Orders (order_id, order_date, customer_id, cost) values ('10', '2020-07-15', '1', '2')
表:Customers
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| name
| varchar |
+---------------+---------+
customer_id 是该表主键
该表包含消费者的信息
 
表:Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| order_date
| date
|
| customer_id
| int
|
| cost
| int
|
+---------------+---------+
order_id 是该表主键
该表包含id为customer_id的消费者的订单信息
每一个消费者 每天一笔订单
 
写一个 SQL 语句,找到每个用户的最近三笔订单。如果用户的订单少于 3 笔,则返回他的全部订单。
返回的结果按照 customer_name 升序排列。如果排名有相同,则继续按照 customer_id 升序排列。如果排名还有相同,则继续按照 order_date 降序排列。
查询结果格式如下例所示:
Customers
+-------------+-----------+
| customer_id | name
|
+-------------+-----------+
| 1
| Winston
|
| 2
| Jonathan
|
| 3
| Annabelle |
| 4
| Marwan
|
| 5
| Khaled
|
+-------------+-----------+
Orders
+----------+------------+-------------+------+
| order_id | order_date | customer_id | cost |
+----------+------------+-------------+------+
| 1
| 2020-07-31 | 1
| 30
|
| 2
| 2020-07-30 | 2
| 40
|
| 3
| 2020-07-31 | 3
| 70
|
| 4
| 2020-07-29 | 4
| 100
|
| 5
| 2020-06-10 | 1
| 1010 |
| 6
| 2020-08-01 | 2
| 102
|
| 7
| 2020-08-01 | 3
| 111
|
| 8
| 2020-08-03 | 1
| 99
|
| 9
| 2020-08-07 | 2
| 32
|
| 10
| 2020-07-15 | 1
| 2
|
+----------+------------+-------------+------+
Result table:
+---------------+-------------+----------+------------+
| customer_name | customer_id | order_id | order_date |
+---------------+-------------+----------+------------+
| Annabelle
| 3
| 7
| 2020-08-01 |
| Annabelle
| 3
| 3
| 2020-07-31 |
| Jonathan
| 2
| 9
| 2020-08-07 |
| Jonathan
| 2
| 6
| 2020-08-01 |
| Jonathan
| 2
| 2
| 2020-07-30 |
| Marwan
| 4
| 4
| 2020-07-29 |
| Winston
| 1
| 8
| 2020-08-03 |
| Winston
| 1
| 1
| 2020-07-31 |
| Winston
| 1
| 10
| 2020-07-15 |
+---------------+-------------+----------+------------+
Winston 有 4 笔订单, 排除了 "2020-06-10" 的订单, 因为它是最老的订单。
Annabelle 只有 2 笔订单, 全部返回。
Jonathan 恰好有 3 笔订单。
Marwan 只有 1 笔订单。
结果表我们按照 customer_name 升序排列,customer_id 升序排列,order_date 降序排列。
# 答案:
select b.name as customer_name, a.customer_id, a.order_id, a.order_date
from
(
select order_id, customer_id, row_number()over(partition by customer_id order by order_date desc) as rn, order_date
from Orders
)a
join customers b
on a.customer_id = b.customer_id
where a.rn < 4
order by customer_name, customer_id, order_date desc

1549 每件商品对的最新订单

##SQL架构
Create table If Not Exists Customers (customer_id int, name varchar(10))
Create table If Not Exists Orders (order_id int, order_date date, customer_id int, product_id int)
Create table If Not Exists Products (product_id int, product_name varchar(20), price int)
Truncate table Customers
insert into Customers (customer_id, name) values ('1', 'Winston')
insert into Customers (customer_id, name) values ('2', 'Jonathan')
insert into Customers (customer_id, name) values ('3', 'Annabelle')
insert into Customers (customer_id, name) values ('4', 'Marwan')
insert into Customers (customer_id, name) values ('5', 'Khaled')
Truncate table Orders
insert into Orders (order_id, order_date, customer_id, product_id) values ('1', '2020-07-31', '1', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('2', '2020-7-30', '2', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('3', '2020-08-29', '3', '3')
insert into Orders (order_id, order_date, customer_id, product_id) values ('4', '2020-07-29', '4', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('5', '2020-06-10', '1', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('6', '2020-08-01', '2', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('7', '2020-08-01', '3', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('8', '2020-08-03', '1', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('9', '2020-08-07', '2', '3')
insert into Orders (order_id, order_date, customer_id, product_id) values ('10', '2020-07-15', '1', '2')
Truncate table Products
insert into Products (product_id, product_name, price) values ('1', 'keyboard', '120')
insert into Products (product_id, product_name, price) values ('2', 'mouse', '80')
insert into Products (product_id, product_name, price) values ('3', 'screen', '600')
insert into Products (product_id, product_name, price) values ('4', 'hard disk', '450')
表: Customers
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| name
| varchar |
+---------------+---------+
customer_id 是该表主键.
该表包含消费者的信息.
 
表: Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| order_date
| date
|
| customer_id
| int
|
| product_id
| int
|
+---------------+---------+
order_id 是该表主键.
该表包含消费者customer_id产生的订单.
不会有商品被相同的用户在一天内下单超过一次.
 
表: Products
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| product_id
| int
|
| product_name
| varchar |
| price
| int
|
+---------------+---------+
product_id 是该表主键.
该表包含所有商品的信息.
 
写一个SQL 语句, 找到每件商品的最新订单(可能有多个).
返回的结果以 product_name 升序排列, 如果有排序相同, 再以 product_id 升序排列. 如果还有排序相同, 再以 order_id 升序排列.
查询结果格式如下例所示:
Customers
+-------------+-----------+
| customer_id | name
|
+-------------+-----------+
| 1
| Winston
|
| 2
| Jonathan
|
| 3
| Annabelle |
| 4
| Marwan
|
| 5
| Khaled
|
+-------------+-----------+
Orders
+----------+------------+-------------+------------+
| order_id | order_date | customer_id | product_id |
+----------+------------+-------------+------------+
| 1
| 2020-07-31 | 1
| 1
|
| 2
| 2020-07-30 | 2
| 2
|
| 3
| 2020-08-29 | 3
| 3
|
| 4
| 2020-07-29 | 4
| 1
|
| 5
| 2020-06-10 | 1
| 2
|
| 6
| 2020-08-01 | 2
| 1
|
| 7
| 2020-08-01 | 3
| 1
|
| 8
| 2020-08-03 | 1
| 2
|
| 9
| 2020-08-07 | 2
| 3
|
| 10
| 2020-07-15 | 1
| 2
|
+----------+------------+-------------+------------+
Products
+------------+--------------+-------+
| product_id | product_name | price |
+------------+--------------+-------+
| 1
| keyboard
| 120
|
| 2
| mouse
| 80
|
| 3
| screen
| 600
|
| 4
| hard disk
| 450
|
+------------+--------------+-------+
Result
+--------------+------------+----------+------------+
| product_name | product_id | order_id | order_date |
+--------------+------------+----------+------------+
| keyboard
| 1
| 6
| 2020-08-01 |
| keyboard
| 1
| 7
| 2020-08-01 |
| mouse
| 2
| 8
| 2020-08-03 |
| screen
| 3
| 3
| 2020-08-29 |
+--------------+------------+----------+------------+
keyboard 的最新订单在2020-08-01, 在这天有两次下单.
mouse 的最新订单在2020-08-03, 在这天只有一次下单.
screen 的最新订单在2020-08-29, 在这天只有一次下单.
hard disk 没有被下单, 我们不把它包含在结果表中.
# 答案:
select product_name, o.product_id, order_id, order_date
from Orders o left join Products p
using(product_id)
where (product_id, order_date) in
(
select product_id, max(order_date) order_date
from Orders
group by product_id
)
order by product_name, product_id, order_id

1555 银行账户概要

##SQL架构
Create table If Not Exists Users (user_id int, user_name varchar(20), credit int)
Create table If Not Exists Transactions (trans_id int, paid_by int, paid_to int, amount int, transacted_on date)
Truncate table Users
insert into Users (user_id, user_name, credit) values ('1', 'Moustafa', '100')
insert into Users (user_id, user_name, credit) values ('2', 'Jonathan', '200')
insert into Users (user_id, user_name, credit) values ('3', 'Winston', '10000')
insert into Users (user_id, user_name, credit) values ('4', 'Luis', '800')
Truncate table Transactions
insert into Transactions (trans_id, paid_by, paid_to, amount, transacted_on) values ('1', '1', '3', '400', '2020-08-01')
insert into Transactions (trans_id, paid_by, paid_to, amount, transacted_on) values ('2', '3', '2', '500', '2020-08-02')
insert into Transactions (trans_id, paid_by, paid_to, amount, transacted_on) values ('3', '2', '1', '200', '2020-08-03')
用户表: Users
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| user_id
| int
|
| user_name
| varchar |
| credit
| int
|
+--------------+---------+
user_id 是这个表的主键。
表中的每一列包含每一个用户当前的额度信息。
 
交易表:Transactions
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| trans_id
| int
|
| paid_by
| int
|
| paid_to
| int
|
| amount
| int
|
| transacted_on | date
|
+---------------+---------+
trans_id 是这个表的主键。
表中的每一列包含银行的交易信息。
ID 为 paid_by 的用户给 ID 为 paid_to 的用户转账。
 
力扣银行 (LCB) 帮助程序员们完成虚拟支付。我们的银行在表 Transaction 中记录每条交易信息,我们要查询每个用户的当前余额,并检查他们是否已透支(当前额度小于 0)。
写一条 SQL 语句,查询:
user_id 用户 ID
user_name 用户名
credit 完成交易后的余额
credit_limit_breached 检查是否透支 ("Yes" 或 "No")
以任意顺序返回结果表。
 
查询格式见如下示例:
Users 表:
+------------+--------------+-------------+
| user_id
| user_name
| credit
|
+------------+--------------+-------------+
| 1
| Moustafa
| 100
|
| 2
| Jonathan
| 200
|
| 3
| Winston
| 10000
|
| 4
| Luis
| 800
|
+------------+--------------+-------------+
Transactions 表:
+------------+------------+------------+----------+---------------+
| trans_id
| paid_by
| paid_to
| amount
| transacted_on |
+------------+------------+------------+----------+---------------+
| 1
| 1
| 3
| 400
| 2020-08-01
|
| 2
| 3
| 2
| 500
| 2020-08-02
|
| 3
| 2
| 1
| 200
| 2020-08-03
|
+------------+------------+------------+----------+---------------+
结果表:
+------------+------------+------------+-----------------------+
| user_id
| user_name
| credit
| credit_limit_breached |
+------------+------------+------------+-----------------------+
| 1
| Moustafa
| -100
| Yes
|
| 2
| Jonathan
| 500
| No
|
| 3
| Winston
| 9900
| No
|
| 4
| Luis
| 800
| No
|
+------------+------------+------------+-----------------------+
Moustafa 在 "2020-08-01" 支付了 $400 并在 "2020-08-03" 收到了 $200 ,当前额度 (100 -400 +200) = -$100
Jonathan 在 "2020-08-02" 收到了 $500 并在 "2020-08-08" 支付了 $200 ,当前额度 (200 +500 -200) = $500
Winston 在 "2020-08-01" 收到了 $400 并在 "2020-08-03" 支付了 $500 ,当前额度 (10000 +400 -500) = $9900
Luis 未收到任何转账信息,额度 = $800
# 答案:
select user_id,
user_name,
ifnull(sum(if(u.user_id=t.paid_by,-amount,amount)),0)+credit as credit,
if((ifnull(sum(if(u.user_id=t.paid_by,-amount,amount)),0)+credit)<0,'Yes','No') as credit_limit_breached
from Users u left
join Transactions T
on u.user_id=t.paid_by or u.user_id=t.paid_to
group by user_id

1596 每位顾客最经常订购的商品

##SQL架构
Create table If Not Exists Customers (customer_id int, name varchar(10))
Create table If Not Exists Orders (order_id int, order_date date, customer_id int, product_id int)
Create table If Not Exists Products (product_id int, product_name varchar(20), price int)
Truncate table Customers
insert into Customers (customer_id, name) values ('1', 'Alice')
insert into Customers (customer_id, name) values ('2', 'Bob')
insert into Customers (customer_id, name) values ('3', 'Tom')
insert into Customers (customer_id, name) values ('4', 'Jerry')
insert into Customers (customer_id, name) values ('5', 'John')
Truncate table Orders
insert into Orders (order_id, order_date, customer_id, product_id) values ('1', '2020-07-31', '1', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('2', '2020-7-30', '2', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('3', '2020-08-29', '3', '3')
insert into Orders (order_id, order_date, customer_id, product_id) values ('4', '2020-07-29', '4', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('5', '2020-06-10', '1', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('6', '2020-08-01', '2', '1')
insert into Orders (order_id, order_date, customer_id, product_id) values ('7', '2020-08-01', '3', '3')
insert into Orders (order_id, order_date, customer_id, product_id) values ('8', '2020-08-03', '1', '2')
insert into Orders (order_id, order_date, customer_id, product_id) values ('9', '2020-08-07', '2', '3')
insert into Orders (order_id, order_date, customer_id, product_id) values ('10', '2020-07-15', '1', '2')
Truncate table Products
insert into Products (product_id, product_name, price) values ('1', 'keyboard', '120')
insert into Products (product_id, product_name, price) values ('2', 'mouse', '80')
insert into Products (product_id, product_name, price) values ('3', 'screen', '600')
insert into Products (product_id, product_name, price) values ('4', 'hard disk', '450')
表:Customers
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| name
| varchar |
+---------------+---------+
customer_id 是该表主键
该表包含所有顾客的信息
 
表:Orders
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| order_id
| int
|
| order_date
| date
|
| customer_id
| int
|
| product_id
| int
|
+---------------+---------+
order_id 是该表主键
该表包含顾客 customer_id 的订单信息
没有顾客会在一天内订购相同的商品 多于一次
 
表:Products
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| product_id
| int
|
| product_name
| varchar |
| price
| int
|
+---------------+---------+
product_id 是该表主键
该表包含了所有商品的信息
 
写一个 SQL 语句,找到每一个顾客最经常订购的商品。
结果表单应该有每一位至少下过一次单的顾客 customer_id , 他最经常订购的商品的 product_id 和 product_name。
返回结果 没有顺序要求。
查询结果格式如下例所示:
Customers
+-------------+-------+
| customer_id | name
|
+-------------+-------+
| 1
| Alice |
| 2
| Bob
|
| 3
| Tom
|
| 4
| Jerry |
| 5
| John
|
+-------------+-------+
Orders
+----------+------------+-------------+------------+
| order_id | order_date | customer_id | product_id |
+----------+------------+-------------+------------+
| 1
| 2020-07-31 | 1
| 1
|
| 2
| 2020-07-30 | 2
| 2
|
| 3
| 2020-08-29 | 3
| 3
|
| 4
| 2020-07-29 | 4
| 1
|
| 5
| 2020-06-10 | 1
| 2
|
| 6
| 2020-08-01 | 2
| 1
|
| 7
| 2020-08-01 | 3
| 3
|
| 8
| 2020-08-03 | 1
| 2
|
| 9
| 2020-08-07 | 2
| 3
|
| 10
| 2020-07-15 | 1
| 2
|
+----------+------------+-------------+------------+
Products
+------------+--------------+-------+
| product_id | product_name | price |
+------------+--------------+-------+
| 1
| keyboard
| 120
|
| 2
| mouse
| 80
|
| 3
| screen
| 600
|
| 4
| hard disk
| 450
|
+------------+--------------+-------+
Result 表:
+-------------+------------+--------------+
| customer_id | product_id | product_name |
+-------------+------------+--------------+
| 1
| 2
| mouse
|
| 2
| 1
| keyboard
|
| 2
| 2
| mouse
|
| 2
| 3
| screen
|
| 3
| 3
| screen
|
| 4
| 1
| keyboard
|
+-------------+------------+--------------+
Alice (customer 1) 三次订购鼠标, 一次订购键盘, 所以鼠标是 Alice 最经常订购的商品.
Bob (customer 2) 一次订购键盘, 一次订购鼠标, 一次订购显示器, 所以这些都是 Bob 最经常订购的商品.
Tom (customer 3) 只两次订购显示器, 所以显示器是 Tom 最经常订购的商品.
Jerry (customer 4) 只一次订购键盘, 所以键盘是 Jerry 最经常订购的商品.
John (customer 5) 没有订购过商品, 所以我们并没有把 John 包含在结果表中.
# 答案:
select a.customer_id, a.product_id, b.product_name
from
(
select customer_id, product_id,rank()over(partition by customer_id order by cnt desc) as rk
from
(
select customer_id, product_id,count(1) as cnt
from Orders
group by customer_id, product_id
)a
)a
join Products b
on a.product_id = b.product_id
where a.rk = 1
order by customer_id, product_id

1613 找到遗失的ID

##SQL架构
Create table If Not Exists Customers (customer_id int, customer_name varchar(20))
Truncate table Customers
insert into Customers (customer_id, customer_name) values ('1', 'Alice')
insert into Customers (customer_id, customer_name) values ('4', 'Bob')
insert into Customers (customer_id, customer_name) values ('5', 'Charlie')
表: Customers
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| customer_id
| int
|
| customer_name | varchar |
+---------------+---------+
customer_id 是该表主键.
该表第一行包含了顾客的名字和id.
 
写一个 SQL 语句, 找到所有遗失的顾客id. 遗失的顾客id是指那些不在 Customers 表中, 值却处于 1 和表中最大 customer_id 之间的id.
注意: 最大的 customer_id 值不会超过 100.
返回结果按 ids 升序排列
查询结果格式如下例所示.
 
Customers 表:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1
| Alice
|
| 4
| Bob
|
| 5
| Charlie
|
+-------------+---------------+
Result 表:
+-----+
| ids |
+-----+
| 2
|
| 3
|
+-----+
表中最大的customer_id是5, 所以在范围[1,5]内, ID2和3从表中遗失.
# 答案:
WITH t1 as (
SELECT 1 as a
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
),
t2 as (
SELECT 0 as b
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
),
t3 as (
SELECT 10*a + 1*b as NUMBERS FROM t1, t2
UNION ALL SELECT 100
UNION ALL SELECT a FROM t1
)
SELECT NUMBERS AS 'ids' FROM t3
WHERE NUMBERS < (SELECT MAX(customer_id) FROM Customers)
AND NUMBERS NOT IN (SELECT customer_id FROM Customers)
ORDER BY NUMBERS ASC

1635 Hopper公司查询

##SQL架构
Create table If Not Exists Drivers (driver_id int, join_date date)
Create table If Not Exists Rides (ride_id int, user_id int, requested_at date)
Create table If Not Exists AcceptedRides (ride_id int, driver_id int, ride_distance int, ride_duration int)
Truncate table Drivers
insert into Drivers (driver_id, join_date) values ('10', '2019-12-10')
insert into Drivers (driver_id, join_date) values ('8', '2020-1-13')
insert into Drivers (driver_id, join_date) values ('5', '2020-2-16')
insert into Drivers (driver_id, join_date) values ('7', '2020-3-8')
insert into Drivers (driver_id, join_date) values ('4', '2020-5-17')
insert into Drivers (driver_id, join_date) values ('1', '2020-10-24')
insert into Drivers (driver_id, join_date) values ('6', '2021-1-5')
Truncate table Rides
insert into Rides (ride_id, user_id, requested_at) values ('6', '75', '2019-12-9')
insert into Rides (ride_id, user_id, requested_at) values ('1', '54', '2020-2-9')
insert into Rides (ride_id, user_id, requested_at) values ('10', '63', '2020-3-4')
insert into Rides (ride_id, user_id, requested_at) values ('19', '39', '2020-4-6')
insert into Rides (ride_id, user_id, requested_at) values ('3', '41', '2020-6-3')
insert into Rides (ride_id, user_id, requested_at) values ('13', '52', '2020-6-22')
insert into Rides (ride_id, user_id, requested_at) values ('7', '69', '2020-7-16')
insert into Rides (ride_id, user_id, requested_at) values ('17', '70', '2020-8-25')
insert into Rides (ride_id, user_id, requested_at) values ('20', '81', '2020-11-2')
insert into Rides (ride_id, user_id, requested_at) values ('5', '57', '2020-11-9')
insert into Rides (ride_id, user_id, requested_at) values ('2', '42', '2020-12-9')
insert into Rides (ride_id, user_id, requested_at) values ('11', '68', '2021-1-11')
insert into Rides (ride_id, user_id, requested_at) values ('15', '32', '2021-1-17')
insert into Rides (ride_id, user_id, requested_at) values ('12', '11', '2021-1-19')
insert into Rides (ride_id, user_id, requested_at) values ('14', '18', '2021-1-27')
Truncate table AcceptedRides
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('10', '10', '63', '38')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('13', '10', '73', '96')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('7', '8', '100', '28')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('17', '7', '119', '68')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('20', '1', '121', '92')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('5', '7', '42', '101')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('2', '4', '6', '38')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('11', '8', '37', '43')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('15', '8', '108', '82')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('12', '8', '38', '34')
insert into AcceptedRides (ride_id, driver_id, ride_distance, ride_duration) values ('14', '1', '90', '74')
表: Drivers
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| driver_id
| int
|
| join_date
| date
|
+-------------+---------+
driver_id是该表的主键。
该表的每一行均包含驾驶员的ID以及他们加入Hopper公司的日期。
 
表: Rides
+--------------+---------+
| Column Name
| Type
|
+--------------+---------+
| ride_id
| int
|
| user_id
| int
|
| requested_at | date
|
+--------------+---------+
ride_id是该表的主键。
该表的每一行均包含行程ID(ride_id),用户ID(user_id)以及该行程的日期(requested_at)。
该表中可能有一些不被接受的乘车请求。
 
表: AcceptedRides
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| ride_id
| int
|
| driver_id
| int
|
| ride_distance | int
|
| ride_duration | int
|
+---------------+---------+
ride_id是该表的主键。
该表的每一行都包含已接受的行程信息。
表中的行程信息都在“Rides”表中存在。
 
编写SQL查询以报告2020年每个月的以下统计信息:
截至某月底,当前在Hopper公司工作的驾驶员数量(active_drivers)。
该月接受的乘车次数(accepted_rides)。
返回按month 升序排列的结果表,其中month 是月份的数字(一月是1,二月是2,依此类推)。
查询结果格式如下例所示。
表 Drivers:
+-----------+------------+
| driver_id | join_date
|
+-----------+------------+
| 10
| 2019-12-10 |
| 8
| 2020-1-13
|
| 5
| 2020-2-16
|
| 7
| 2020-3-8
|
| 4
| 2020-5-17
|
| 1
| 2020-10-24 |
| 6
| 2021-1-5
|
+-----------+------------+
表 Rides:
+---------+---------+--------------+
| ride_id | user_id | requested_at |
+---------+---------+--------------+
| 6
| 75
| 2019-12-9
|
| 1
| 54
| 2020-2-9
|
| 10
| 63
| 2020-3-4
|
| 19
| 39
| 2020-4-6
|
| 3
| 41
| 2020-6-3
|
| 13
| 52
| 2020-6-22
|
| 7
| 69
| 2020-7-16
|
| 17
| 70
| 2020-8-25
|
| 20
| 81
| 2020-11-2
|
| 5
| 57
| 2020-11-9
|
| 2
| 42
| 2020-12-9
|
| 11
| 68
| 2021-1-11
|
| 15
| 32
| 2021-1-17
|
| 12
| 11
| 2021-1-19
|
| 14
| 18
| 2021-1-27
|
+---------+---------+--------------+
表 AcceptedRides:
+---------+-----------+---------------+---------------+
| ride_id | driver_id | ride_distance | ride_duration |
+---------+-----------+---------------+---------------+
| 10
| 10
| 63
| 38
|
| 13
| 10
| 73
| 96
|
| 7
| 8
| 100
| 28
|
| 17
| 7
| 119
| 68
|
| 20
| 1
| 121
| 92
|
| 5
| 7
| 42
| 101
|
| 2
| 4
| 6
| 38
|
| 11
| 8
| 37
| 43
|
| 15
| 8
| 108
| 82
|
| 12
| 8
| 38
| 34
|
| 14
| 1
| 90
| 74
|
+---------+-----------+---------------+---------------+
结果表:
+-------+----------------+----------------+
| month | active_drivers | accepted_rides |
+-------+----------------+----------------+
| 1
| 2
| 0
|
| 2
| 3
| 0
|
| 3
| 4
| 1
|
| 4
| 4
| 0
|
| 5
| 5
| 0
|
| 6
| 5
| 1
|
| 7
| 5
| 1
|
| 8
| 5
| 1
|
| 9
| 5
| 0
|
| 10
| 6
| 0
|
| 11
| 6
| 2
|
| 12
| 6
| 1
|
+-------+----------------+----------------+
截至1月底->两个活跃的驾驶员(10,8),没有被接受的行程。
截至2月底->三个活跃的驾驶员(10,8,5),没有被接受的行程。
截至3月底->四个活跃的驾驶员(10,8,5,7),一个被接受的行程(10)。
截至4月底->四个活跃的驾驶员(10,8,5,7),没有被接受的行程。
截至5月底->五个活跃的驾驶员(10,8,5,7,4),没有被接受的行程。
截至6月底->五个活跃的驾驶员(10,8,5,7,4),一个被接受的行程(13)。
截至7月底->五个活跃的驾驶员(10,8,5,7,4),一个被接受的行程(7)。
截至8月底->五个活跃的驾驶员(10,8,5,7,4),一位接受的行程(17)。
截至9月底->五个活跃的驾驶员(10,8,5,7,4),没有被接受的行程。
截至10月底->六个活跃的驾驶员(10,8,5,7,4,1),没有被接受的行程。
截至11月底->六个活跃的驾驶员(10,8,5,7,4,1),两个被接受的行程(20,5)。
截至12月底->六个活跃的驾驶员(10,8,5,7,4,1),一个被接受的行程(2)。
# 答案:
#1递归生成连续12个月
-- with recursive mon_time as
-- (
--
select 1 as month
--
union all
--
select month + 1 from mon_time where month <= 11
-- )
-- select * from mon_time
#2求每个月的司机数
-- select if(year(join_date)<'2020',1,month(join_date)) as month_driver,
-- count(driver_id) as num_driver
-- from drivers
-- where year(join_date)<='2020'
-- group by if(year(join_date)<'2020',1,month(join_date))
#3求每个月的accepted_rides数
-- select month(requested_at) as month_rides,
-- count(ride_id) as num_rides
-- from rides
-- where ride_id in (select ride_id from acceptedrides)
-- and year(requested_at) = '2020'
-- group by month(requested_at)
# 完整代码
with recursive mon_time as
(
select 1 as month
union all
select month + 1 from mon_time where month <= 11
)
select m.month,
sum(ifnull(d.num_driver,0)) over(order by m.month) as active_drivers,
ifnull(num_rides,0) as accepted_rides
from mon_time m
left join
(
select if(year(join_date)<'2020',1,month(join_date)) as month_driver,
count(driver_id) as num_driver
from drivers
where year(join_date)<='2020'
group by if(year(join_date)<'2020',1,month(join_date))
)d
on m.month = month_driver
left join
(
select month(requested_at) as month_rides,
count(ride_id) as num_rides
from rides
where ride_id in (select ride_id from acceptedrides)
and year(requested_at) = '2020'
group by month(requested_at)
)r on m.month = month_rides
group by m.month
order by m.month

1699 两人之间的通话次数

##SQL架构
Create table If Not Exists Calls (from_id int, to_id int, duration int)
Truncate table Calls
insert into Calls (from_id, to_id, duration) values ('1', '2', '59')
insert into Calls (from_id, to_id, duration) values ('2', '1', '11')
insert into Calls (from_id, to_id, duration) values ('1', '3', '20')
insert into Calls (from_id, to_id, duration) values ('3', '4', '100')
insert into Calls (from_id, to_id, duration) values ('3', '4', '200')
insert into Calls (from_id, to_id, duration) values ('3', '4', '200')
insert into Calls (from_id, to_id, duration) values ('4', '3', '499')
表: Calls
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| from_id
| int
|
| to_id
| int
|
| duration
| int
|
+-------------+---------+
该表没有主键,可能存在重复项。
该表包含 from_id 与 to_id 间的一次电话的时长。
from_id != to_id
 
编写 SQL 语句,查询每一对用户 (person1, person2) 之间的通话次数和通话总时长,其中 person1 < person2 。
以任意顺序返回结果表。
查询结果格式如下示例所示:
 
Calls 表:
+---------+-------+----------+
| from_id | to_id | duration |
+---------+-------+----------+
| 1
| 2
| 59
|
| 2
| 1
| 11
|
| 1
| 3
| 20
|
| 3
| 4
| 100
|
| 3
| 4
| 200
|
| 3
| 4
| 200
|
| 4
| 3
| 499
|
+---------+-------+----------+
结果表:
+---------+---------+------------+----------------+
| person1 | person2 | call_count | total_duration |
+---------+---------+------------+----------------+
| 1
| 2
| 2
| 70
|
| 1
| 3
| 1
| 20
|
| 3
| 4
| 4
| 999
|
+---------+---------+------------+----------------+
用户 1 和 2 打过 2 次电话,总时长为 70 (59 + 11)。
用户 1 和 3 打过 1 次电话,总时长为 20。
用户 3 和 4 打过 4 次电话,总时长为 999 (100 + 200 + 200 + 499)。
# 答案:
select person1,person2, count(1) as call_count, sum(duration) as total_duration from (
select if(from_id>to_id,to_id,from_id) as person1,if(from_id>to_id,from_id,to_id) as person2 , duration
from Calls ) c
group by person1,person2

1709 访问日期之间最大的空档期

##SQL架构
Create table If Not Exists UserVisits(user_id int, visit_date date)
Truncate table UserVisits
insert into UserVisits (user_id, visit_date) values ('1', '2020-11-28')
insert into UserVisits (user_id, visit_date) values ('1', '2020-10-20')
insert into UserVisits (user_id, visit_date) values ('1', '2020-12-3')
insert into UserVisits (user_id, visit_date) values ('2', '2020-10-5')
insert into UserVisits (user_id, visit_date) values ('2', '2020-12-9')
insert into UserVisits (user_id, visit_date) values ('3', '2020-11-11')
表: UserVisits
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id
| int
|
| visit_date
| date |
+-------------+------+
该表没有主键。
该表包含用户访问某特定零售商的日期日志。
 
假设今天的日期是 '2021-1-1' 。
编写 SQL 语句,对于每个 user_id ,求出每次访问及其下一个访问(若该次访问是最后一次,则为今天)之间最大的空档期天数 window 。
返回结果表,按用户编号 user_id 排序。
查询格式如下示例所示:
 
UserVisits 表:
+---------+------------+
| user_id | visit_date |
+---------+------------+
| 1
| 2020-11-28 |
| 1
| 2020-10-20 |
| 1
| 2020-12-3
|
| 2
| 2020-10-5
|
| 2
| 2020-12-9
|
| 3
| 2020-11-11 |
+---------+------------+
结果表:
+---------+---------------+
| user_id | biggest_window|
+---------+---------------+
| 1
| 39
|
| 2
| 65
|
| 3
| 51
|
+---------+---------------+
对于第一个用户,问题中的空档期在以下日期之间:
- 2020-10-20 至 2020-11-28 ,共计 39 天。
- 2020-11-28 至 2020-12-3 ,共计 5 天。
- 2020-12-3 至 2021-1-1 ,共计 29 天。
由此得出,最大的空档期为 39 天。
对于第二个用户,问题中的空档期在以下日期之间:
- 2020-10-5 至 2020-12-9 ,共计 65 天。
- 2020-12-9 至 2021-1-1 ,共计 23 天。
由此得出,最大的空档期为 65 天。
对于第三个用户,问题中的唯一空档期在 2020-11-11 至 2021-1-1 之间,共计 51 天。
# 答案:
select user_id,max(diff) as biggest_window
from(
select user_id,datediff(
lead(visit_date,1,'2021-01-01') over(partition by user_id order by visit_date),visit_date
) as diff
from uservisits
) tmp
group by user_id

1715 苹果和橘子的个数

##SQL架构
Create table If Not Exists Boxes (box_id int, chest_id int, apple_count int, orange_count int)
Create table If Not Exists Chests (chest_id int, apple_count int, orange_count int)
Truncate table Boxes
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('2', 'None', '6', '15')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('18', '14', '4', '15')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('19', '3', '8', '4')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('12', '2', '19', '20')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('20', '6', '12', '9')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('8', '6', '9', '9')
insert into Boxes (box_id, chest_id, apple_count, orange_count) values ('3', '14', '16', '7')
Truncate table Chests
insert into Chests (chest_id, apple_count, orange_count) values ('6', '5', '6')
insert into Chests (chest_id, apple_count, orange_count) values ('14', '20', '10')
insert into Chests (chest_id, apple_count, orange_count) values ('2', '8', '8')
insert into Chests (chest_id, apple_count, orange_count) values ('3', '19', '4')
insert into Chests (chest_id, apple_count, orange_count) values ('16', '19', '19')
表: Boxes
+--------------+------+
| Column Name
| Type |
+--------------+------+
| box_id
| int
|
| chest_id
| int
|
| apple_count
| int
|
| orange_count | int
|
+--------------+------+
box_id 是该表的主键。
chest_id 是 chests 表的外键。
该表包含大箱子 (box) 中包含的苹果和橘子的个数。每个大箱子中可能包含一个小盒子 (chest) ,小盒子中也包含若干苹果和橘子。
 
表: Chests
+--------------+------+
| Column Name
| Type |
+--------------+------+
| chest_id
| int
|
| apple_count
| int
|
| orange_count | int
|
+--------------+------+
chest_id 是该表的主键。
该表包含小盒子的信息,以及小盒子中包含的苹果和橘子的个数。
 
编写 SQL 语句,查询每个大箱子中苹果和橘子的个数。如果大箱子中包含小盒子,还应当包含小盒子中苹果和橘子的个数。
以任意顺序返回结果表。
查询结果的格式如下示例所示:
 
Boxes 表:
+--------+----------+-------------+--------------+
| box_id | chest_id | apple_count | orange_count |
+--------+----------+-------------+--------------+
| 2
| null
| 6
| 15
|
| 18
| 14
| 4
| 15
|
| 19
| 3
| 8
| 4
|
| 12
| 2
| 19
| 20
|
| 20
| 6
| 12
| 9
|
| 8
| 6
| 9
| 9
|
| 3
| 14
| 16
| 7
|
+--------+----------+-------------+--------------+
Chests 表:
+----------+-------------+--------------+
| chest_id | apple_count | orange_count |
+----------+-------------+--------------+
| 6
| 5
| 6
|
| 14
| 20
| 10
|
| 2
| 8
| 8
|
| 3
| 19
| 4
|
| 16
| 19
| 19
|
+----------+-------------+--------------+
结果表:
+-------------+--------------+
| apple_count | orange_count |
+-------------+--------------+
| 151
| 123
|
+-------------+--------------+
大箱子 2 中有 6 个苹果和 15 个橘子。
大箱子 18 中有 4 + 20 (在小盒子中) = 24 个苹果和 15 + 10 (在小盒子中) = 25 个橘子。
大箱子 19 中有 8 + 19 (在小盒子中) = 27 个苹果和 4 + 4 (在小盒子中) = 8 个橘子。
大箱子 12 中有 19 + 8 (在小盒子中) = 27 个苹果和 20 + 8 (在小盒子中) = 28 个橘子。
大箱子 20 中有 12 + 5 (在小盒子中) = 17 个苹果和 9 + 6 (在小盒子中) = 15 个橘子。
大箱子 8 中有 9 + 5 (在小盒子中) = 14 个苹果和 9 + 6 (在小盒子中) = 15 个橘子。
大箱子 3 中有 16 + 20 (在小盒子中) = 36 个苹果和 7 + 10 (在小盒子中) = 17 个橘子。
苹果的总个数 = 6 + 24 + 27 + 27 + 17 + 14 + 36 = 151
橘子的总个数 = 15 + 25 + 8 + 28 + 15 + 15 + 17 = 123
# 答案:
select sum(ifnull(b.apple_count, 0) + ifnull(c.apple_count, 0)) as apple_count,
sum(ifnull(b.orange_count, 0) + ifnull(c.orange_count, 0)) as orange_count
from Boxes b
left join Chests c
on b.chest_id = c.chest_id

1747 应该被禁止的Leeflex账户

##SQL架构
Create table If Not Exists LogInfo (account_id int, ip_address int, login datetime, logout datetime)
Truncate table LogInfo
insert into LogInfo (account_id, ip_address, login, logout) values ('1', '1', '2021-02-01 09:00:00', '2021-02-01 09:30:00')
insert into LogInfo (account_id, ip_address, login, logout) values ('1', '2', '2021-02-01 08:00:00', '2021-02-01 11:30:00')
insert into LogInfo (account_id, ip_address, login, logout) values ('2', '6', '2021-02-01 20:30:00', '2021-02-01 22:00:00')
insert into LogInfo (account_id, ip_address, login, logout) values ('2', '7', '2021-02-02 20:30:00', '2021-02-02 22:00:00')
insert into LogInfo (account_id, ip_address, login, logout) values ('3', '9', '2021-02-01 16:00:00', '2021-02-01 16:59:59')
insert into LogInfo (account_id, ip_address, login, logout) values ('3', '13', '2021-02-01 17:00:00', '2021-02-01 17:59:59')
insert into LogInfo (account_id, ip_address, login, logout) values ('4', '10', '2021-02-01 16:00:00', '2021-02-01 17:00:00')
insert into LogInfo (account_id, ip_address, login, logout) values ('4', '11', '2021-02-01 17:00:00', '2021-02-01 17:59:59')
表: LogInfo
+-------------+----------+
| Column Name | Type
|
+-------------+----------+
| account_id
| int
|
| ip_address
| int
|
| login
| datetime |
| logout
| datetime |
+-------------+----------+
该表是没有主键的,它可能包含重复项。
该表包含有关Leetflex帐户的登录和注销日期的信息。 它还包含了该账户用于登录和注销的网络地址的信息。
题目确保每一个注销时间都在登录时间之后。
 
编写一个SQL查询语句,查找那些应该被禁止的Leetflex帐户编号account_id。 如果某个帐户在某一时刻从两个不同的网络地址登录了,则这个帐户应该被禁止。
可以以任何顺序返回结果。
查询结果格式如下例所示:
 
LogInfo table:
+------------+------------+---------------------+---------------------+
| account_id | ip_address | login
| logout
|
+------------+------------+---------------------+---------------------+
| 1
| 1
| 2021-02-01 09:00:00 | 2021-02-01 09:30:00 |
| 1
| 2
| 2021-02-01 08:00:00 | 2021-02-01 11:30:00 |
| 2
| 6
| 2021-02-01 20:30:00 | 2021-02-01 22:00:00 |
| 2
| 7
| 2021-02-02 20:30:00 | 2021-02-02 22:00:00 |
| 3
| 9
| 2021-02-01 16:00:00 | 2021-02-01 16:59:59 |
| 3
| 13
| 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |
| 4
| 10
| 2021-02-01 16:00:00 | 2021-02-01 17:00:00 |
| 4
| 11
| 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |
+------------+------------+---------------------+---------------------+
Result table:
+------------+
| account_id |
+------------+
| 1
|
| 4
|
+------------+
Account ID 1 --> 该账户从 "2021-02-01 09:00:00" 到 "2021-02-01 09:30:00" 在两个不同的网络地址(1 and 2)上激活了。它应该被禁止.
Account ID 2 --> 该账户在两个不同的网络地址 (6, 7) 激活了,但在不同的时间上.
Account ID 3 --> 该账户在两个不同的网络地址 (9, 13) 激活了,虽然是同一天,但时间上没有交集.
Account ID 4 --> 该账户从 "2021-02-01 17:00:00" 到 "2021-02-01 17:00:00" 在两个不同的网络地址 (10 and 11)上激活了。它应该被禁止.
# 答案:
SELECT DISTINCT a.account_id
FROM LogInfo a
INNER JOIN LogInfo b
ON a.account_id = b.account_id AND a.ip_address <> b.ip_address AND ((a.login between b.login and b.logout) OR (a.logout between b.login and b.logout))

1767 寻找没有被执行的任务对

##SQL架构
Create table If Not Exists Tasks (task_id int, subtasks_count int)
Create table If Not Exists Executed (task_id int, subtask_id int)
Truncate table Tasks
insert into Tasks (task_id, subtasks_count) values ('1', '3')
insert into Tasks (task_id, subtasks_count) values ('2', '2')
insert into Tasks (task_id, subtasks_count) values ('3', '4')
Truncate table Executed
insert into Executed (task_id, subtask_id) values ('1', '2')
insert into Executed (task_id, subtask_id) values ('3', '1')
insert into Executed (task_id, subtask_id) values ('3', '2')
insert into Executed (task_id, subtask_id) values ('3', '3')
insert into Executed (task_id, subtask_id) values ('3', '4')
表:Tasks
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| task_id
| int
|
| subtasks_count | int
|
+----------------+---------+
task_id 是这个表的主键。
task_id 表示的为主任务的id,每一个task_id被分为了多个子任务(subtasks),subtasks_count表示为子任务的个数(n),它的值表示了子任务的索引从1到n。
本表保证2 <=subtasks_count<= 20。
表: Executed
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| task_id
| int
|
| subtask_id
| int
|
+---------------+---------+
(task_id, subtask_id) 是这个表的主键。
每一行表示标记为task_id的主任务与标记为subtask_id的子任务被成功执行。
本表保证,对于每一个task_id,subtask_id <= subtasks_count。
 
请试写一个SQL查询语句报告没有被执行的(主任务,子任务)对,即没有被执行的(task_id, subtask_id)。
以 任何顺序 返回即可。
查询结果格式如下:
 
Tasks table:
+---------+----------------+
| task_id | subtasks_count |
+---------+----------------+
| 1
| 3
|
| 2
| 2
|
| 3
| 4
|
+---------+----------------+
Executed table:
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1
| 2
|
| 3
| 1
|
| 3
| 2
|
| 3
| 3
|
| 3
| 4
|
+---------+------------+
Result table:
+---------+------------+
| task_id | subtask_id |
+---------+------------+
| 1
| 1
|
| 1
| 3
|
| 2
| 1
|
| 2
| 2
|
+---------+------------+
Task 1 被分成了 3 subtasks (1, 2, 3)。只有 subtask 2 被成功执行, 所以我们返回 (1, 1) 和 (1, 3) 这两个主任务子任务对。
Task 2 被分成了 2 subtasks (1, 2)。没有一个subtask被成功执行, 因此我们返回(2, 1)和(2, 2)。
Task 3 被分成了 4 subtasks (1, 2, 3, 4)。所有的subtask都被成功执行,因此对于Task 3,我们不返回任何值。
# 答案:
select x.task_id, x.n as subtask_id
from
(
WITH RECURSIVE t AS
(
SELECT 1 as n
UNION ALL
SELECT n+1 from t where n<20
)
select task_id, n from Tasks, t
where n<=subtasks_count
) x
left join Executed e on x.task_id = e.task_id and x.n = e.subtask_id
where e.subtask_id is null

1783 大满贯数量

##SQL架构
Create table If Not Exists Players (player_id int, player_name varchar(20))
Create table If Not Exists Championships (year int, Wimbledon int, Fr_open int, US_open int, Au_open int)
Truncate table Players
insert into Players (player_id, player_name) values ('1', 'Nadal')
insert into Players (player_id, player_name) values ('2', 'Federer')
insert into Players (player_id, player_name) values ('3', 'Novak')
Truncate table Championships
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2018', '1', '1', '1', '1')
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2019', '1', '1', '2', '2')
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2020', '2', '1', '2', '2')
表:Players
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| player_id
| int
|
| player_name
| varchar |
+----------------+---------+
player_id 是这个表的主键
这个表的每一行给出一个网球运动员的 ID 和 姓名
表:Championships
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| year
| int
|
| Wimbledon
| int
|
| Fr_open
| int
|
| US_open
| int
|
| Au_open
| int
|
+---------------+---------+
year 是这个表的主键
该表的每一行都包含在每场大满贯网球比赛中赢得比赛的球员的 ID
 
请写出查询语句,查询出每一个球员赢得大满贯比赛的次数。结果不包含没有赢得比赛的球员的ID 。
结果集无顺序要求。
查询结果的格式,如下所示:
 
Players 表:
+-----------+-------------+
| player_id | player_name |
+-----------+-------------+
| 1
| Nadal
|
| 2
| Federer
|
| 3
| Novak
|
+-----------+-------------+
Championships 表:
+------+-----------+---------+---------+---------+
| year | Wimbledon | Fr_open | US_open | Au_open |
+------+-----------+---------+---------+---------+
| 2018 | 1
| 1
| 1
| 1
|
| 2019 | 1
| 1
| 2
| 2
|
| 2020 | 2
| 1
| 2
| 2
|
+------+-----------+---------+---------+---------+
Result 表:
+-----------+-------------+-------------------+
| player_id | player_name | grand_slams_count |
+-----------+-------------+-------------------+
| 2
| Federer
| 5
|
| 1
| Nadal
| 7
|
+-----------+-------------+-------------------+
Player 1 (Nadal) 获得了 7 次大满贯:其中温网 2 次(2018, 2019), 法国公开赛 3 次 (2018, 2019, 2020), 美国公开赛 1 次 (2018)以及澳网公开赛 1 次 (2018) 。
Player 2 (Federer) 获得了 5 次大满贯:其中温网 1 次 (2020), 美国公开赛 2 次 (2019, 2020) 以及澳网公开赛 2 次 (2019, 2020) 。
Player 3 (Novak)
没有赢得,因此不包含在结果集中。
# 答案:
select a.wimbledon player_id, b.player_name, COUNT(1) grand_slams_count
from (select a.wimbledon
from Championships a
union all
select a.fr_open
from Championships a
union all
select a.us_open
from Championships a
union all
select a.au_open from Championships a) A,
Players B
WHERE A.WIMBLEDON = B.Player_Id
GROUP BY a.wimbledon, b.player_name

1811 寻找面试候选人

##SQL架构
Create table If Not Exists Contests (contest_id int, gold_medal int, silver_medal int, bronze_medal int)
Create table If Not Exists Users (user_id int, mail varchar(50), name varchar(30))
Truncate table Contests
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('190', '1', '5', '2')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('191', '2', '3', '5')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('192', '5', '2', '3')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('193', '1', '3', '5')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('194', '4', '5', '2')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('195', '4', '2', '1')
insert into Contests (contest_id, gold_medal, silver_medal, bronze_medal) values ('196', '1', '5', '2')
Truncate table Users
insert into Users (user_id, mail, name) values ('1', 'sarah@leetcode.com', 'Sarah')
insert into Users (user_id, mail, name) values ('2', 'bob@leetcode.com', 'Bob')
insert into Users (user_id, mail, name) values ('3', 'alice@leetcode.com', 'Alice')
insert into Users (user_id, mail, name) values ('4', 'hercy@leetcode.com', 'Hercy')
insert into Users (user_id, mail, name) values ('5', 'quarz@leetcode.com', 'Quarz')
表: Contests
+--------------+------+
| Column Name
| Type |
+--------------+------+
| contest_id
| int
|
| gold_medal
| int
|
| silver_medal | int
|
| bronze_medal | int
|
+--------------+------+
contest_id 是该表的主键.
该表包含LeetCode竞赛的ID和该场比赛中金牌、银牌、铜牌的用户id。
可以保证,所有连续的比赛都有连续的ID,没有ID被跳过。
Table: Users
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| user_id
| int
|
| mail
| varchar |
| name
| varchar |
+-------------+---------+
user_id 是该表的主键.
该表包含用户信息。
 
编写 SQL 语句来返回面试候选人的 姓名和 邮件.当用户满足以下两个要求中的任意一条,其成为面试候选人:
该用户在连续三场及更多比赛中赢得奖牌。
该用户在三场及更多不同的比赛中赢得金牌(这些比赛可以不是连续的)
可以以任何顺序返回结果。
查询结果格式如下例所示:
Contests表:
+------------+------------+--------------+--------------+
| contest_id | gold_medal | silver_medal | bronze_medal |
+------------+------------+--------------+--------------+
| 190
| 1
| 5
| 2
|
| 191
| 2
| 3
| 5
|
| 192
| 5
| 2
| 3
|
| 193
| 1
| 3
| 5
|
| 194
| 4
| 5
| 2
|
| 195
| 4
| 2
| 1
|
| 196
| 1
| 5
| 2
|
+------------+------------+--------------+--------------+
Users表:
+---------+--------------------+-------+
| user_id | mail
| name
|
+---------+--------------------+-------+
| 1
| sarah@leetcode.com | Sarah |
| 2
| bob@leetcode.com
| Bob
|
| 3
| alice@leetcode.com | Alice |
| 4
| hercy@leetcode.com | Hercy |
| 5
| quarz@leetcode.com | Quarz |
+---------+--------------------+-------+
结果表:
+-------+--------------------+
| name
| mail
|
+-------+--------------------+
| Sarah | sarah@leetcode.com |
| Bob
| bob@leetcode.com
|
| Alice | alice@leetcode.com |
| Quarz | quarz@leetcode.com |
+-------+--------------------+
Sarah 赢得了3块金牌 (190, 193, and 196),所以我们将她列入结果表。
Bob在连续3场竞赛中赢得了奖牌(190, 191, and 192), 所以我们将他列入结果表。
- 注意他在另外的连续3场竞赛中也赢得了奖牌(194, 195, and 196).
Alice在连续3场竞赛中赢得了奖牌 (191, 192, and 193), 所以我们将她列入结果表。
Quarz在连续5场竞赛中赢得了奖牌(190, 191, 192, 193, and 194), 所以我们将他列入结果表。
 
进阶:
如果第一个条件变成“该用户在连续n场及比赛中赢得任意奖牌。”呢?你如何更改你的解法,来选出面试候选人?可以把n想象成存储过程中的参数。
有的用户可能没有参加每一场竞赛,但是在参加的每一场竞赛中都表现得不错。你如何更改你的解法,以达到只考虑那些用户参与了的比赛?可假设另一张表给出了每场比赛的注册用户信息。
# 答案:
select B.name, b.mail
from (select a.gold_medal
from Contests a
group by a.gold_medal
having count(1) >= 3
UNION
select A.Gold_Medal
from (select a.*,
a.contest_id - row_number() over(partition by a.gold_medal order by a.contest_id) rn
from (select a.contest_id, a.gold_medal
from Contests a
union all
select a.contest_id, a.silver_medal
from Contests a
union all
select a.contest_id, a.bronze_medal from Contests a) A) A
GROUP BY A.rn, A.Gold_Medal
HAVING COUNT(1) >= 3) A,
Users B
WHERE A.Gold_Medal = B.User_Id

1831 每天的最大交易

##SQL架构
Create table If Not Exists Transactions (transaction_id int, day datetime, amount int)
Truncate table Transactions
insert into Transactions (transaction_id, day, amount) values ('8', '2021-4-3 15:57:28', '57')
insert into Transactions (transaction_id, day, amount) values ('9', '2021-4-28 08:47:25', '21')
insert into Transactions (transaction_id, day, amount) values ('1', '2021-4-29 13:28:30', '58')
insert into Transactions (transaction_id, day, amount) values ('5', '2021-4-28 16:39:59', '40')
insert into Transactions (transaction_id, day, amount) values ('6', '2021-4-29 23:39:28', '58')
表: Transactions
+----------------+----------+
| Column Name
| Type
|
+----------------+----------+
| transaction_id | int
|
| day
| datetime |
| amount
| int
|
+----------------+----------+
transaction_id 是此表的主键。
每行包括了该次交易的信息。
 
写一条 SQL 返回每天交易金额 amount 最大的交易 ID 。如果某天有多个这样的交易,返回这些交易的 ID 。
返回结果根据 transaction_id 升序排列。
查询结果样例如下:
 
Transactions table:
+----------------+--------------------+--------+
| transaction_id | day
| amount |
+----------------+--------------------+--------+
| 8
| 2021-4-3 15:57:28
| 57
|
| 9
| 2021-4-28 08:47:25 | 21
|
| 1
| 2021-4-29 13:28:30 | 58
|
| 5
| 2021-4-28 16:39:59 | 40
|
| 6
| 2021-4-29 23:39:28 | 58
|
+----------------+--------------------+--------+
Result table:
+----------------+
| transaction_id |
+----------------+
| 1
|
| 5
|
| 6
|
| 8
|
+----------------+
"2021-4-3"
--> 有一个 id 是 8 的交易,因此,把它加入结果表。
"2021-4-28" --> 有两个交易,id 是 5 和 9 ,交易 5 的金额是 40 ,而交易 9 的数量是 21 。只需要将交易 5 加入结果表,因为它是当天金额最大的交易。
"2021-4-29" --> 有两个交易,id 是 1 和 6 ,这两个交易的金额都是 58 ,因此需要把它们都写入结果表。
最后,把交易 id 按照升序排列。
# 答案:
SELECT
transaction_id
FROM transactions
WHERE (DATE(day), amount) in (
SELECT DATE(day), MAX(amount)
FROM transactions GROUP BY DATE(day)
)
ORDER BY 1;

1841 联赛信息统计

##SQL架构
Create table If Not Exists Teams (team_id int, team_name varchar(20))
Create table If Not Exists Matches (home_team_id int, away_team_id int, home_team_goals int, away_team_goals int)
Truncate table Teams
insert into Teams (team_id, team_name) values ('1', 'Ajax')
insert into Teams (team_id, team_name) values ('4', 'Dortmund')
insert into Teams (team_id, team_name) values ('6', 'Arsenal')
Truncate table Matches
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals) values ('1', '4', '0', '1')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals) values ('1', '6', '3', '3')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals) values ('4', '1', '5', '2')
insert into Matches (home_team_id, away_team_id, home_team_goals, away_team_goals) values ('6', '1', '0', '0')
Table: Teams
+----------------+---------+
| Column Name
| Type
|
+----------------+---------+
| team_id
| int
|
| team_name
| varchar |
+----------------+---------+
team_id 是该表主键.
每一行都包含了一个参加联赛的队伍信息.
 
Table: Matches
+-----------------+---------+
| Column Name
| Type
|
+-----------------+---------+
| home_team_id
| int
|
| away_team_id
| int
|
| home_team_goals | int
|
| away_team_goals | int
|
+-----------------+---------+
(home_team_id, away_team_id) 是该表主键.
每一行包含了一次比赛信息.
home_team_goals 代表主场队得球数.
away_team_goals 代表客场队得球数.
获得球数较多的队伍为胜者队伍.
 
写一段SQL,用来报告联赛信息. 统计数据应使用已进行的比赛来构建,其中获胜球队获得三分,而失败球队获得零分. 如果打平,两支球队都得一分.
result表的每行应包含以下信息:
team_name - Teams表中的队伍名字
matches_played - 主场与客场球队进行的比赛次数.
points - 球队获得的总分数.
goal_for - 球队在所有比赛中获取的总进球数
goal_against - 球队在所有比赛中,他的对手球队的所有进球数
goal_diff - goal_for - goal_against.
按分数降序返回结果表。 如果两队或多队得分相同,则按goal_diff 降序排列。 如果仍然存在平局,则以 team_name 按字典顺序排列它们。
查询的结果格式如下例所示:
 
Teams table:
+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1
| Ajax
|
| 4
| Dortmund
|
| 6
| Arsenal
|
+---------+-----------+
Matches table:
+--------------+--------------+-----------------+-----------------+
| home_team_id | away_team_id | home_team_goals | away_team_goals |
+--------------+--------------+-----------------+-----------------+
| 1
| 4
| 0
| 1
|
| 1
| 6
| 3
| 3
|
| 4
| 1
| 5
| 2
|
| 6
| 1
| 0
| 0
|
+--------------+--------------+-----------------+-----------------+
Result table:
+-----------+----------------+--------+----------+--------------+-----------+
| team_name | matches_played | points | goal_for | goal_against | goal_diff |
+-----------+----------------+--------+----------+--------------+-----------+
| Dortmund
| 2
| 6
| 6
| 2
| 4
|
| Arsenal
| 2
| 2
| 3
| 3
| 0
|
| Ajax
| 4
| 2
| 5
| 9
| -4
|
+-----------+----------------+--------+----------+--------------+-----------+
Ajax (team_id=1) 有4场比赛: 2败2平. 总分数 = 0 + 0 + 1 + 1 = 2.
Dortmund (team_id=4) 有2场比赛: 2胜. 总分数 = 3 + 3 = 6.
Arsenal (team_id=6) 有2场比赛: 2平. 总分数 = 1 + 1 = 2.
Dortmund 是积分榜上的第一支球队. Ajax和Arsenal 有同样的分数, 但Arsenal的goal_diff高于Ajax, 所以Arsenal在表中的顺序在Ajaxzhiqian.
# 答案:
SELECT distinct team_name,
COUNT(*) AS matches_played,
SUM(
CASE
WHEN (team_id=home_team_id AND home_team_goals>away_team_goals) OR
(team_id=away_team_id AND home_team_goals<away_team_goals) THEN 3
WHEN (team_id=home_team_id AND home_team_goals<away_team_goals) OR
(team_id=away_team_id AND home_team_goals>away_team_goals) THEN 0
ELSE 1 END) AS points,
SUM(IF(team_id=home_team_id,home_team_goals,away_team_goals)) AS goal_for,
SUM(IF(team_id=home_team_id,away_team_goals,home_team_goals)) AS goal_against,
SUM(IF(team_id=home_team_id,home_team_goals,away_team_goals)) - SUM(IF(team_id=home_team_id,away_team_goals,home_team_goals)) AS goal_diff
FROM Teams JOIN Matches ON team_id=home_team_id OR team_id=away_team_id
GROUP BY team_id
ORDER BY points DESC, goal_diff DESC, team_name ASC

1892 页面推荐

##SQL架构
Create table If Not Exists Friendship (user1_id int, user2_id int)
Create table If Not Exists Likes (user_id int, page_id int)
Truncate table Friendship
insert into Friendship (user1_id, user2_id) values ('1', '2')
insert into Friendship (user1_id, user2_id) values ('1', '3')
insert into Friendship (user1_id, user2_id) values ('1', '4')
insert into Friendship (user1_id, user2_id) values ('2', '3')
insert into Friendship (user1_id, user2_id) values ('2', '4')
insert into Friendship (user1_id, user2_id) values ('2', '5')
insert into Friendship (user1_id, user2_id) values ('6', '1')
Truncate table Likes
insert into Likes (user_id, page_id) values ('1', '88')
insert into Likes (user_id, page_id) values ('2', '23')
insert into Likes (user_id, page_id) values ('3', '24')
insert into Likes (user_id, page_id) values ('4', '56')
insert into Likes (user_id, page_id) values ('5', '11')
insert into Likes (user_id, page_id) values ('6', '33')
insert into Likes (user_id, page_id) values ('2', '77')
insert into Likes (user_id, page_id) values ('3', '77')
insert into Likes (user_id, page_id) values ('6', '88')
Table: Friendship
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user1_id
| int
|
| user2_id
| int
|
+---------------+---------+
(user1_id,user2_id)是Friendship表的主键。
该表的每一行表示用户user1_id和user2_id是好友。
 
Table: Likes
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| user_id
| int
|
| page_id
| int
|
+-------------+---------+
(user_id,page_id)是Likes表的主键。
(user_id, page_id) is the primary key for this table.
该表的每一行表示user_id喜欢page_id。
 
您正在为一个社交媒体网站实施一个页面推荐系统。如果页面被user_id的至少一个朋友喜欢,而不被user_id喜欢,你的系统将推荐一个页面到user_id。
编写一个SQL查询来查找针对每个用户的所有可能的页面建议。每个建议应该在结果表中显示为一行,包含以下列:
user_id: 系统向其提出建议的用户的ID。
page_id: 推荐为user_id的页面ID。.
friends_likes: user_id对应page_id的好友数。
以任意顺序返回结果表。
查询结果格式示例如下:
 
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1
| 2
|
| 1
| 3
|
| 1
| 4
|
| 2
| 3
|
| 2
| 4
|
| 2
| 5
|
| 6
| 1
|
+----------+----------+
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1
| 88
|
| 2
| 23
|
| 3
| 24
|
| 4
| 56
|
| 5
| 11
|
| 6
| 33
|
| 2
| 77
|
| 3
| 77
|
| 6
| 88
|
+---------+---------+
Result table:
+---------+---------+---------------+
| user_id | page_id | friends_likes |
+---------+---------+---------------+
| 1
| 77
| 2
|
| 1
| 23
| 1
|
| 1
| 24
| 1
|
| 1
| 56
| 1
|
| 1
| 33
| 1
|
| 2
| 24
| 1
|
| 2
| 56
| 1
|
| 2
| 11
| 1
|
| 2
| 88
| 1
|
| 3
| 88
| 1
|
| 3
| 23
| 1
|
| 4
| 88
| 1
|
| 4
| 77
| 1
|
| 4
| 23
| 1
|
| 5
| 77
| 1
|
| 5
| 23
| 1
|
+---------+---------+---------------+
以用户1为例:
—用户1是用户2、3、4、6的好友。
推荐页面有23(用户2喜欢),24(用户3喜欢),56(用户3喜欢),33(用户6喜欢),77(用户2和用户3喜欢)。
-请注意,第88页不推荐,因为用户1已经喜欢它。
另一个例子是用户6:
—用户6是用户1的好友。
-用户1只喜欢了88页,但用户6已经喜欢了。因此,用户6没有推荐。
您可以使用类似的过程为用户2、3、4和5推荐页面。
# 答案:
SELECT f.user1_id AS user_id, l.page_id, COUNT(*) AS friends_likes
FROM (
SELECT user1_id, user2_id
FROM Friendship
UNION
SELECT user2_id, user1_id
FROM Friendship
) f
INNER JOIN Likes l ON f.user2_id = l.user_id
WHERE NOT EXISTS (
SELECT *
FROM Likes
WHERE user_id = f.user1_id
AND page_id = l.page_id
)
GROUP BY f.user1_id, l.page_id

1907 按分类统计薪水

##SQL架构
Create table If Not Exists Accounts (account_id int, income int)
Truncate table Accounts
insert into Accounts (account_id, income) values ('3', '108939')
insert into Accounts (account_id, income) values ('2', '12747')
insert into Accounts (account_id, income) values ('8', '87709')
insert into Accounts (account_id, income) values ('6', '91796')
表: Accounts
+-------------+------+
| 列名
| 类型
|
+-------------+------+
| account_id
| int
|
| income
| int
|
+-------------+------+
account_id 是这个表的主键。
每一行都包含一个银行帐户的月收入的信息。
写出一个 SQL 查询,来报告每个工资类别的银行账户数量。 工资类别如下:
“低薪”:所有工资严格低于20000美元。
“中等薪水”:包含范围内的所有工资 [$20000, $50000]。
“高薪”:所有工资严格大于50000美元。
结果表必须包含所有三个类别。 如果某个类别中没有帐户,则报告 0。
按任意顺序返回结果表。
查询结果格式如下示例:
Accounts 表:
+------------+--------+
| account_id | income |
+------------+--------+
| 3
| 108939 |
| 2
| 12747
|
| 8
| 87709
|
| 6
| 91796
|
+------------+--------+
Result 表:
+----------------+----------------+
| category
| accounts_count |
+----------------+----------------+
| Low Salary
| 1
|
| Average Salary | 0
|
| High Salary
| 3
|
+----------------+----------------+
低薪: 数量为 2.
中等薪水: 没有.
高薪: 有三个账户,他们是 3, 6和 8.
# 答案:
SELECT 'Low Salary' category, COUNT(account_id) accounts_count FROM Accounts ac WHERE income < 20000
UNION
SELECT 'Average Salary' category, COUNT(account_id) accounts_count FROM Accounts ac WHERE income BETWEEN 20000 AND 50000
UNION
SELECT 'High Salary' category, COUNT(account_id) accounts_count FROM Accounts ac WHERE income > 50000;

1919 兴趣相同的朋友

##SQL架构
Create table If Not Exists Listens (user_id int, song_id int, day date)
Create table If Not Exists Friendship (user1_id int, user2_id int)
Truncate table Listens
insert into Listens (user_id, song_id, day) values ('1', '10', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('1', '11', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('1', '12', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('2', '10', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('2', '11', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('2', '12', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('3', '10', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('3', '11', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('3', '12', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('4', '10', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('4', '11', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('4', '13', '2021-03-15')
insert into Listens (user_id, song_id, day) values ('5', '10', '2021-03-16')
insert into Listens (user_id, song_id, day) values ('5', '11', '2021-03-16')
insert into Listens (user_id, song_id, day) values ('5', '12', '2021-03-16')
Truncate table Friendship
insert into Friendship (user1_id, user2_id) values ('1', '2')
insert into Friendship (user1_id, user2_id) values ('2', '4')
insert into Friendship (user1_id, user2_id) values ('2', '5')
Table: Listens
+-------------+---------+
| Column Name | Type
|
+-------------+---------+
| user_id
| int
|
| song_id
| int
|
| day
| date
|
+-------------+---------+
该表没有主键,因此会存在重复的行。
该表的每一行所代表的含义是:用户(user_id)在某天(day)听了某首歌曲(song_id)。
 
Table: Friendship
+---------------+---------+
| Column Name
| Type
|
+---------------+---------+
| user1_id
| int
|
| user2_id
| int
|
+---------------+---------+
(user1_id, user2_id) 是该表的主键。
该表的每一行所代表的含义是,用户(user1_id, user2_id)是朋友。
注意:user1_id < user2_id。
 
请写一段SQL查询获取到兴趣相同的朋友。用户 x 和 用户 y 是兴趣相同的朋友,需满足下述条件:
用户 x 和 y 是朋友,并且
用户 x and y 在同一天内听过相同的歌曲,且数量大于等于三首.
结果表无需排序。注意:返回的结果需要和源数据表的呈现方式相同 (例如, 需满足 user1_id < user2_id)。
结果表的格式如下例:
 
Listens table:
+---------+---------+------------+
| user_id | song_id | day
|
+---------+---------+------------+
| 1
| 10
| 2021-03-15 |
| 1
| 11
| 2021-03-15 |
| 1
| 12
| 2021-03-15 |
| 2
| 10
| 2021-03-15 |
| 2
| 11
| 2021-03-15 |
| 2
| 12
| 2021-03-15 |
| 3
| 10
| 2021-03-15 |
| 3
| 11
| 2021-03-15 |
| 3
| 12
| 2021-03-15 |
| 4
| 10
| 2021-03-15 |
| 4
| 11
| 2021-03-15 |
| 4
| 13
| 2021-03-15 |
| 5
| 10
| 2021-03-16 |
| 5
| 11
| 2021-03-16 |
| 5
| 12
| 2021-03-16 |
+---------+---------+------------+
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1
| 2
|
| 2
| 4
|
| 2
| 5
|
+----------+----------+
Result table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1
| 2
|
+----------+----------+
用户 1 和 2 是朋友, 并且他们在同一天内都听了10、11、12的歌曲。所以,他们是兴趣相同的朋友。
用户 1 和 3 在同一天内都听了10、11、12的歌曲,但他们不是朋友。
用户 2 和 4 是朋友,但他们同一天内听过相同的歌曲的数量小于3。
用户 2 和 5 是朋友,并且在都听了了10、11、12的歌曲,但不在同一天内。
# 答案:
select distinct t.user1_id
,t.user2_id
from
(select a.user_id as user1_id
,b.user_id as user2_id
,a.song_id
,a.day
,count(1) over (partition by a.user_id,b.user_id,a.day) as cnt
from (select distinct user_id,song_id,day from Listens) a
inner join (select distinct user_id,song_id,day from Listens) b
on a.user_id <> b.user_id
and a.song_id = b.song_id
and a.day = b.day) t
inner join Friendship t1
on t.user1_id = t1.user1_id and t.user2_id = t1.user2_id
where t.cnt >= 3

1951 查询具有最多共同关注着的所有两两对组

##SQL架构
Create table If Not Exists Relations (user_id int, follower_id int)
Truncate table Relations
insert into Relations (user_id, follower_id) values ('1', '3')
insert into Relations (user_id, follower_id) values ('2', '3')
insert into Relations (user_id, follower_id) values ('7', '3')
insert into Relations (user_id, follower_id) values ('1', '4')
insert into Relations (user_id, follower_id) values ('2', '4')
insert into Relations (user_id, follower_id) values ('7', '4')
insert into Relations (user_id, follower_id) values ('1', '5')
insert into Relations (user_id, follower_id) values ('2', '6')
insert into Relations (user_id, follower_id) values ('7', '5')
表: Relations
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id
| int
|
| follower_id | int
|
+-------------+------+
(user_id, follower_id) 是这个表的主键.
这个表的每一行,表示这个user_id的用户和他的关注者,关注者的id 就是本表的 user_id.
 
写出一个查询语句,找到具有最多共同关注者的所有两两结对组。换句话说,如果有两个用户的共同关注者是最大的,我们应该返回所有具有此最大值的两两结对组
The result table should contain the pairs user1_id and user2_id where user1_id < user2_id.
结果返回表,每一行应该包含user1_id和 user2_id,其中user1_id < user2_id.
返回结果 不要求顺序 。
查询结果格式如下例:
 
Relations 表:
+---------+-------------+
| user_id | follower_id |
+---------+-------------+
| 1
| 3
|
| 2
| 3
|
| 7
| 3
|
| 1
| 4
|
| 2
| 4
|
| 7
| 4
|
| 1
| 5
|
| 2
| 6
|
| 7
| 5
|
+---------+-------------+
Result 表:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1
| 7
|
+----------+----------+
用户1 和用户 2 有2个共同的关注者(3和4)。
用户1 和用户 7 有3个共同的关注者(3,4和5)。
用户2 和用户7 有2个共同的关注者(3和4)。
既然两两结对的所有组队的最大共同关注者的数值是3,所以,我们应该返回所有拥有3个共同关注者的两两组队,这就是仅有的一对(1, 7).
我们返回的是(1, 7).,而不是(7, 1).
注意,我们没有关于用户3,4,5的任何关注者信息,我们认为他们有0个关注者。
# 答案:
方法一:
1. 统计拥有相同关注者的用户对的关注者数量
SELECT
r1.user_id user1_id, r2.user_id user2_id, COUNT(r1.follower_id) followers_count
FROM Relations r1, Relations r2 WHERE r1.follower_id = r2.follower_id AND r1.user_id < r2.user_id GROUP BY r1.user_id, r2.user_id;
2. 查询出拥有最大共同关注者数量的用户对
WITH a AS (
SELECT
r1.user_id user1_id, r2.user_id user2_id, COUNT(r1.follower_id) followers_count
FROM Relations r1, Relations r2 WHERE r1.follower_id = r2.follower_id AND r1.user_id < r2.user_id GROUP BY r1.user_id, r2.user_id
)
SELECT a.user1_id, a.user2_id FROM a WHERE a.followers_count = (
SELECT
MAX(a.followers_count) max_count
FROM a
);
方法二:
SELECT DISTINCT t1.user1_id
,t1.user2_id
FROM (SELECT user1_id
,user2_id
,cnt
,DENSE_RANK() OVER (ORDER BY cnt DESC) AS rnk --对cnt从大到小排序,为了找到cnt最大的user_id对,要允许并列的存在
FROM (SELECT t1.user_id AS user1_id
,t2.user_id AS user2_id
,COUNT(1) cnt
FROM Relations t1
INNER JOIN Relations t2 --内连接,条件为follower_id相同user_id不同,意味着不同客户的follwer相同,并求这种不同user_id对的follower数量(计为cnt)
ON t1.follower_id = t2.follower_id AND t1.user_id <> t2.user_id
GROUP BY t1.user_id,t2.user_id) t ) t1
WHERE t1.rnk = 1 AND t1.user1_id < t1.user2_id --取cnt排名为1的且user1_id 小于user2_id

致语:SQL、sQl、sqL、SqL、sQL、sQL、数据、数据库

最后

以上就是无奈世界为你收集整理的Leetcode力扣数据库SQL(VIP)题目及解答全集511 游戏玩法分析I512 游戏玩法分析II534 游戏玩法分析III550 游戏玩法分析IV569 员工薪水中位数570 至少有5名直接下属的经理571 给定数字的频率查询中位数574 当选者578 查询回答率最高的问题579 查询员工的累积薪水580 统计各专业学生人数585 2016年的投资602 好友申请II:谁有最多的好友614 二级关注者615 平均工资:部门与公司比较618 学生地理信息报告1045 买下所有产品的客户107的全部内容,希望文章能够帮你解决Leetcode力扣数据库SQL(VIP)题目及解答全集511 游戏玩法分析I512 游戏玩法分析II534 游戏玩法分析III550 游戏玩法分析IV569 员工薪水中位数570 至少有5名直接下属的经理571 给定数字的频率查询中位数574 当选者578 查询回答率最高的问题579 查询员工的累积薪水580 统计各专业学生人数585 2016年的投资602 好友申请II:谁有最多的好友614 二级关注者615 平均工资:部门与公司比较618 学生地理信息报告1045 买下所有产品的客户107所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部