datawhale-SQL编程:Task06-综合练习

练习一: 各部门工资最高的员工(leetcode184 难度:中等)

创建 Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+
use testdb;
create table Employee(
Id char(5) not null, 
Name varchar(20) not null,
Salary integer not null,
DepartmentId char(3) not null
primary key (Id));

insert into Employee values ('00001', 'Joe', 70000, '001'),
('00002', 'Henry', 80000, '002'),
('00003', 'Sam', 60000, '002'),
('00004', 'Max', 90000, '001');

创建 Department 表,包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+
use testdb;
create table Department (
Id char(3) not null, 
Name varchar(20) not null
primary key (Id));

insert into Department values ('001', 'IT'),
('002', 'Sales');

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+
use testdb;
select d.Name as Department, e3.Name as Employee, e3.Salary
from Department as d
inner join (select e2.Name, e2.Salary, e2.DepartmentId
			from Employee as e2
            inner join (select DepartmentId, max(Salary) as max_salary
						from Employee
						group by DepartmentId) as e1
            on e2.Salary=max_salary
            and e2.DepartmentId=e1.DepartmentId) as e3
on e3.DepartmentId=d.Id;

练习二: 换座位(leetcode626 难度:中等)

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的id是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

请创建如下所示seat表:

示例:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

假如数据输入的是上表,则输出结果如下:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。

use testdb;
create table seat(
id integer not null auto_increment,
student varchar(8) not null,
primary key (id));

insert into seat (student) values ('Abbot'),
('Doris'),
('Emerson'),
('Green'),
('Jeames');

select (case when s.id % 2 = 0 then s.id - 1
			 when s.id % 2 != 0 and s.id != c.counts then s.id + 1
             else s.id
		end) as id,
		student
from seat as s
join (select count(*) as counts 
	  from seat) as c
order by id;

练习三: 分数排名(leetcode178 难度:中等)

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”

创建以下score表:

+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+
use testdb;
create table score (
Id integer not null auto_increment,
Score float not null,
primary key (Id));

insert into score (Score) values(3.50),
(3.65),
(4.00),
(3.85),
(4.00),
(3.65);

-- Rank和关键字重复了 因此要加引号表示字符串
select Score,
dense_rank() over(order by Score desc) as 'Rank'
from score;

练习四:连续出现的数字(leetcode180 难度:中等)

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
create table Logs(
	Id int not null auto_increment,
    Num int not null,
    primary key(Id));

insert into Logs (Num) values (1);
insert into Logs (Num) values (1);
insert into Logs (Num) values (1);
insert into Logs (Num) values (2);
insert into Logs (Num) values (1);
insert into Logs (Num) values (2);
insert into Logs (Num) values (2);

select distinct l2.Num as ConsecutiveNums
from Logs as l1
join Logs as l2
join Logs as l3
on l1.id-l2.id=1
and l3.id-l1.id=1
where l1.Num=l2.Num
and l1.Num=l3.Num;

练习五:树节点(leetcode608 难度:中等)

对于tree表,id是树节点的标识,p_id是其父节点的id

+----+------+
| id | p_id |
+----+------+
| 1  | null |
| 2  | 1    |
| 3  | 1    |
| 4  | 2    |
| 5  | 2    |
+----+------+

每个节点都是以下三种类型中的一种:

  • Root: 如果节点是根节点。
  • Leaf: 如果节点是叶子节点。
  • Inner: 如果节点既不是根节点也不是叶子节点。

写一条查询语句打印节点id及对应的节点类型。按照节点id排序。上面例子的对应结果为:

+----+------+
| id | Type |
+----+------+
| 1  | Root |
| 2  | Inner|
| 3  | Leaf |
| 4  | Leaf |
| 5  | Leaf |
+----+------+

说明

  • 节点’1’是根节点,因为它的父节点为NULL,有’2’和’3’两个子节点。
  • 节点’2’是内部节点,因为它的父节点是’1’,有子节点’4’和’5’。
  • 节点’3’,‘4’,'5’是叶子节点,因为它们有父节点但没有子节点。

下面是树的图形:

    1         
  /   \ 
 2    3    
