【分类】:连接查询、多表连接from、join

分析思路

一开始打算使用avg(),但是涉及到不同用户,实现不了,所以只能用除法

  • 分子: 每个学校 答题数量
  • 分母: 每个学校 不同答题用户
select 查询结果 [学校;平均答题数]
from 从哪张表中查询数据[多个join连接的表]
group by 分组条件 [学校]
order by 排序要求  [学校升序]

扩展

前往查看:****************************************

求解代码

方法一:

表 join 表 using(连接列)

select 
    university,
    count(device_id)/count(distinct(device_id)) AS avg_answer_cnt 
from user_profile 
join question_practice_detail using(device_id)
group by university
order by university

方法二

from表连接

select 
    a.university,
    count(a.device_id)/count(distinct(a.device_id)) as avg_answer_cnt 
from user_profile a, question_practice_detail b
where a.device_id = b.device_id
group by university
order by university