Q值。让部门的规模是教师的数量加上学生的数量。找到最大部门的名称。
select dept_name
from(select dept_name,count(id) as people from instructor group by dept_name
union
select dept_name,count(id) as people from student group by dept_name)
having count(id)=(select max(A.people)
from (select count(id) as people
from (select dept_name,count(id) as people
from instructor
group by dept_name
union
select dept_name,count(id) as people
from student
group by dept_name)
group by dept_name) as A)
讲师表视图:
学生表视图:
[在此输入图像说明] [3]
结果是Comp.Sci。
答案 0 :(得分:0)
SELECT COUNT(*) +
(SELECT COUNT(*) FROM student where dept_name = t1.dept_name) as total_member,
t1.dept_name FROM instructor
GROUP BY t1.dept_name
ORDER BY total_member DESC
LIMIT 1
答案 1 :(得分:0)
我测试了我的测试样本数据库并且它有效!
它也适合你。要包含重复记录,您可以考虑使用UNION ALL,如下所示:
SELECT MAX(total),dept_name
FROM (
SELECT count(a.dept_name) as total,a.dept_name
from student a left join instructor b ON a.dept_name = b.dept_name GROUP BY a.dept_name
union all select count(c.dept_name) as total,c.dept_name
from instructor c RIGHT JOIN student d ON c.dept_name = d.dept_name group by c.dept_name) combine_table
答案 2 :(得分:0)
select top 1 dept_name from
(
select dept_name, sum(student_num + inst_num)
from(
select dept_name, 0 as student_num, count(id) as inst_num from instructor group by dept_name
union all
select dept_name,count(id) as student_num, 0 as inst_num from student group by dept_name
)t group by t.dept_name
order by sum(student_num + inst_num) desc
)