Task06:综合练习
练习⼀: 各部⻔⼯资最⾼的员⼯(难度:中等)
创建Employee 表,包含所有员⼯信息,每个员⼯有其对应的 Id, salary 和 department Id。
1 ±—±------±-------±-------------+
2 | Id | Name | Salary | DepartmentId |
3 ±—±------±-------±-------------+
4 | 1 | Joe | 70000 | 1 |
5 | 2 | Henry | 80000 | 2 |
6 | 3 | Sam | 60000 | 2 |
7 | 4 | Max | 90000 | 1 |
8 ±—±------±-------±-------------+
1 ±—±---------+
2 | Id | Name |
3 ±—±---------+
4 | 1 | IT |
5 | 2 | Sales |
6 ±—±---------+
编写⼀个 SQL 查询,找出每个部⻔⼯资最⾼的员⼯。例如,根据上述给定的表格,Max 在 IT 部⻔有最⾼
⼯资,Henry 在 Sales 部⻔有最⾼⼯资。
1 ±-----------±---------±-------+
2 | Department | Employee | Salary |
3 ±-----------±---------±-------+
4 | IT | Max | 90000 |
5 | Sales | Henry | 80000 |
6 ±-----------±---------±-------+

答案:
create table employee(
id int primary key,
name char(10),
salary integer,
departmentid int);

insert into employee values(1,‘Joe’,70000,1);
insert into employee values(2,‘Henry’,80000,2);
insert into employee values(3,‘Sam’,60000,2);
insert into employee values(4,‘Max’,90000,1);

id | name | salary | departmentid
----±-----------±-------±-------------
1 | Joe | 70000 | 1
2 | Henry | 80000 | 2
3 | Sam | 60000 | 2
4 | Max | 90000 | 1
(4 行记录)

create table department(
id int,
name char(10));
insert into department values(1,‘IT’);
insert into department values(2,‘Sales’);

id | name
----±-----------
1 | IT
2 | Sales
(2 行记录)

select department,employee,salary
from(
select d.name department,e.name employee,e.salary,
rank() over (partition by d.name order by e.salary desc) rank
from employee e,department d
where e.departmentid=d.Id) foo
where rank=1;

department | employee | salary
------------±-----------±-------
IT | Max | 90000
Sales | Henry | 80000
(2 行记录)

练习⼆: 换座位(难度:中等)
⼩美是⼀所中学的信息科技⽼师,她有⼀张 seat 座位表,平时⽤来储存学⽣名字和与他们相对应的座位id。
其中纵列的 id 是连续递增的
⼩美想改变相邻俩学⽣的座位。
你能不能帮她写⼀个 SQL query 来输出⼩美想要的结果呢?
请创建如下所示seat表:
示例:
create table seat (
id int,
student char(10));

insert into seat values(1,‘abbot’);
insert into seat values(2,‘doris’);
insert into seat values(3,‘green’);
insert into seat values(4,‘emerson’);
insert into seat values(5,‘jeames’);

id | student
----±-----------
1 | abbot
2 | doris
3 | green
4 | emerson
5 | jeames
(5 行记录)

select id,student,
case when mod(id,2)=0 then id-1 else id+1 end nid
from seat
order by nid;
id | student | nid
----±-----------±----
2 | doris | 1
1 | abbot | 2
4 | emerson | 3
3 | green | 4
5 | jeames | 6
(5 行记录)

练习三: 分数排名(难度:中等)
编写⼀个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后
的下⼀个名次应该是下⼀个连续的整数值。换句话说,名次之间不应该有“间隔”。
创建以下score表:
create table score(
id int ,
score numeric);

insert into score values(1,3.50);
insert into score values(2,3.65);
insert into score values(3,4.00);
insert into score values(4,3.85);
insert into score values(5,4.00);
insert into score values(6,3.65);

id | score
----±------
1 | 3.50
2 | 3.65
3 | 4.00
4 | 3.85
5 | 4.00
6 | 3.65
(6 行记录)

select score,
dense_rank() over (order by score desc)
from score;

