一道SQL语句的面试题目
今天一个朋友发了一道题目给我,我把此题目贴出来,以便大伙看看,共同研究研究。
题目:
已知关系模式:
1、s (sno,sname) 学生关系。sno 为学号,sname 为姓名
2、c (cno,cname,cteacher) 课程关系cno 为课程号,cname 为课程名,cteacher 为任课教师
3、sc(sno,cno,scgrade) 选课关系。scgrade 为成绩
要求实现如下5 个处理:
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
解题:
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
Select Sname As 学生姓名 From s Where Not Exists (Select * From c, Sc Where c.Cno = Sc.Cno And Cteacher = ‘李明‘ And Sc.Sno = s.Sno);Select Sname As 学生姓名 From s Where Sno Not In (Select Sno From c, Sc Where c.Cno = Sc.Cno And Cteacher = ‘李明‘);
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
Select s.Sno As 学生学号, s.Sname As 学生姓名, Avg(Sc.Scgrade) As 平均成绩 From s, Sc Where Sc.Sno = s.Sno And Sc.Sno In (Select Sc.Sno From Sc Where Sc.Scgrade < 60 Group By Sc.Sno Having Count(*) > 2) Group By s.Sno, s.Sname;
3. 列出既学过“01”号课程,又学过“02”号课程的所有学生姓名
select s.sno as 学生学号,s.sname as 学生姓名 from s where sno in(select sc.sno as 学生学号 from c,sc where c.cno=sc.cno and c.cno in(‘01‘,‘02‘) group by sno having count(distinct sc.cno)=2);
4. 列出“01”号课成绩比“02”号同学该门课成绩高的所有学生的学号
select sc1.sno as 学生学号 from sc as sc1,c as c1,sc as sc2,c as c2where sc1.cno=c1.cno and c1.cno=‘01‘ and sc2.cno=c2.cno and c2.cno=‘02‘and sc1.scgrade>sc2.scgrade group by sc1.sno;
5. 列出“01”号课成绩比“02”号课成绩高的所有学生的学号及其“01”号课和“02”号课的成绩
select sc1.sno as 学生学号, sc1.scgrade as no1grade ,sc2.scgrade as no2gradefrom sc as sc1,c as c1,sc as sc2,c as c2where sc1.cno=c1.cno and c1.cno=‘01‘ and sc2.cno=c2.cno and c2.cno=‘02‘and sc1.scgrade>sc2.scgrade group by sc1.sno;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。