刷牛客2024.1.24

查询语句

第十九题分组过滤练习题

select
university,
round(avg(question_cnt),3) AS avg_question_cnt,
round(AVG(answer_cnt),3) as avg_answer_cnt
FROM
user_profile
GROUP BY 
university

HAVING
avg_question_cnt<5  or
avg_answer_cnt<20

用select老进行查询然后用group by来进行分组查询,having一般与group by来一起使用来指定组的条件。


第二十题分组过滤练习

select university ,round(avg(question_cnt),4) as avg_question_cnt
from user_profile
group by university
order by avg_question_cnt

分组过滤排序,group by 是进行分组过滤,order by 是进行排序的而且是降序排序。


第二十一题浙江大学用户题目回答情况

select device_id,question_id,result
from question_practice_detail
where device_id in (
    select device_id from user_profile
    where university ='浙江大学'
)
order by question_id;

select查询device_id ,question_id,result

用两个where来连接两个表来进行连接,然后用order by 来进行对question_id进行排序。


第二十二题统计每个学校的答过题的用户的平均

select university,
round(count(qpd.question_id)/count(distinct qpd.device_id),4) as avg_answer_cnt
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id =qpd.device_id
group by university

主要是考察avg_answer_cnt这列是怎么算来的,inner join子句将一个表中的行与其他表中的行进行匹配,并可以查询这两个表中的字段。连接条件的关键字ONINNER JOIN语句之后。连接条件是用于在主表和其他表之间匹配行的条件。最后再进行排序。


  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值