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

【场景】:某个条件下做统计

【分类】:where 数据 > select 条件

分析思路

难点:

1.理解题目要求的是什么

(1)得到SQL试卷的平均分

  • [条件]:where tag = 'SQL'

  • [条件]:avg(score)

(2)找到得分不小于平均分的最低分

  • [条件]:score >= score_avg

  • [使用]:min(score) 或者 正序取第一条数据

求解代码

方法一:

with子句

with
    main as(
        #得到SQL试卷的平均分
        select
            avg(score) as score_avg
        from exam_record a, examination_info b
        where tag = 'SQL'
        and a.exam_id = b.exam_id
    )
#找到得分不小于平均分的最低分
select 
    min(score) as min_score_over_avg
from exam_record a, examination_info b,main
where b.tag = 'SQL'
and a.exam_id = b.exam_id
and score >= score_avg

方法二:

where select

select 
    min(score) as min_score_over_avg
from exam_record a, examination_info b
where b.tag = 'SQL'
and a.exam_id = b.exam_id
and score >= (
    select
        avg(score) as score
    from exam_record a, examination_info b
    where b.tag = 'SQL'
    and a.exam_id = b.exam_id
)

方法三:

order by limit

select 
    score as min_score_over_avg
from exam_record a, examination_info b
where b.tag = 'SQL'
and a.exam_id = b.exam_id
and score >= (
    select
        avg(score) as score
    from exam_record a, examination_info b
    where b.tag = 'SQL'
    and a.exam_id = b.exam_id
)
order by score
limit 1
全部评论

相关推荐

星期一的大老师:项目描述 和 技术栈单开一栏;八股文:算法与数据结构,计算机网络一定要写,操作系统不了解可以不写;Linux命令,Git,Docker基础命令和基本使用一定要写,要有实际使用场景的解决经验;项目的八股文上:redis 解决 缓存雪崩,缓存击穿,缓存穿透的解决方案,一个问题的不同方案可以一起用,不需要重复在两个项目写。第二个项目换一个。小厂可以投一投
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

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