insertinto S values('412109002126','xiaojing','男','2000-05-21','cs','上海');insertinto S values('131312321111','张三','男','2000-02-08','cs','上海');insertinto S values('256345611138','留香','女','2000-10-21','计算机学院','上海');insertinto C values('100002','高等数学1','0000',6);insertinto C values('500001','大学物理','0000',5);insertinto SC values('123131231111','100002',100);insertinto SC values('131312321111','100002',90);insertinto SC values('256345611138','100002',80);insertinto SC values('412109002126','100002',70);insertinto SC values('41210900286','100002',60);insertinto SC values('419109070212','100002',50);insertinto SC values('419109070226','100002',40);insertinto SC values('420109070226','100002',30);insertinto SC values('420109070426','100002',20);insertinto SC values('421109070126','100002',10);insertinto SC values('420109070226','500001',80);insertinto SC values('420109070226','0000',80);insertinto SC values('420109070226','0001',80);insertinto SC values('420109070226','500012',80);-- (1) 查询每一门的间接先修课的信息select c2.*from c c1, c c2 where c1.cpno = c2.cno and c1.cpno isnotnull;-- (2) 查询所有学生的选课情况,没选课的学生也要显示。select s.sname, sc.cno from sc fulljoin s on sc.sno = s.sno;-- (3) 查询选修了“500012”课程且成绩在80~90之间的学生信息。select s.sno, s.sname, s.saddress from s, sc
where s.sno = sc.sno and sc.cno ='500012'and sc.score between80and90;-- (4) 查询每个学生的学号、姓名、选修课名称及成绩select s.sno, s.sname, c.cname, sc.score from sc, s, c where sc.sno = s.sno and sc.cno = c.cno;-- (5) 查询女生人数最多的两个学院,查询结果显示:学院、女生人数。selecttop2 dept, persons from(select sdept as dept,count(s.sno)as persons from s groupby sdept
) tmp orderby persons desc;-- (6) 查询最低分低于50分的学生信息select s.sno, s.sname from sc, s where s.sno = sc.sno and sc.score <50;-- (7) 查询与“张三”同岁的“计算机学院”的学生信息select s1.sno, s1.sname from s s1
where s1.sdept ='计算机学院'and datediff(yyyy, s1.Sbirthday,getdate())=(select datediff(yyyy,s2.Sbirthday,getdate())from s s2 where s2.sname ='张三');-- (8) 查找“高等数学1”成绩最好的10个学生的信息。selecttop10 sno, sname, score from(select s.sno, s.sname, sc.score from sc, c, s
where sc.cno = c.cno and c.cname ='高等数学1'and s.sno = sc.sno
) tmp orderby score desc;-- (9) 找出每个学生超过他的选修课程平均成绩的课程号select sc.sno, cno from sc,(select sno,avg(score) avg_score from sc groupby sno
) tmp where score > avg_score and sc.sno = tmp.sno;-- (10) 查询其它学院比“计算机学院”的学生年龄都大的学生信息select sno, sname, sdept from s where s.sdept !='计算机学院'and s.Sbirthday <=ALL(select tmp.sbirthday from s tmp where tmp.Sdept ='计算机学院');-- (11) 查询没有选课的学生信息select*from s where s.sno notin(selectdistinct sno from sc where cno isnotnull);-- (12) 查询至少有两门课超过85分的学生的基本信息select*from s,(select sno,sum(casewhen score >85then1else0end)as cnt from sc groupby sno
) tmp where s.sno = tmp.sno and cnt >=2;-- (13) 查询只被一个学生选修了的课程信息select sc.cno from sc,(selectcount(sno) cnt, cno from sc groupby cno
) tmp where1= tmp.cnt and sc.cno = tmp.cno;-- (14) 查询所有没有选修“500001”课程的学生信息 ( EXISTS练习)select*from s wherenotexists(select*from sc where sc.sno = s.sno and sc.cno =500001);-- (15) 查询选修了全部课程的学生信息select*from s wherenotexists(select*from c wherenotexists(select*from sc where sc.cno = c.cno and sc.sno = s.sno
))--(16) 查询至少选修了“李勇”选修的所有课程的学生信息select*from sc x wherenotexists(select*from sc y where y.sno =(select sno from s where s.sname ='李勇')andnotexists(select*from sc z where z.cno = y.cno and z.sno = x.sno
))