MySQL HIVE经典50题 21~25题

– 21、查询不同老师所教不同课程平均分从高到低显示

1
3个job
select distinct * from(
select t.tid,sc.cid,avg(sc.score)over(partition by t.tid,sc.cid) avg
from teacher t,course c,sc
where t.tid=c.tid and c.cid=sc.cid order by avg desc)b1
2
2个job
select t.tid,sc.cid,avg(sc.score) avg
from teacher t,course c,sc
where t.tid=c.tid and c.cid=sc.cid group by t.tid,sc.cid order by avg desc
3
2个job
select t.tid,sc.cid,avg(sc.score) avg
from teacher t join course c join sc
on t.tid=c.tid and c.cid=sc.cid group by t.tid,sc.cid order by avg desc
在这里插入图片描述
– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

1
1个job
select *
from(
select s.*,score,
dense_rank()over(partition by cid order by score desc) rank
from sc,student s
where sc.sid=s.sid)b1 where rank=2 or rank=3
在这里插入图片描述
– 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
60<score<70 不生效 但能执行

select c.cid,c.cname,sc.score,
case when 85<score<100 then 1
when 70<score<85 then 2
when 60<score<70 then 3
when 0<score<60 then 4
else 5 end l1
from sc,course c where sc.cid=c.cid
在这里插入图片描述

select c.cid,c.cname,sc.score,
case when 85<score and score<100 then 1
when 70<score and score<85 then 2
when 60<score and score<70 then 3
when 0<score and score<60 then 4
else 5 end l1
from sc,course c where sc.cid=c.cid
在这里插入图片描述
答案
3个job
select distinct * from(
select cid,cname,count(b1.l1)over(partition by cid,cname,l1) a,
round(count(b1.l1)over(partition by cid,cname,l1)/c,2) b
from(
select c.cid,c.cname,sc.score,
count(1)over() c,
case when 85<score and score<100 then 1
when 70<score and score<85 then 2
when 60<score and score<70 then 3
when 0<score and score<60 then 4
else 5 end l1
from sc,course c where sc.cid=c.cid
) b1
)b2
在这里插入图片描述

– 24、查询学生平均成绩及其名次

3个job
select *,row_number()over(order by avg desc)
from(
select sid,round(sum/max,2) avg from(
select sid,max©over() max,sum from(
select s.sid,
count(1) c,
sum(nvl(score,0)) sum
from student s left join sc on s.sid=sc.sid
group by s.sid
)b1
)b2
)b3
在这里插入图片描述

– 25、查询各科成绩前三名的记录
一个job
select * from
(
select *,
row_number() over(distribute by cid sort by score desc) as rm,
rank() over(distribute by cid sort by score desc) as rk,
dense_rank() over(distribute by cid sort by score desc) as drk
from sc
) a
where a.rm < 4
;

在这里插入图片描述

看后续

写SQL就像写填空题
先把语法写出来
select
from
where
group
然后再根据题意加条件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值