score | dense_rank
-------±-----------
4.00 | 1
4.00 | 1
3.85 | 2
3.65 | 3
3.65 | 3
3.50 | 4
(6 行记录)

练习四:连续出现的数字(难度:中等)
编写⼀个 SQL 查询,查找所有⾄少连续出现上表2次的数字。

select score
from(
select score,dense_rank,count(*)
from(
select score,
dense_rank() over (order by score desc)
from score
) foo–用dense_rank函数排名,显示出数字一样的
group by dense_rank,score) fooo–分组,计数数字一样的个数
where count>=2;–筛选数字一样的个数大于等于2的score

score

4.00
3.65
(2 行记录)

练习五:树节点 (难度:中等)
对于 tree 表,id 是树节点的标识,p_id 是其⽗节点的 id
1 ±—±-----+
2 | id | p_id |
3 ±—±-----+
4 | 1 | null |
5 | 2 | 1 |
6 | 3 | 1 |
7 | 4 | 2 |
8 | 5 | 2 |
9 ±—±-----+
每个节点都是以下三种类型中的⼀种:
 Leaf: 如果节点是根节点。
 Root: 如果节点是叶⼦节点。
 Inner: 如果节点既不是根节点也不是叶⼦节点。
写⼀条查询语句打印节点id及对应的节点类型。按照节点id排序。上⾯例⼦的对应结果为:
1 ±—±-----+
2 | id | Type |
3 ±—±-----+
4 | 1 | Root |
5 | 2 | Inner|
6 | 3 | Leaf |
7 | 4 | Leaf |
8 | 5 | Leaf |
9 ±—±-----+
说明
 节点’1’是根节点,因为它的⽗节点为NULL,有’2’和’3’两个⼦节点。
 节点’2’是内部节点,因为它的⽗节点是’1’,有⼦节点’4’和’5’。
 节点’3’,‘4’,’5‘ 是叶⼦节点,因为它们有⽗节点但没有⼦节点。
create table tree(
id int,
p_id int);

insert into tree values(1,null);
insert into tree values(2,1);
insert into tree values(3,1);
insert into tree values(4,2);
insert into tree values(5,2);

select id,
case when p_id is null then ‘Root’
when id in (select distinct p_id from tree) then ‘Inner’
else ‘Leaf’ end
from tree;
id | case
----±------
1 | Root
2 | Inner
3 | Leaf
4 | Leaf
5 | Leaf
(5 行记录)

练习六:⾄少有五名直接下属的经理 (难度:中等)
Employee 表包含所有员⼯及其上级的信息。每位员⼯都有⼀个Id,并且还有⼀个对应主
管的Id(ManagerId)。
create table employee1 (
id int ,
name char(10),
department char(4),
managerID int);

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 name
from employee1
where ID = (
select managerID
from(
select managerID,count(managerID) numan
from employee1
group by managerID) foo
where numan=5
);

name

john
(1 行记录)

练习七: 分数排名 (难度:中等)
练习三的分数表,实现排名功能,但是排名需要是⾮连续的,如下:
1 ±------±-----+
2 | Score | Rank |
3 ±------±-----+
4 | 4.00 | 1 |
5 | 4.00 | 1 |
6 | 3.85 | 3 |
7 | 3.65 | 4 |
8 | 3.65 | 4 |
9 | 3.50 | 6 |
10 ±------±-----

select score,
rank() over (order by score desc)
from score;

score | rank
-------±-----
4.00 | 1
4.00 | 1
3.85 | 3
3.65 | 4
3.65 | 4
3.50 | 6
(6 行记录)

练习九:各部⻔前3⾼⼯资的员⼯(难度:中等)
将项⽬7中的employee表清空,重新插⼊以下数据(其实是多插⼊5,6两⾏):
insert into employee values(5,‘Janet’,69000,1);
insert into employee values(6,‘Randy’,85000,1);

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
(6 行记录)

select department,employee,salary
from(
select e.id,e.name employee,e.departmentid,d.name department,e.salary,
rank() over (partition by departmentid order by salary desc)
from employee e,department d
where e.departmentid=d.id) foo
where rank<4;