/ \
4  5

注意

如果一个树只有一个节点,只需要输出根节点属性。

create table tree(
	id int not null auto_increment,
    p_id int,
    primary key (id));
    
insert into tree (p_id) values (null);
insert into tree (p_id) values (1);
insert into tree (p_id) values (1);
insert into tree (p_id) values (2);
insert into tree (p_id) values (2);

select id,
        (case when p_id is null then 'Root'
              when id in (select p_id from tree) then 'Inner'
        	  else 'Leaf'
        end) as Type
from tree;

练习六:至少有五名直接下属的经理 (leetcode570 难度:中等)

Employee表包含所有员工及其上级的信息。每位员工都有一个Id,并且还有一个对应主管的Id(ManagerId)。

+------+----------+-----------+----------+
|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个下属的主管。对于上面的表,结果应输出:

+-------+
| Name  |
+-------+
| John  |
+-------+

注意:

没有人向自己汇报。

create table Employee1(
	Id int not null,
    Name varchar(8) not null,
    Department char(1) not null,
    ManagerId int,
    primary key (Id));
    
insert into Employee1 values (101, 'John', 'A', null);
insert into Employee1 values (102, 'Dan', 'A', 101);
insert into Employee1 values (103, 'James', 'A', 101);
insert into Employee1 values (104, 'Amy', 'A', 101);
insert into Employee1 values (105, 'Anne', 'A', 101);
insert into Employee1 values (106, 'Ron', 'B', 101);

select e2.Name
from Employee1 as e2
inner join (select ManagerId, count(*) as cnt
			from Employee1
			group by ManagerId) as e1
on cnt>=5
and e2.Id=e1.ManagerId;

练习七: 分数排名 (难度:中等)

练习三的分数表,实现排名功能,但是排名需要是非连续的,如下:

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 3    |
| 3.65  | 4    |
| 3.65  | 4    |
| 3.50  | 6    |
+-------+------
use testdb;
select Score,
rank() over(order by Score desc) as 'Rank'
from score;

练习八:查询回答率最高的问题 (leetcode578 难度:中等)

求出survey_log表中回答率最高的问题,表格的字段有:uid, action, question_id, answer_id, q_num, timestamp

uid是用户id;action的值为:“show”, “answer”, “skip”;当action是"answer"时,answer_id不为空,相反,当action是"show"和"skip"时为空(null);q_num是问题的数字序号。

写一条sql语句找出回答率最高的问题。

举例:

输入

uidactionquestion_idanswer_idq_numtimestamp
5show285null1123
5answer2851241241124
5show369null2125
5skip369null2126

输出

survey_log
285

说明

问题285的回答率为1/1,然而问题369的回答率是0/1,所以输出是285。

**注意:**最高回答率的意思是:同一个问题出现的次数中回答的比例。

use testdb;
create table survey_log(
	uid int not null,
    action varchar(8) not null,
    question_id int not null,
    answer_id int,
    q_num int not null,
    timestamp int not null,
    primary key(timestamp));

insert into survey_log values (5, 'show', 285, null, 1, 123);
insert into survey_log values (5, 'answer', 285, 124124, 1, 124);
insert into survey_log values (5, 'show', 369, null, 2, 125);
insert into survey_log values (5, 'skip', 369, null, 2, 126);

select question_id as survey_log
from (select question_id, 
			sum(case when action = 'answer' then 1 else 0 end) / sum(case when action = 'show' then 1 else 0 end) as ratio
	from survey_log
	group by question_id) as s
order by ratio desc
limit 1;

练习九:各部门前3高工资的员工(leetcode185 难度:中等)

将项目7中的employee表清空,重新插入以下数据(其实是多插入5,6两行):

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

此外,请考虑实现各部门前N高工资的员工功能。

select d.Name as Department, e.Name as Employee, e.Salary
from Department as d
join (select *, dense_rank() over(partition by DepartmentId order by Salary desc) as 'rank'
		from Employee) as e
on e.DepartmentId=d.Id
where e.rank<=3;

练习十:平面上最近距离 (leetcode612 难度: 困难)

point_2d表包含一个平面内一些点(超过两个)的坐标值(x,y)。

