题解 | 计算男生人数以及平均GPA

计算男生人数以及平均GPA

https://www.nowcoder.com/practice/7d9a7b2d6b4241dbb5e5066d7549ca01

select
count(gender) as male_num,
round(avg(gpa),1) as avg_gpa
from user_profile
where gender = 'male'
# 注意第一行的 ,

限定条件为 男性用户;有多少人,明显是计数,count函数;平均gpa,求平均值用avg函数;细节问题:根据输出示例,有两个问题需要注意:表头重命名,用as语法浮点数的平均值可能小数点位数很多,按照示例保存一位小数,用round函数

select
round(count(1),1)as male_num
,round(avg(gpa),1) as avg_gpa
from user_profile
where gender = 'male'

如果想要分开计算男性的数量和所有人的平均值:

select
count(if(gender='male'),1,null)as male_num
,round(avg(gpa),1) as avg_gpa
from user_profile

SQL刷题 文章被收录于专栏

SQL刷题文章专栏

全部评论
select count(1) as male_num ,avg(gpa) as avg_gpa from user_profile where gender = 'male'
点赞 回复 分享
发布于 05-01 02:33 河南

相关推荐

牛奶配面包:第二个经典博弈题目吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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