第1关:交换工资
任务描述
本关任务:给定一张 tb_Salary 表,如下所示,有 m = 男性 和 f = 女性的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。
| id | name | sex | salary |
|---|---|---|---|
| 1 | Elon | f | 7000 |
| 2 | Donny | f | 8000 |
| 3 | Carey | m | 6000 |
| 4 | Karin | f | 9000 |
| 5 | Larisa | m | 5500 |
| 6 | Sora | m | 500 |
要求只使用一句更新update语句,且不允许含有任何select语句完成任务。
#请在此添加实现代码
########## Begin ##########
update tb_Salary
set sex=
case
when sex="m" then "f"
when sex="f" then "m"
end;
########## End ##########
第2关:换座位
任务描述
本关任务:改变相邻俩学生的座位。
小美是一所中学的信息科技老师,她有一张 tb_Seat座位表,平时用来储存学生名字和与他们相对应的座位 id。
tb_Seat表结构数据如下:
| id | name |
|---|---|
| 1 | Elon |
| 2 | Donny |
| 3 | Carey |
| 4 | Karin |
| 5 | Larisa |
现在小美想改变相邻俩学生的座位(若学生人数为奇数,则无需改变最后一位同学的座位),现在需要你编写SQL输出小美想要的的结果。
#请在此添加实现代码
########## Begin ##########
select if(id%2=0,id-1,if(id=(select max(id) from tb_Seat),id,id+1))as id,name
from tb_Seat order by id asc;
########## End ##########
第3关:分数排名
任务描述
本关任务:编写SQL查询来实现二种排名方式的分数排名。
score表结构信息如下:
| Id | Score |
|---|---|
| 1 | 3.52 |
| 2 | 3.65 |
| 3 | 4.23 |
| 4 | 3.85 |
| 5 | 4.23 |
| 6 | 3.65 |
如果两个分数相同,则两个分数排名(Rank)相同。
情况一:平分后的下一个名次是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。例:1、1、2、3、4、4。
情况二:排名是非连续的。例:1、1、1、4、4、6。
#请在此添加实现代码
########## Begin ##########
select Score,(select count(distinct score) from score where score >=s.score) as Rank
from score as s
order by Score desc;
select Score,(select count(*) from score as s2 where s2.score >s1.score)+1 as Rank
from score as s1
order by Rank;
########## End ##########
第4关:体育馆的人流量
任务描述
本关任务:某市建了一个新的体育馆,每日人流量信息被记录在gymnasium表中:序号 (id)、日期 (date)、 人流量 (visitors_flow)。
请编写一个查询语句,找出人流量处于高峰的记录 id、日期 date 和人流量 visitors_flow,其中高峰定义为前后连续三天人流量均不少于 100。
gymnasium表结构数据如下:
| id | date | visitors_flow |
|---|---|---|
| 1 | 2019-01-01 | 58 |
| 2 | 2019-01-02 | 110 |
| 3 | 2019-01-03 | 123 |
| 4 | 2019-01-04 | 67 |
| 5 | 2019-01-05 | 168 |
| 6 | 2019-01-06 | 1352 |
| 7 | 2019-01-07 | 382 |
| 8 | 2019-01-08 | 326 |
| 9 | 2019-01-09 | 99 |
提示:每天只有一行记录,日期随着 id 的增加而增加。
#请在此添加实现代码
########## Begin ##########
select distinct a.* from gymnasium a,gymnasium b,gymnasium c
where a.visitors_flow>=100 and b.visitors_flow>=100 and c.visitors_flow>=100
and (
(a.id = b.id-1 and b.id = c.id -1) or
(a.id = b.id-1 and a.id = c.id +1) or
(a.id = b.id+1 and b.id = c.id +1)
) order by a.id;
########## End ##########
第5关:统计总成绩
任务描述
本关任务:计算每个班的语文总成绩和数学总成绩,其中低于 60 分的成绩不记入总成绩。
tb_score结构数据:
| name | chinese | maths |
|---|---|---|
| A | 89 | 98 |
| B | 99 | 89 |
| C | 55 | 66 |
| D | 88 | 66 |
| E | 55 | 66 |
| F | 88 | 99 |
tb_class表结构数据:
| stuname | classname |
|---|---|
| A | C1 |
| B | C2 |
| C | C3 |
| D | C2 |
| E | C1 |
| F | C3 |
#请在此添加实现代码
########## Begin ##########
select t1.classname,t1.chinese,t2.maths
from(select c.classname classname,sum(s.chinese) chinese from tb_class c,tb_score s
where c.stuname=s.name and s.chinese>=60 group by c.classname) t1,
(select c.classname classname,sum(s.maths) maths from tb_class c,tb_score s
where c.stuname=s.name and s.maths>=60 group by c.classname) t2
where t1.classname=t2.classname;
########## End ##########
一系列SQL任务,包括使用单个更新语句交换表中性别的值,重新排列座位顺序,实现两种不同的分数排名方法,识别体育馆人流量高峰,以及统计班级的语文和数学总成绩(忽略不及格分数)。这些任务展示了数据库管理和数据分析的基本技能。
4443

被折叠的 条评论
为什么被折叠?



