题解 | #得分不小于平均分的最低分#
得分不小于平均分的最低分
http://www.nowcoder.com/practice/3de23f1204694e74b7deef08922805b2
明确题意:
找到SQL试卷得分不小于该类试卷平均得分的用户最低得分。
问题拆解:
- 本题主要是考察知识点:avg、join、min
- 用t1、t2关联出SQL的平均分
- 用t3、t4关联出SQL的所有分数,然后用where过滤出>=avg的分数,再取min,则结果符合预期!
代码实现:
select min(t3.score) as min_score_over_avg from ( select exam_id,score from exam_record where score is not null )t3 join ( select exam_id from examination_info where tag = 'SQL' )t4 on t3.exam_id = t4.exam_id where t3.score >= ( select avg(t1.score) as avg_score from ( select exam_id,score from exam_record where score is not null )t1 join ( select exam_id from examination_info where tag = 'SQL' )t2 on t1.exam_id = t2.exam_id );
注意:
where tag=‘xxx’是区分大小写的,如果写成where tag=‘SqL’则结果错误!!!
不足之处,欢迎指正。