当前位置:网站首页>牛客刷SQL--7
牛客刷SQL--7
2022-08-01 13:42:00 【小唐同学(๑><๑)】
作者简介:大家好我是小唐同学(๑><๑),大家可以叫我小唐
个人主页:小唐同学(๑><๑)的博客主页
系列专栏:数据库
博友们如果也是新手入门MySQL 我希望大家可以多加练习 MySQL题库在牛客网就有已经给大家附上链接,可以直接点击跳转:点击跳转
牛客网支持ACM模式哦,刷算法题也很推荐哦!!!
下面上文章------》
目录
SQL17 计算男生人数以及平均GPA
描述
题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。
示例:user_profile
id device_id gender age university gpa 1 2138 male 21 北京大学 3.4 2 3214 male 复旦大学 4.0 3 6543 female 20 北京大学 3.2 4 2315 female 23 浙江大学 3.6 5 5432 male 25 山东大学 3.8 6 2131 male 28 北京师范大学 3.3 根据输入,你的查询应返回以下结果,结果保留到小数点后面1位(1位之后的四舍五入):
male_num avg_gpa 4 3.6 示例1
输入:
drop table if exists user_profile; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float); INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8); INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);复制输出:
4|3.6
select count(id) male_num, avg(gpa) avg_gpa
from user_profile
where gender = 'male';SQL19 分组过滤练习题
描述
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
示例:user_profile
id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt 1 2138 male 21 北京大学 3.4 7 2 12 2 3214 male 复旦大学 4.0 15 5 25 3 6543 female 20 北京大学 3.2 12 3 30 4 2315 female 23 浙江大学 3.6 5 1 2 5 5432 male 25 山东大学 3.8 20 15 70 6 2131 male 28 山东大学 3.3 15 7 13 7 4321 female 26 复旦大学 3.6 9 6 52 第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4在过去的30天里面活跃了7天,发帖数量为2,回答数量为12
。。。
最后一行表示:id为7的用户的常用信息为使用的设备id为4321,性别为男,年龄26岁,复旦大学,gpa为3.6在过去的30天里面活跃了9天,发帖数量为6,回答数量为52根据示例,你的查询应返回以下结果,请你保留3位小数(系统后台也会自动校正),3位之后四舍五入:
university avg_question_cnt avg_answer_cnt 北京大学 2.5000 21.000 浙江大学 1.000 2.000 解释: 平均发贴数低于5的学校或平均回帖数小于20的学校有2个
属于北京大学的用户的平均发帖量为2.500,平均回答数量为21.000
属于浙江大学的用户的平均发帖量为1.000,平均回答数量为2.000
示例1
输入:
drop table if exists user_profile; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int , `question_cnt` float, `answer_cnt` float ); INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);复制输出:
university|avg_question_cnt|avg_answer_cnt 北京大学|2.500|21.000 浙江大学|1.000|2.000
# select avg(question_cnt) as from user_profile group by university
select
university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg_question_cnt<5 or avg_answer_cnt<20边栏推荐
猜你喜欢

Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing

Qt实战案例(56)——利用QProcess实现应用程序重启功能

什么是混合元编程

什么是一致性哈希?可以应用在哪些场景?

Batch replace tables in Word with pictures and save

台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视

leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】

消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?

Programmer's Romantic Tanabata

SAP ABAP OData 服务如何支持创建(Create)操作试读版
随机推荐
人像分割技术解析与应用
Do wildcard SSL certificates not support multiple domains?
Programmer's Romantic Tanabata
PanGu-Coder:函数级的代码生成模型
【每日一题】952. 按公因数计算最大组件大小
E - Red and Blue Graph(组合数学)
JMP Pro 16.0软件安装包下载及安装教程
六石编程学:问题要面对,办法要技巧,做不好的功能要想办法
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
Istio投入生产的障碍以及如何解决这些问题
How do we do full-link grayscale on the database?
Batch replace tables in Word with pictures and save
lua脚本关键
硬链接、软连接浅析
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
2022-07-25 网工进阶(二十一)BGP-路由反射器、联盟、聚合
2022图片在线加水印源码
批量替换Word中的表格为图片并保存
Software designer test center summary (interior designer personal summary)
Data Mining-04