我在数据库中有以下关系
ENROLLMENT (snum: int, cname:varchar(100))
其中snum是学生ID,cname是课程名称
我希望找到注册人数最多的班级名称。
答案 0 :(得分:3)
有多种解决方案。一种解决方案是使用SELECT TOP 1
:
SELECT TOP 1
cname, count(*) as NumberOfStudents
FROM ENROLLMENT
GROUP BY cname
ORDER BY count(*) desc
答案 1 :(得分:2)
使用公用表表达式(cte):
with cte as
(
select cname, count(*) as cnt
from ENROLLMENT
group by cname
)
select cname
from cte
where cnt = (select max(cnt) from cte)