第5章 数据操作语句 习题答案 --1.查询SC表中的全部数据 select * from SC --2.查询计算机系学生的姓名和年龄 select Sname,Sage from Student where Sdept = '计算机系' --3.查询成绩在70~80分的学生的学号、课程号和成绩 select Sno,Cno,Grade from SC where Grade between 70 and 80 --4.查询计算机系年龄在18~20岁的男学生的姓名和年龄 select Sname,Sage from Student where Sdept = '计算机系' and Sage between 18 and 20 --5.查询C001课程的最高分 select max(Grade) 最高分 from SC where Cno = 'C001' --6.查询计算机系学生的最大年龄和最小年龄 select max(Sage) 最大年龄,MIN(Sage) 最小年龄 from Student where Sdept = '计算机系' --7.统计每个系的学生人数 select Sdept,count(*) 人数 from Student group by Sdept --8.统计每门课程的选课人数和考试最高分 select Cno 课程号,count(*) 选课人数,max(Grade) 最高分 from SC group by Cno --9.统计每个学生的选课门数和考试总成绩,并按选课门数升序显示结果 select sno,count(*)选课门数,sum(Grade) 总成绩 from SC group by Sno order by count(*) --10.查询选修C002课程的学生的姓名和所在系 select Sname,Sdept from Student S join SC on S.Sno = SC.Sno where Cno = 'C002' --11.查询成绩80分以上的学生的姓名、课程号和成绩,并按成绩降序排列结果 select Sname,Cno,Grade from Student S join SC on S.Sno = SC.Sno where Grade > 80 order by Grade DESC --12.查询选课门数最多的前2位学生,列出学号和选课门数 select top 2 with ties Sno,count(*) 选课门数 from SC group by Sno order by count(*) desc --13.查询哪些课程没有学生选修,要求列出课程号和课程名 select C.Cno,Cname from Course C left join SC on C.Cno = SC.Cno where SC.Cno is null --14.查询计算机系哪些学生没有选课,列出学生姓名 select Sname 未选课的学生 from Student S left join SC on S.Sno=SC.Sno where Cno is null --15.用子查询实现如下查询 --(1)查询选修C001课程的学生的姓名和所在系 select Sname,Sdept from Student where Sno in ( select Sno from SC where Cno = 'C001') --(2)查询通信工程系成绩80分以上的学生的学号和姓名 select Sno,Sname from Student where Sno in ( select Sno from SC where Grade >80) --(3)查询计算机系考试成绩最高的学生的姓名 select Sname from Student where Sdept = '计算机系' and Sno in ( select top 1 with ties Sno from SC order by Grade desc) --(4)查询年龄最大的男学生的姓名和年龄 select Sname,Sage from Student where Ssex = '男' and Sno in( select top 1 with ties Sno from Student order by Sage desc) --(5)查询C001课程的考试成绩高于该课程平均成绩的学生的学号和成绩 select Sno,Grade from SC where Cno = 'C001' and Grade > ( select avg(Grade) from SC where Cno = 'C001') --16.创建一个新表,表名为test,其结构为(COL1,COL2,COL3),其中, --COL1:整形,允许空值 --COL2:普通编码定长字符类型,长度为10,不允许空值 --COL3:普通编码定长字符类型,长度为10,允许空值 create table test( COL1 int, COL2 char(10) not null, COL3 char(10)) insert into test values(NULL,'B1',NUll) insert into test values('1','B2','C2') insert into test values('2','B3',NUll) --17.将所有选修C001课程的学生成绩加10分 update SC set Grade = Grade +10 where Cno = 'C001' --18.将计算机系所有选修“计算机文化学”课程的学生的成绩加10分 update SC set Grade = Grade +10 where Cno in ( select Cno from Course where Cname = '计算机文化学') --19.删除成绩小于50分的学生的选课记录 delete from SC where Grade < 50 --20.删除计算机系VB考试成绩不及格学生的VB选课记录 delete from SC where Cno= 'C005' and Grade < 60 and Sno in ( select Sno from Student where Sdept = '计算机系') --21.删除没人选的课程的基本信息 delete from Course where Cno in ( select C.Cno from Course C left join SC on C.Cno=SC.Cno where SC.Cno is null) 转载请保留出处,http://www./doc/info-b84c18b52af90242a895e5de.html |
|