题解 | #得分不小于平均分的最低分#

得分不小于平均分的最低分

https://www.nowcoder.com/practice/3de23f1204694e74b7deef08922805b2

-- 方法一 窗口函数
-- 1、拼表,并利用窗口函数对exam_id进行分区,并计算每个分区的平均得分
# select
#     score,
#     avg(score) over(partition by er.exam_id) avg_score
# from exam_record er left join examination_info ei on er.exam_id=ei.exam_id
# where tag='SQL'

-- 2、利用子查询,where过滤score大于均值的数据,并通过min取最小值
# select min(score) min_score_over_avg
# from (
#     select
#         score,
#         avg(score) over(partition by er.exam_id) avg_score
#     from exam_record er left join examination_info ei on er.exam_id=ei.exam_id
#     where tag='SQL'
# ) tmp
# where score>=avg_score

-- 方法二 子查询
-- 1、计算SQL试卷的平均得分
# select avg(score)
# from exam_record er left join examination_info ei on er.exam_id=ei.exam_id
# where tag='SQL'

-- 2、计算大于等于平均得分的用户最低分(此处不想重新用join,故用了两个子查询)
select min(score) min_score_over_avg
from exam_record
where score >= (
    select avg(score)
    from exam_record er left join examination_info ei on er.exam_id=ei.exam_id
    where tag='SQL'
) and exam_id in (select exam_id from examination_info where tag='SQL')

全部评论

相关推荐

牛客小菜鸡66:boss里面,招人的叫老板,找工作的叫牛人
点赞 评论 收藏
分享
09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务