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

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

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

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

题目主要信息:

  • 从试卷作答记录表中找到类别为的SQL试卷得分不小于该类试卷平均得分的用户最低得分
  • 其中试卷信息记录在表examination_info(包括试卷ID、类别、难度、时长、发布时间),答题信息记录在表exam_record(包括试卷ID、用户ID、开始时间、结束时间、得分)

问题拆分:

  • 要找类别为SQL的试卷平均得分:
    • 得分信息在exam_record,试卷类别在表examination_info中,因此要将两个表以exam_id连接。知识点:join...on...
    • 从连接后的表中找到类别为SQL的试卷的分数。知识点:select...from...where...
    • 计算得分的平均值。知识点:avg()
  • 找到类别SQL的试卷得分大于平均得分的最小值:
    • 得分信息在exam_record,试卷类别在表examination_info中,因此要将两个表以exam_id连接。知识点:join...on...
    • 从连接后的表中找到类别为SQL的试卷且分数大于刚刚找到的平均分的分数。知识点:select...from...where...and...
    • 从中选出最小值。知识点:min()

代码:

select min(e_r.score) as min_score_over_avg
from exam_record e_r join examination_info e_i
on e_r.exam_id = e_i.exam_id
where e_i.tag = 'SQL'
and score >= (select avg(e1.score)
             from exam_record e1 join examination_info e2
             on e1.exam_id = e2.exam_id
             where tag = 'SQL'
             )
孤帆远影碧空尽 文章被收录于专栏

牛客网各类题单题解~

全部评论
为什么left join不行呢
1 回复 分享
发布于 2022-03-01 11:22
min(if(score>=avg(score),score,null)) as min_score_over_avg 为什么不可以啊,网上也查不到,哭了
1 回复 分享
发布于 2022-08-04 10:42
select score as min_score_over_avg from exam_record e1 left join (select exam_id,avg(score)as score_over_avg from exam_record where exam_id in (select exam_id from examination_info where tag ='SQL' ) group by exam_id ) e2 on e1.exam_id =e2.exam_id where e1.score >e2.score_over_avg and e1.score is not null order by e1.score asc limit 1 为什么这样写不行
1 回复 分享
发布于 2022-04-12 11:33
为什么我的结果是none,要加上score is not null 才可以
1 回复 分享
发布于 2021-12-19 21:04
select t3.score min_score_over_avg from exam_record t3 inner join examination_info t4 on t3.exam_id=t4.exam_id where t3.score>= (select avg(t1.score) from exam_record t1 inner join examination_info t2 on t1.exam_id=t2.exam_id where t2.tag='SQL') and t4.tag='SQL' order by t3.score limit 1 我是这样写的,多执行几次后显示过关~所以这样写没毛病吧,哈哈
点赞 回复 分享
发布于 2023-06-03 14:10 河北
提问:sql是先执行子查询再执行外面的查询吗?感觉写俩遍连接和where条件好繁琐,能不能直接把“sql”提出来,然后连接,然后算平均再取最小
点赞 回复 分享
发布于 2023-02-19 01:34 江苏
上面的e_i.tag = 'SQL'不写是不是也可以呢,毕竟子查询已经写了,我在MySQL跑程序没写也是一样的结果
点赞 回复 分享
发布于 2022-03-22 22:20
想问一下为什么最后where里还要嵌套,用这个score>=avg(score)为啥不行呢
点赞 回复 分享
发布于 2022-03-01 10:46
为什么子查询里的表连接不能和主查询的标链接名字一样呀?
点赞 回复 分享
发布于 2022-02-22 15:59
看我写的思路https://blog.nowcoder.net/n/762d0e2291084042b71c8f5e58572f9a
点赞 回复 分享
发布于 2022-02-11 17:01
请问为什么计算平均分的时候要用子函数再次连接 这样为什么不可以select min(score) min_score_over_avg from exam_record er join examination_info ei on er.exam_id=ei.exam_id and tag='SQL' where score>=avg(score)
点赞 回复 分享
发布于 2022-02-09 17:58
要把score is not null加上,不然没法计算了
点赞 回复 分享
发布于 2021-12-23 17:15

相关推荐

不愿透露姓名的神秘牛友
07-21 13:38
8月实习会变多吗现在还没找到实习该怎么办...回复的hr好少
码农索隆:3-4月就要开始找,基本上6月份就发offer,7月初已经开始暑期实习了。
点赞 评论 收藏
分享
LazyBreeze:项目尽量体现你对技术的理解和深度,不是说把中间件用一下就完事了,你项目里面提到集群和分布式,你真在服务器上部署过吗,感觉太假了,第二个项目说自己用了微服务的什么组件,只是用了没有自己的思考,很难让面试官注意到你的简历。针对某几个技术点自己多思考一下,考虑一下有没有别的替代方案,可以写一下,即使没有真的实现
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-24 13:35
点赞 评论 收藏
分享
评论
87
9
分享

创作者周榜

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