SQL练习题答案_第1页
SQL练习题答案_第2页
SQL练习题答案_第3页
SQL练习题答案_第4页
SQL练习题答案_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、sql 练习题答案一、补充作业一、设有三个关系:s(sno, sname, age, sex,sdept)sc(sno, cno, grade)c(cno, cname, teacher)试用关系代数表达式表示下列查询:1、查询学号为s3学生所学课程的课程名与任课教师名。 2、查询至少选修liu老师所教课程中一门课的女生姓名。3、查询wang同学不学的课程的课程号。4、查询至少选修两门课程的学生学号。5、查询选修课程中包含liu老师所教全部课程的学生学号。补充作业二、三个关系同上,试用sql语言表示下列查询:1、 查询门门课程都及格的学生的学号方法1:提示:根据学号分组,就得到每个学生所有的课

2、程成绩,在某个学生这一组成绩里,如果他所有的课程成绩都大于60分则输出该组学生的学号select sno frome sc group by sno having(min(grade)=60)2、查询既有课程大于90分又有课程不及格的学生的学号自身连接:select sno from sc where grade 90 and sno in (select sno from sc where grade60)3、查询平均分不及格的课程号和平均成绩select cno , avg(grade) from sc group by cno having avg(grade)=604、找出至少选修了2

3、号学生选修过的全部课程的学生号提示:不存在这样的课程y,学生2选修了y,而学生x没有选。select distinct sno from sc as scx where not exists (select * from sc as scy where scy.sno =2and not exists (select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno) 5、求各门课程去掉一个最高分和最低分后的平均分第一步,求所有成绩的平均分(去掉一个最高分和最低分)select avg(grade) from sc where gr

4、ade not in (select top 1 grade from sc order by grade) and grade not in (select top 1 grade from sc order by grade desc) 第二步,将所有成绩按各门课程的课程号cno分组select cno avg(grade) from sc where grade not in (select top 1 grade from sc order by grade) and grade not in (select top 1 grade from sc order by grade des

5、c) group by cno 1、查询7号课程没有考试成绩的学生学号。 select sno from sc where cno=7 and grade is null 2、查询7号课程成绩在90分以上或60分以下的学生学号。select sno from sc where cno=7 and grade not between 60 and 90 3、查询课程名以“数据”两个字开头的所有课程的课程号和课程名。select cno,cname from c where cname like 数据% 4、查询每个学生所有课程的平均成绩,输出学生学号和平均成绩。 select sno,avg(g

6、rade) from sc group by sno 5、查询每门课程的选修人数,输出课程号和选修人数。 select cno,count(*) from sc group by cno 6、查询选修7号课程的学生的学号、姓名、性别。 select s.sno,sname,ssex from s,sc where s.sno=sc.sno and cno=7 或: select sno,sname,ssex from s where sno in ( select sno from sc where cno=7 ) 7、查询选修7号课程的学生的平均年龄。 select avg(sage) fr

7、om s,sc where s.sno=sc.sno and cno=7 或: select avg(sage) from s where sno in ( select sno from sc where cno=7 ) 8、查询有30名以上学生选修的课程号。 select cno from sc group by cno having count(*)30 9、查询至今没有考试不及格的学生学号。 select distinct sno from sc where sno not in ( select sno from sc where grade=6010、查询所有考试成绩的平均分相同的

8、学生学号分组二、 1、找出选修课程号为c2的学生学号与成绩。select sno,grade from sc where cno=c2 2、找出选修课程号为c4的学生学号与姓名。 select s.sno,sname from s,sc where s.sno=sc.sno and cno=c4 注意本题也可以用嵌套做思考本题改为“找出选修课程号为c4的学生学号、姓名与成绩”后还能用嵌套做吗? 3、找出选修课程名为 maths 的学生学号与姓名。 select s.sno,sname from s,sc,c where s.sno=sc.sno and o=o and cname=maths注

9、意本题也可以用嵌套做 4、找出选修课程号为c2或c4的学生学号。 select distinct sno from sc where cno in (c2,c4) 或: select distinct sno from sc where cno=c2 or cno=c4 5、找出选修课程号为c2和c4的学生学号。 select sno from sc where cno=c2 and sno in ( select sno from sc where cno=c4 ) 注意本题也可以用连接做思考:select distinct sno from sc where cno=c2 and cno=

10、c4正确吗? 6、找出不学c2课程的学生姓名和年龄。 select sname,sage from s where sno not in ( select sno from sc where cno=c2 ) 或: select sname,sage from s where not exists ( select * from sc where sno=s.sno and cno=c2 ) 7、找出选修了数据库课程的所有学生姓名。(同3) select sname from s,sc,c where s.sno=sc.sno and o=o and cname=数据库 8、找出数据库课程不及

11、格的女生姓名。 连接:select sname from s,sc,c where s.sno=sc.sno and o=o and cname=数据库 and grade60 and ssex=女 嵌套:select sname from s where ssex=女 and sno in ( select sno from sc where grade=30 )注意本题也可以用连接做12、找出选修了不少于3门课程的学生姓名。 select sname from s where sno in ( select sno from sc group by sno having count(*)=

12、3 )注意本题也可以用连接做13、找出各门课程的成绩均不低于90分的学生姓名。 select sname from s,sc where s.sno=sc.sno group by s.sno,sname having min(grade)=90方法二:select sname from s where sno not in ( select sno from sc where grade ( select avg(grade) from sc,c where o=o and cname=数据库 )15、找出各个系科男女学生的平均年龄和人数。 select sdept,ssex,avg(sag

13、e),count(*) from s group by sdept,ssex16、找出计算机系(jsj)课程平均分最高的学生学号和姓名。 select s.sno,sname from s,sc where s.sno=sc.sno and sdept=jsj group by s.sno,sname having avg(grade) =all ( select avg(grade) from s,sc where s.sno=sc.sno and sdept=jsj group by s.sno )17、(补充)查询每门课程的及格率。 本题可以分三步做: 第1步:得到每门课的选修人数 cr

14、eate view v_all(cno,cnt) as select cno, count(*) from sc group by cno 第2步:得到每门课及格人数 create view v_pass(cno,cnt_pass) as select cno, count(*) from sc where grade=60 group by cno 第3步:每门课的及格人数/每门课选修人数 select v_o, cnt_pass*100/cnt from v_all, v_pass where v_o = v_o18、查询平均分不及格的学生的学号,姓名,平均分。 select sc.sno

15、,sname,avg(grade) from student,sc where student.sno=sc.sno group by sc.sno,sname having avg(grade)60思考本题也可以用嵌套做吗?19、查询平均分不及格的学生人数。 select count(*) from student where sno in ( select sno from sc group by sno having avg(grade)60 ) 下面是一个典型的错误select count(*) from sc group by sno having avg(grade)5 order

16、 by kno asc 5、查询各办公室男性和女性业务员的平均工资。 select ono,ysex,avg(salary) from ywy group by ono,ysex 6、查询2002年5月曾经在王海亮业务员手中购买过商品的客户号、 客户姓名和联系电话。 select kno,kname,phone from kh where kno in ( select kno from fp where fdate between 2002.5.1 and 2002.5.31 and yno in ( select yno from ywy where yname=王海亮 ) ) 注意本题也

17、可以用连接做 7、查询所有工资比1538号业务员高的业务员的编号、姓名和工资。 select yno,yname,salary from ywy where salary ( select salary from ywy where yno=1538 ) 8、查询所有与1538号业务员在同一个办公室的其他业务员的编号和姓名。 select yno,yname from ywy where yno!=1538 and ono in ( select ono from ywy where yno=1538 ) 9、查询销售总金额最高的业务员的编号。 select yno from fp group

18、 by yno having sum(fmoney) =all ( select sum(fmoney) from fp group by yno )10、查询所有业务员的编号、姓名、工资以及工资比他高的其他业务员的平均工资。 利用自连接 select y1.yno,y1.yname,y1.salary,avg(y2.salary) from ywy y1, ywy y2 where y1.salary y2.salary group by y1.yno 四、 1、找出每个班级的班级代码、学生人数、平均成绩。 select bjdm,count(*),avg(cj) from sc group

19、 by bjdm 2、找出每个学生的班级代码、学生姓名、考试科目数、总成绩。 select bjdm,xsxm,count(*),sum(cj) from sc group by bjdm,bnxh,xsxm 3、输出一张表格,每位学生对应一条记录,包括字段: 班级代码、学生姓名、语文成绩、数学成绩、外语成绩。 select sc1.bjdm,sc1.xsxm,sc1.cj,sc2.cj,sc3.cj from sc sc1, sc sc2, sc sc3 where sc1.bjdm=sc2.bjdm and sc1.bnxh=sc2.bnxh and sc2.bjdm=sc3.bjdm a

20、nd sc2.bnxh=sc3.bnxh and sc1.km=语文 and sc2.km=数学 and sc3.km=外语 4、输出一张表格,有成绩低于60分的每位学生对应一条记录,包括字段: 班级代码、学生姓名、最低成绩。 select bjdm,xsxm,min(cj) from sc where cj60 group by bjdm,bnxh,xsxm 或: select bjdm,xsxm,min(cj) from sc group by bjdm,bnxh,xsxm having min(cj)60 5、输出一张表格,有成绩低于60分的每位学生对应一条记录,包括字段: 班级代码、学

21、生姓名、最高成绩、平均成绩。 select bjdm,xsxm,max(cj) from sc group by bjdm,bnxh,xsxm having min(cj)60 请思考下列做法是否正确: select bjdm,xsxm,max(cj),avg(cj) from sc where cj=60 7、输出一张表格,每一位学生对应一条记录,包括字段: 班级代码、学生姓名、去掉一个最低分后的平均成绩。 select bjdm,xsxm,(sum(cj)-min(cj)/(count(*)-1) from sc group by bjdm,bnxh,xsxm 8、输出一张表格,每门科目对

22、应一条记录,包括字段: 科目、去掉一个最低分后的平均成绩。 select km,(sum(cj)-min(cj)/(count(*)-1) from sc group by km实验指导中“八 sql查询语句” 的答案 1、查询年龄在19至21岁之间的女生的学号,姓名,年龄,按年龄从大到小排列。 select sno,sname,sage from student where sage between 19 and 21 and ssex=女 order by sage desc 2、查询姓名中有“明”字的学生人数。 select count(*) from student where sna

23、me like %明% 3、查询1001课程没有成绩的学生的学号。 select sno from sc where cno=1001 and grade is null 4、查询jsj、sx、wl系的学生学号,姓名,结果按系及学号排列。 select sno,sname,sdept from student where sdept in ( jsj, sx, wl ) order by sdept,sno 5、计算每一门课的总分、平均分,最高分、最低分。 select cno,sum(grade),avg(grade),max(grade),min(grade) from sc group

24、by cno 6、查询平均分大于90分的男学生学号及平均分。 连接: select sc.sno,avg(grade) from student,sc where student.sno=sc.sno and ssex=男 group by sc.sno having avg(grade)90 嵌套: select sno,avg(grade) from sc where sno in ( select sno from student where ssex=男 ) group by sno having avg(grade)90 7、查询选修课程超过2门的学生姓名。 select sname

25、 from student,sc where student.sno=sc.sno group by sc.sno,sname having count(*)2 本题也可以用嵌套做 8、查询 jsj 系的学生选修的课程号。 select distinct cno from student,sc where student.sno=sc.sno and sdept=jsj 本题也可以用嵌套做 9、查询选修1002课程的学生的学生姓名(用连接和嵌套2种方法) 连接:select sname from student,sc where student.sno=sc.sno and cno=1002

26、嵌套:select sname from student where sno in ( select sno from sc where cno=1002 )10、查询学生姓名以及他选修课程的课程号及成绩。 select sname,cno,grade from student,sc where student.sno=sc.sno 思考本题也可以用嵌套做吗? 11、查询选修“数据库原理”课且成绩 80 以上的学生姓名(用连接和嵌套2种方法) 连接:select sname from student,sc,course where student.sno=sc.sno and o=o and

27、cname=数据库原理 and grade80 嵌套:select sname from student where sno in ( select sno from sc where grade80 and cno in ( select cno from course where cname=数据库原理 ) )14、查询没有选修1002课程的学生的学生姓名。 select sname from student where sno not in ( select sno from sc where cno=1002) 或: select sname from student where no

28、t exists ( select * from sc where cno=1002 and sno=student.sno) 思考本题也可以用一般的连接做吗?15、查询平均分最高的学生学号及平均分。 select sno,avg(grade) from sc group by sno having avg(grade) =all ( select avg(grade) from sc group by sno )16、查询每门课程成绩都高于该门课程平均分的学生学号。 可以先计算每门课程平均分 create view c_avg(cno,avg_grade) as select cno,avg

29、(grade) from sc group by cno 再查询 select distinct sno from sc where sno not in ( select sno from sc,c_avg where o=c_o and gradeavg_grade ) = select distinct sno from sc sc1 where sc1.sno not in ( select sc2.sno from sc sc2 where sc2.grade = ( select avg(sc3.grade) from sc sc3 where sc3.cno=sc2.cno )

30、) 或: select distinct sno from sc sc1 where not exists ( select * from sc sc2 where sc2.sno=sc1.sno and sc2.grade =( select avg(sc3.grade) from sc sc3 where sc3.cno=sc2.cno) ) (3)检索至少选修liu老师所授课程中一门课程的女学生姓名。 select sname from s where sex=f and s# in (select s# from sc where c# in (select c# from c whe

31、re teacher=liu) notice:有多种写法,比如联接查询写法: select sname from s,sc,c where sex=f and sc.s#=s.s# and sc.c#=c.c# and teacher=liu 但上一种写法更好一些。 (4)检索wang同学不学的课程的课程号。 select c# from c where c# not in (select c# from sc where s# in (select s# from s where sname=wang) (5)检索至少选修两门课程的学生学号。 select distinct x.sno fr

32、omsc x,sc y where x.sno=y.sno and x.cnoy.cno notice:对表sc进行自连接,x,y是sc的两个别名。 (6)检索全部学生都选修的课程的课程号与课程名。 select c#,cname from c where not exists (select * from s where s# not in (select * from sc where sc.c#=c.c#) 要从语义上分解:(1)选择课程的课程号与课程名,不存在不选这门课的同学。 其中,“不选这门课的同学”可以表示为: select * from s where s# not in (s

33、elect * from sc where sc.c#=c.c#) 或者 select * from s where not exists (select * from sc where s.s#=c.s# and sc.c#=c.c# ) (7)检索选修课程包含liu老师所授课的学生学号。 select distinct s# from sc where c# in (select c# from c where teacher=liu) 3.3设有两个基本表r(a,b,c)和s(d,e,f),试用sql查询语句表达下列关系代数表达式: (1)a(r)(2)b=17(r) (3)rs(4)a

34、,f(c=d(rs) (1)select a from r (2)select * from r where b=17 (3)select a,b,c,d,e,f from r,s (4)select a,f from r,s where r.c=s.d 3.43.4设有两个基本表r(a,b,c)和s(a,b,c)试用sql查询语句表达下列关系代数表达式: (1)rs(2)rs(3)rs(4)a,b(r)b,c(s) (1)select a,b,c from r union select a,b,c from s (2)select a,b,c from r intersect select

35、a,b,c from s (3)select a,b,c from r where not exists (select a,b,c from s where r.a=s.a and r.b=s.b and r.c=s.c) (4)select r.a,r.b,s.c from r,s where r.b=s.b 3.5试叙述sql语言的关系代数特点和元组演算特点。 (p61-62) 3.6试用sql查询语句表达下列对教学数据库中三个基本表s、sc、c的查询:(1)统计有学生选修的课程门数。 select count(distinct c#) from sc (2)求选修c4课程的学生的平均年

36、龄。 select avg(age) from s where s# in (select s# from sc where c#=c4) 或者, select avg(age) from s,sc where s.s#=sc.s# and c#=004 (3)求liu老师所授课程的每门课程的学生平均成绩。 select cname,avg(grade) from sc ,c where sc.c#=c.c# and teacher=liu group by c# (4)统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数, 查询结果按人数降序排列,若人数相同,按课程

37、号升序排列。 select distinct c#,count(s#) from sc group by c# having count(s#)10 order by 2 desc, c# asc (5)检索学号比wang同学大,而年龄比他小的学生姓名。 select x.sname from s as x, s as y where y.sname=wang and x.s#y.s# and x.age(select avg(age) from s as y where y.sex=女) (9)求年龄大于所有女同学年龄的男学生姓名和年龄。 select sname,age from s as

38、 x where x.sex=男 and x.ageall (select age from s as y where y.sex=女) 除法运算3.7试用sql更新语句表达对教学数据库中三个基本表s、sc、c的各个更新操作:(1)往基本表s中插入一个学生元组(s9,wu,18)。 insert into s(s#,sname,age) values(59,wu,18) (2)在基本表s中检索每一门课程成绩都大于等于80分的学生学号、姓名和性别, 并把检索到的值送往另一个已存在的基本表student(s,sanme,sex)。 insert into student(s#,sname,sex)

39、 select s#,sname,sex from s where not exists (select * from sc where grade80 and s.s#=sc.s#) (3)在基本表sc中删除尚无成绩的选课元组。 delete from sc where grade is null (4)把wang同学的学习选课和成绩全部删去。 delete from sc where s# in (select s# from s where sname=wang) (5)把选修maths课不及格的成绩全改为空值。 update sc set grade=null where grade6

40、0 and c# in (select c# from c where cname=maths) (6)把低于总平均成绩的女同学成绩提高5%。 update sc set grade=grade*1.05 where grade(select avg(grade) from sc) and s# in (select s# from s where sex=f) (7)在基本表sc中修改c4课程的成绩,若成绩小于等于75分时提高5%, 若成绩大于75分时提高4%(用两个update语句实现)。 update sc set grade=grade*1.05 where c#=c4 and gra

41、de75 3.8在第1章例1.4中提到“仓库管理”关系模型有五个关系模式: 零件part(p,pname,color,weight) 项目project(j,jname,date) 供应商supplier(s,sname,saddr) 供应pp(j,p,totoal) 采购ps(p,s,quantity) (1)试用sqlddl语句定义上述五个基本表,并说明主键和外键。 create table part (p# char(4) not null,pname char(12) not null, color char(10),weight real, primary key(p#) creat

42、e table project (j# char(4) not null,jname char(12) not null, date date, primary key(j#) create table suplier (s# char(4) not null,sname char(12),saddr varchar(20), primary key(s#) create table p_p (j# char(4),p# char(4),total integer, primary key(j#,p#), foreign key(j#) reference project(j#), foreign key(p#) reference part(p#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论