department | employee | salary
------------±-----------±-------
IT | Max | 90000
IT | Randy | 85000
IT | Joe | 70000
Sales | Henry | 80000
Sales | Sam | 60000
(5 行记录)

练习⼗:平⾯上最近距离 (难度: 困难)
point_2d 表包含⼀个平⾯内⼀些点(超过两个)的坐标值(x,y)。
写⼀条查询语句求出这些点中的最短距离并保留2位⼩数。
1 |x | y |
2 |----|----|
3 | -1 | -1 |
4 | 0 | 0 |
5 | -1 | -2 |

create table point(x int,y int);
insert into point values(-1,-1);
insert into point values(0,0);
insert into point values(-1,-2);
x | y
----±—
-1 | -1
0 | 0
-1 | -2
(3 行记录)
select ,
rank() over (order by d)
from(
select foo.
,absx^2+absy*absy d
from(
select *,abs(p1.x-p2.x) absx,abs(p1.y-p2.y) absy
from point p1 cross join point p2
where p1.x!=p2.x or p1.y!=p2.y) foo) fooo
limit 1;

x | y | x | y | absx | absy | d | rank
----±—±---±—±-----±-----±–±-----
-1 | -1 | -1 | -2 | 0 | 1 | 1 | 1
(1 行记录)

练习⼗⼀:⾏程和⽤户(难度:困难)
Trips 表中存所有出租⻋的⾏程信息。每段⾏程有唯⼀键 Id,Client_Id 和 Driver_Id
是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’,
‘cancelled_by_driver’, ‘cancelled_by_client’)。
create table trips(
id int primary key,
Client_Id int,
Driver_Id int,
City_Id int,
Status char(10),
request_date date
);
insert into trips values(1,1,10,1,‘completed’,‘2013-10-1’);
insert into trips values(2,2,11,1,‘can_driver’,‘2013-10-1’);
insert into trips values(3,3,12,6,‘completed’,‘2013-10-1’);
insert into trips values(4,4,13,6,‘can_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,‘can_driver’,‘2013-10-3’);

id | client_id | driver_id | city_id | status | request_date
----±----------±----------±--------±-----------±-------------
1 | 1 | 10 | 1 | completed | 2013-10-01
3 | 3 | 12 | 6 | completed | 2013-10-01
5 | 1 | 10 | 1 | completed | 2013-10-02
6 | 2 | 11 | 6 | completed | 2013-10-02
7 | 3 | 12 | 6 | completed | 2013-10-02
8 | 2 | 12 | 12 | completed | 2013-10-03
9 | 3 | 10 | 12 | completed | 2013-10-03
2 | 2 | 11 | 1 | can_driver | 2013-10-01
4 | 4 | 13 | 6 | can_client | 2013-10-01
10 | 4 | 13 | 12 | can_driver | 2013-10-03
(10 行记录)

create table users(
users_id int,
banned char(4),
role char(10));

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 * from users;
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
(8 行记录)

select a.request_date,a.status,a.nu_com,b.nu,1-round(cast(nu_com as numeric)/cast(nu as numeric),2)CancellationRate
from(
select request_date,status,
count() nu_com
from users u,trips t
where u.users_id=t.client_id
and banned=‘no’
and status=‘completed’
group by request_date,status) a
join
(select request_date,count(
) nu
from users u,trips t
where u.users_id=t.client_id
and banned=‘no’
group by request_date) b
on a.request_date=b.request_date;

request_date | status | nu_com | nu | cancellationrate
--------------±-----------±-------±—±-----------------
2013-10-01 | completed | 2 | 3 | 0.33
2013-10-02 | completed | 2 | 2 | 0.00
2013-10-03 | completed | 1 | 2 | 0.50
(3 行记录)

