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