SQL19、分组过滤练习题

思路:数据中存在一个学校的多个用户,所以首先要对university进行分组,然后用AVG函数求均值。并且在用GROUP BY分组时,要用HAVING关键字添加限定条件

代码解释:从表user_profile中取出列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

题目:https://www.nowcoder.com/practice/ddbcedcd9600403296038ee44a172f2d?tpId=199&tqId=1975671&ru=/exam/oj&qru=/ta/sql-quick-study/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199

posted @ 2022-07-11 12:26  tiansz  阅读(24)  评论(0编辑  收藏  举报