每个年龄的学生在各学校最大的数量

select age, max(cnt) max_cnt
from (
select age, university, count(*) cnt
from user_profile
group by age, university
) t
group by age
order by max_cnt desc;
为什么子查询以后还要groupby age 啊,不是子查询的表格里已经groupby 过了吗?

是计算每一个年龄的最大值数量,肯定要先算出每一个学院的年龄的数量,然后再求出最大值呀,
子查询分组只能取得count,你是用max不得再次分组么或者只能查出分组后的max值,一个group by 不能同时 max(count(1))这样写,你要么只能求一个最大值这样
select max(count(device_id)) max_cnt
from user_profile
group by university,age
order by max_cnt desc