题解 | #SQL类别高难度试卷得分的截断平均值#
SQL类别高难度试卷得分的截断平均值
https://www.nowcoder.com/practice/a690f76a718242fd80757115d305be45
这道题的考点较为丰富,包括表连接、条件筛选、均值计算、保留小数等,其中在剔除最大最小值时卡了一下,有想过使用limit,但没奏效,最会还是选择较为直接暴力的min、max函数。
- 表连接:由于分数数据存放在exam_record表中,因而需要以exam_record表为主表进行外连接,此处选择使用right join。
- 条件筛选:要求中有好几个细节,首先是是针对SQL类中难度为hard的数据,同时观察可知,最终数据是不包含空值的,因此在筛选时同样要予以剔除。
- 均值计算:avg()函数
- 保留小数:round(x,d),其中x为需要进行处理的数据,d为需要保留的小数点位。
select tag,difficulty,round(avg(score),1) clip_avg_score from examination_info t1 right join exam_record t2 on t1.exam_id = t2.exam_id where tag = 'SQL' and difficulty = 'hard' and score is not null and score > (select min(score) from exam_record where exam_id = 9001 and score is not null) // 筛选出min值后需要对整个部分加括号 and score < (select max(score) from exam_record where exam_id = 9001 and score is not null)

