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

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

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

select level, COUNT(distinct er.uid) AS level_cnt
FROM user_info AS ui 
JOIN exam_record AS er using(uid)
JOIN examination_info AS ei using(exam_id)
WHERE ei.tag = 'SQL' AND score > 80
GROUP BY level
ORDER BY level_cnt DESC, level DESC

子查询

select level, COUNT(uid) AS level_cnt
FROM user_info AS ui 
WHERE uid in (
    select uid FROM exam_record
    WHERE score > 80 AND exam_id in(
        select exam_id FROM examination_info
        WHERE tag = 'SQL'
    )
)
GROUP BY level
ORDER BY level_cnt DESC, level DESC

关于子查询和关联的用法

我的理解是如果存在一个表 包含了所需要的字段 就可以用子查询

否则用关联

全部评论

相关推荐

04-27 08:59
常州大学 Java
韵不凡:软件开发的工作需要博士吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务