写一条查询语句求出这些点中的最短距离并保留2位小数。

|x   | y  |
|----|----|
| -1 | -1 |
|  0 |  0 |
| -1 | -2 |

最短距离是1,从点(-1,-1)到点(-1,-2)。所以输出结果为:

| shortest |

1.00

+--------+
|shortest|
+--------+
|1.00    |
+--------+

注意: 所有点的最大距离小于10000。

create table point_2d (
	x int not null,
    y int not null,
    primary key (x, y));
    
insert into point_2d values (-1, -1);
insert into point_2d values (0, 0);
insert into point_2d values (-1, -2);

select round(min(sqrt(power(p1.x - p2.x, 2) + power(p1.y - p2.y, 2))), 2) as shortest
from point_2d as p1, point_2d as p2
where p1.x < p2.x or (p1.x = p2.x and p1.y < p2.y);

练习十一:行程和用户(leetcode612 难度:困难)

Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

IdClient_IdDriver_IdCity_IdStatusRequest_at
11101completed2013-10-1
22111cancelled_by_driver2013-10-1
33126completed2013-10-1
44136cancelled_by_client2013-10-1
51101completed2013-10-2
62116completed2013-10-2
73126completed2013-10-2
821212completed2013-10-3
931012completed2013-10-3
1041312cancelled_by_driver2013-10-3

Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

+----------+--------+--------+
| Users_Id | Banned |  Role  |
+----------+--------+--------+
|    1     |   No   | client |
|    2     |   Yes  | client |
|    3     |   No   | client |
|    4     |   No   | client |
|    10    |   No   | driver |
|    11    |   No   | driver |
|    12    |   No   | driver |
|    13    |   No   | driver |
+----------+--------+--------+

写一段 SQL 语句查出2013年10月1日2013年10月3日期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

+------------+-------------------+
|     Day    | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |       0.33        |
| 2013-10-02 |       0.00        |
| 2013-10-03 |       0.50        |
+------------+-------------------+
create table Trips(
	Id int not null auto_increment,
    Client_Id int not null,
    Driver_Id int not null,
    City_Id int not null,
    Status enum('completed','cancelled_by_driver','cancelled_by_client') not null,
    Request_at date not null,
    primary key(Id));
    
insert into Trips values (1, 1, 10, 1, 'completed', '2013-10-1');
insert into Trips values (2, 2, 11, 1, 'cancelled_by_driver', '2013-10-1');
insert into Trips values (3, 3, 12, 6, 'completed', '2013-10-1');
insert into Trips values (4, 4, 13, 6, 'cancelled_by_client', '2013-10-1');
insert into Trips values (5, 1, 10, 1, 'completed', '2013-10-2');
insert into Trips values (6, 2, 11, 6, 'completed', '2013-10-2');
insert into Trips values (7, 3, 12, 6, 'completed', '2013-10-2');
insert into Trips values (8, 2, 12, 12, 'completed', '2013-10-3');
insert into Trips values (9, 3, 10, 12, 'completed', '2013-10-3');
insert into Trips values (10, 4, 13, 12, 'cancelled_by_driver', '2013-10-3');

create table Users(
	Users_Id int not null,
    Banned enum('Yes', 'No') not null,
    Role enum('client', 'driver') not null,
    primary key(Users_Id));
    
insert into Users values (1, 'No', 'client');
insert into Users values (2, 'Yes', 'client');
insert into Users values (3, 'No', 'client');
insert into Users values (4, 'No', 'client');
insert into Users values (10, 'No', 'driver');
insert into Users values (11, 'No', 'driver');
insert into Users values (12, 'No', 'driver');
insert into Users values (13, 'No', 'driver');

select Request_at as Day,
round(sum(case when Status not like 'completed' then 1 else 0 end) / count(*), 2) as 'Cancellation Rate'
from (select * from Trips
	  where Client_Id in (select Users_Id 
						  from Users
                          where Banned like 'No')
	  and Driver_Id in (select Users_Id 
						  from Users
                          where Banned like 'No')) as s
where Request_at between '2013-10-01' and '2013-10-03'
group by Request_at;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值