当前位置:网站首页>牛客刷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边栏推荐
猜你喜欢

华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)

数据挖掘-04

34、树莓派进行人体姿态检测并进行语音播报
![leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]](/img/44/bf1d9b9d85939e73bc44be2f9701e1.png)
leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]

PAT 1163 Dijkstra Sequence(30)

态路小课堂丨浅谈优质光模块需要具备的条件!

易优压双驱挖掘机压路机器类网站源码 v1.5.8

RGB系列开发稳定响应快速灯带拾音灯氛围灯等应用定制方案

Towhee 每周模型

PyTorch 进阶之路:在 GPU 上训练深度神经网络
随机推荐
数据挖掘-03
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
软件测试之发现和解决bug
人像分割技术解析与应用
leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
LeetCode_动态规划_中等_377.组合总和 Ⅳ
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
让程序员早点下班的效率工具
Do wildcard SSL certificates not support multiple domains?
免费使用高性能的GPU和TPU—谷歌Colab使用教程
珠海首个水环境安全监测系统上线
How does the SAP ABAP OData service support the Create operation trial version
gpio模拟串口通信
数字证书原理
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
JMP Pro 16.0软件安装包下载及安装教程
Multithreading Case - Timer
LeetCode_动态规划_中等_313.超级丑数
JMP Pro 16.0 software installation package download and installation tutorial
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