如何编写查询以显示学生人数最少的部门的名称。根据部门名称按升序对结果进行排序
答案 0 :(得分:1)
select d.department_name from Department d, Student s where
d.department_id = s.department_id
group by d.department_name
having count(s.student_id)<=all
(select count(s.student_id) from Department d, Student s where
d.department_id = s.department_id
group by d.department_name)
order by department_name;
尝试一下。
答案 1 :(得分:0)
select d.department_id, d.department_name
from Department d
join Student s on d.department_id = s.department_id
group by d.department_id
having count(s.student_id) = (select min(count(s2.student_id))
from student s2
join department d2
on s2.department_id = d2.department_id
group by d2.department_id)
order by d.department_name
您必须加入2个表才能获得所需信息。
您还必须根据所选信息对它们进行分组,以便您可以对学生进行统计。
最后,你放置条件。需要子查询来检索最少数量的学生。
答案 2 :(得分:0)
select d.department_name from
(select dd.department_name, count(di.department_id) as id from student di
join department dd on di.department_id=dd.department_id group by dd.department_name) d,
(select min(count(*)) as new from student group by department_id) d2
where d.id=d2.new;
答案 3 :(得分:-1)
select department_name
from Department
join Student
on Department.department_id=Student.department_id
having count(*) in
( select min(count(*)) from Student group by department_id)
group by Department.department_id,department_name
order by department_name asc;