Task06:综合练习相关推荐

  1. SQL学习笔记——task06:SQL综合练习

    SQL语言-综合练习 练习一:各部门工资最高的员工(难度:中等) 创建Employee表,包含所有员工信息,每个员工有其对应的id,salary和 department id. create tabl ...

  2. 【学习笔记】阿里云天池龙珠计划SQL训练营-Task06:综合练习题-10道经典题目

    本笔记为阿里云天池龙珠计划SQL训练营的学习内容 链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql 练习题1: 请使用A股上市公司 ...

  3. Task06:综合练习题-10道经典题目-天池龙珠计划SQL训练营 笔记

    本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql 第一题答案 SELECT Ma ...

  4. 2022-2028年中国商业综合体行业市场前瞻与投资规划分析报告

    [报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了商业综合体行业相关概述.中国商业综合体行业运行环境.分析了中国商业综 ...

  5. 2022-2028年中国城市地下综合管廊建设深度调研及投资前景预测报告

    [报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了城市地下综合管廊行业相关概述.中国城市地下综合管廊行业运行环境.分析 ...

  6. 2021年大数据Hive(十二):Hive综合案例!!!

    全网最详细的大数据Hive文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 前言 Hive综合案例 一.需求描述 二.项目表的字段 三.进 ...

  7. 数据库实验:数据库和表、查询、视图与安全性、存储过程及游标、触发器、综合实验-水果商店进阶

    数据库实验:数据库和表.查询.视图与安全性.存储过程及游标.触发器.综合实验-水果商店进阶 实验一.数据库和表 源码1: 源码2: 小结 实验二.查询 源码 小结 实验三.视图.安全性 源码: 小结 ...

  8. 2019 浙江大学 计算机 科目,2019考研大纲:浙江大学2019年《计算机学科专业基础综合》(单考)(科目代码907)...

    中公浙江研招网温馨提醒您关注考试大纲:[2019考研大纲:浙江大学2019年<计算机学科专业基础综合>(单考)(科目代码907)考试大纲] 2019考研报名答疑群:866383964 浙江 ...

  9. 2020版北大核心期刊目录_榜单|2020武大版核心期刊RCCSE高职高专成高院校学报类自然科学综合、社会科学综合学科权威、核心及准核心期刊目录...

    编者按 <中国学术期刊评价研究报告>(以下简称<RCCSE>)是邱均平教授团队创立的四大科教评价报告之一,于2009年3月正式推出第1版,后于2011年后连续推出了<RC ...

最新文章

  1. 深入理解Spring系列之三:BeanFactory解析
  2. 【渝粤教育】国家开放大学2019年春季 1009离散数学(本) 参考试题
  3. 安装Ubunutu音频视频库
  4. Emacs(洛谷P6866题题解,C++语言描述)
  5. winform如何实现将数据库数据加载到树上
  6. Spring 源码解析!
  7. zsh fg: no job control in this shell.
  8. SitePoint / Flippa Hack Day:入侵我们的第一个物联网项目
  9. linux离线安装rpm命令,CentOS-Linux安装软件命令是什么:rpm命令
  10. 全流量回溯分析系统介绍
  11. 微信小程序体验版分享的坑
  12. 微信公众号二次开发实现自动回复文字,图片,图文功能
  13. paddleocr训练自己的数据最简单方式软件一键训练
  14. 060616信用证点滴简结(三)--D/P即期跟单托收
  15. 跨考计算机面试英语自我介绍,2019考研复试面试英语自我介绍范文(2)
  16. 武汉理工计算机与名校的差距
  17. 饿了么(elementUI)组件库如何在vue项目中使用?
  18. Openfiler 基本介绍
  19. [张国荣][10CD][1991-1995][APE+CUE][3.60G][115][sqhhj0622#HD2PT]
  20. Junit报错:Argument(s) are different! Wanted:

热门文章

  1. linux必学的100个命令,Linux必学的60个命令
  2. vb.net、vb播放声音 wince下播放声音文件(wav)
  3. 使用pip安装pytorch失败,报错如下Could not find a version that satisfies the requirement torch
  4. OpenEuler 22.03 安装NextCloud
  5. 5个最适合开发人员的协作平台
  6. 基于Python的网上订餐系统的设计与实现
  7. 算法:next数组的求法详解
  8. 申请邮箱需要什么,邮箱申请方法开通条件教程分享
  9. 使用d3.js开发力导向图
  10. C语言通过指针间接的实现函数返回多个值