有学生和班级两张表,如何用一条sql语句查询学生人数最多的班级信息?

如题
2024-12-04 06:35:36
推荐回答(2个)
回答1:

select * from 班级 a left join (select 班级id,count(*) as counta from 学生 group by 班级id) b on a.班级id=b.班级id
where b.counta=(select top 1 COUNT(*) as counta from StudioNews group by UserID order by counta desc) 可能存在人数最多的不止一个班级

回答2:

select top 1 max(学生ID) as result from 学生, 班级 where 学生.班级ID=班级.班级ID group by 班级 order by result desc;