题解 | #作答试卷得分大于过80的人的用户等级分布#

作答试卷得分大于过80的人的用户等级分布

https://www.nowcoder.com/practice/5bc77e3a3c374ad6a92798f0ead4c744

嵌套子查询

非常简洁

  1. 找出SQL对应exam_id
  2. 找出分数符合条件的uid
  3. 统计uid

分别在三个表当中实现

select level, count(uid) as level_cnt
from user_info
where uid in (
    select distinct uid 
    from exam_record
    where exam_id in (
        select exam_id
        from examination_info
        where tag = 'SQL'
    ) and score > 80 and submit_time is not null

)
group by level
order by level_cnt desc

这个是一开始的想法,将子查询混合了联表,但是觉得这个不如上面的解法好

有两点需要注意

  • 对uid进行去重
  • submit_time 不能为空

select level, count(uid) as level_cnt
from user_info
where uid in (
    select uid
    from exam_record
    inner join examination_info using(exam_id)
    inner join user_info using(uid)
    where tag = 'SQL' and score > 80 and submit_time is not null
)
group by level
order by level_cnt desc

全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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