很难的SQL查询问题

来源:百度知道 编辑:UC知道 时间:2024/05/20 14:13:32
现有数据库关系表:#为主键
Student(#sid,sname,grade)
Teacher(#tid,tname)
Course(#cid,cname)
Temp(#sid,#cid,tid,score)

现在要查询:
1、有15个同一年级(grade)学生(student)选修的课程的名字(cname)
2、给所有年级都上过课的老师的名字(tname)

求高手帮下忙写出相应的SQL语句。。。谢谢~~

--有15个同一年级(grade)学生(student)选修的课程的名字(cname)
select * from course where cid in
(select a.cid from temp a,course b,student c where a.cid=b.cid and a.sid=c.sid group by a.cid,c.grade having count(*)=15 )

--给所有年级都上过课的老师的名字(tname)
select * from teacher where tid in
(select a.tid from temp a,student b where a.sid=b.sid group by a.tid having count(distinct b.grade)=(select count(distinct grade) from student))

选修人数:
select grade,cid,count(*) as 选修人数
from student join temp on student.sid=temp.sid join course on temp.cid=course.cid join teacher on temp.tid=teacher.tid
group by grade,cid
having count(*)=15

全部课程都上过课的老师名字
select *
from temp join course on temp.cid=course.cid right join teacher on temp.tid=teacher.tid
where temp.tid is null

1\select c.cname
from course c,
(select a.grade,b.cid,count(*) cnt
from student a,temp b
where a.sid=b.sid
group by a.grade,b.