sql语句练习题及答案_第1页
sql语句练习题及答案_第2页
sql语句练习题及答案_第3页
sql语句练习题及答案_第4页
sql语句练习题及答案_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

1、sql语句练习题及答案表。一 在数据库 school 中建立 student , sc, course学生表、课程表、选课表属于数据库 School , 其各自的数据结构如下:学生 Stude nt (Sno,S name,Ssex,Sage,Sde pt)序口, 号列名含义数据类型长度1Sno学号字符型(char)62Sn ame姓名字符型(varchar)83Ssex性别字符型(char)24Sage年龄整数(smalli nt)5sde pt系科字符型(varchar)15课程表course(Cno,C name,C pn o,Ccredit)序列名含义数据类型长口 号度1Cno课程口,

2、 号字符型(char)42cn ame课程名字符型(varchar)203Cpno先修课字符型(char)44Ccredi t学分短整数(ti nyi nt)学生选课 SC(Sno,Cno.Grade)序口, 号列名含义数据类型长度1Sno学号字符型(char)62Cno课程口, 号字符型(char)43Grade成绩小数(decimal)12,2二设定主码1 Student表的主码:sno2 Course表的主码:eno3 Se表的主码:sno,eno 1写出使用 Create Table 语句创建表student se, course 的 SQL语句2在student表中插入信息学号姓匚名

3、性别年龄系科4001赵茵男20SX4002杨华女21JSJ3删除student表中的元组4在数据库school中删除关系student5在 student表添加属性 datetimesbirthdate 类型练习Delete1删除所有JSJ系的男生 delete fromStude nt where Sdept= 'SJ' a nd Ssex='男 '2删除 数据库原理”勺课的选课纪录Up date12delete from SC where Cno in (select Cno fromCourse where Cname='数据库原理'修改0

4、001学生的系科为:JSJ把陈小明的年龄加1岁,性别改为女。 修改李文庆的1001课程的成绩为93分 把 数据库原理”课的成绩减去1分Select查询语句一单表1查询年龄在19至21岁之间的女生的学号,姓 名,年龄,按年龄从大到小排列。2查询姓名中第2个字为“明"字的学生学号、 性别。口号、3查询1001课程没有成绩的学生学号、课程号4查询JSJ、SX WL系的年龄大于25岁的学生 学号,姓名,结果按系排列5按10分制查询学生的sno,cno,10分制成绩(1-10 分为 1,11-20 分为 2,30-39 分为 3,。90-100 为 10)6查询student 表中的学生共分布

5、在那几个系 中。(distinct )7查询0001号学生1001,1002课程的成绩。二统计1查询姓名中有"明"字的学生人数。2计算JSJ'系的平均年龄及最大年龄。3查询学生中姓名为张明、赵英的人数4计算每一门课的总分、平均分,最高分、最低 分,按平均分由高到低排列计算1001,1002课程的平均分。查询平均分大于80分的学生学号及平均分 统计选修课程超过2门的学生学号 统计有10位成绩大于85分以上的课程号。统计平均分不及格的学生学号三连接1查询JSJ2查询选修 嵌套及嵌套10统计有大于两门课不及格的学生学号系的学生选修的课程号1002课程的学生的学生姓名(不用

6、2种方法)3查询数据库原理不及格的学生学号及成绩4查询选修“数据库原理”课且成绩 80以上的 学生姓名(不用嵌套及嵌套2种方法)平均5查询平均分不及格的学生的学号,姓名, 分。6查询女学生平均分高于75分的学生姓名。7查询男学生学号、姓名、课程号、成绩。 门课程也没有选修的男学生也要列出,不能遗 漏) 四嵌套、相关及其他1查询平均分不及格的学生人数2查询没有选修1002课程的学生的学生姓名3查询平均分最高的学生学号及平均分(2种方法 TOP , any , all )*4查询没有选修1001,1002课程的学生姓名。5查询1002课程第一名的学生学号(2种方法)6查询平均分前三名的学生学号7查

7、询JSJ系的学生与年龄不大于 19岁的学 生的差集可、8查询1001号课程大于90分的学生学号、姓名及平均分大于85分的学生学号、姓名9查询每门课程成绩都高于该门课程平均分的 学生学号10查询大于本系科平均年龄的学生姓名答案参考答案1 create table stude nt(snosn amessexchar(6), varchar(8), char(2), smalli nt, varchar(15),sagesde pt p rimary key(s no);create table sc(snochar(6),char(4), decimal(12,2),enogradep rima

8、ry key(s no,c no);in sert into stude ntvalues( ' 4001','赵茵','男,20,'SX') delete from stude nt drop table stude nt alter table stude nt add sbirthdate datetime1 select sno, sn ame, sagefromstude ntwhere ssex='女' and sage between 19 and 21 order by sage desc;2 select

9、 sno, ssexfromstude ntwhere sname like '明 % ' ;3 select sno, enofrom scwhere grade is n ull and cno= ' 1001'4 select sno, sn amefrom stude ntwhere sdept in (' JSJ' ,' SX' ,' WL ' ) and sage>25group by sde pt;select sno, eno, grade/10.0+1 as levelfrom sc ;se

10、lect dist inct sde pt from stude nt ;select gradefrom sccno='where sno= '0001' and (cno=' 1001' or 1002');coun t(*) from stude nt where sn ame % 明 % 'select like ' select avg(sage),max(sage) from student where sdept=' JSJ'select cn o,sum(grade),avg(grade),max(

11、grade),mi n(grade) from segroup by enoorder by avg(grade) dese ;select eno, avg(grade) from se where cnc in( 1001' ,' 1002')gro up by eno ;select se.s no ,avg(grade) from segro up by se.s nohav ing avg(grade)>80 ;select sno from se group by sno having eou nt(*)>2 ;select eno from s

12、e where grade>85 group by eno hav ing coun t(*)=10 ;select sno from se group by sno having avg(grade)<60 ;select enoselect sno from se where grade<60 group by sno hav ing coun t(*)>2 ;fromstude nt,scwherestudent.sno=sc.sno and sdept=' JSJ'a:select sn ame fromstude nt,scwherestude

13、 nt.s no=sc.s no and cno=' 1002' b:select sn ame from stude nt where sno in (select sno from sc where cno= ' 1002') select sn o,grade from sc,coursewhere o=o and cname='数据库 原理'and grade <60 a:select sn ame from stude nt ,sc,courseandwherestude nt.s no=sc.s noo=o and grade&

14、gt;80 and cname='数据库原理b:select sn ame from stude nt where sno in(select sno from sc where grade>80 and eno in (select eno from course where cn ame='数据库原理')select sn o,s name,avg(grade) from sc,stude ntwhere stude nt.s no=sc.s no group by stude nt.s nohav ing avg(grade)<60a:select s

15、n ame from stude nt where ssex='女' and sno in( select sno from sc group by snohav ing avg(grade)>75)b:select sn ame from sc,stude nt where stude nt.s no=sc.s no and ssex=女'gro up bystude nt.s nohav ingavg(grade)>75select stude nt.s no,s name,c no,grade from stude nt left jo in sc o

16、n stude nt.s no=sc.s noand ssex='男'select coun t(*) from stude nt where sno in( select sno from sc group by sno hav ingavg(grade)v60)select sname from student where sno not in (select sno from sc where cno=' 1002')stude nt0001aaX0002bb0003ccXSc00011001000110020002100100031002Select s

17、name from student where not exists(select* from sc where cno= ' 1002'and sc.s no=stude nt.s no)a:select top 1 sno,avg(grade) from sc group by sno order by avg(grade) descb:select sno, avg(grade) from sc gro up by sno having avg(grade)=(select top 1 avg(grade) from scgro up by sno order by av

18、g(grade) desc) c:select sno, avg(grade) from sc group by snohaving avg(grade)>=all ( select avg(grade) from sc gro up by sno) select sn ame from stude nt where not exists( select * from course where eno in( "1001',' 1002' ) andnot exists(select * from sc where sno =stude nt.s no and eno=course.c no) a:select top 1 sno from sc cno=' 1002' order by grade descb:select sno from sc where cno=' 1002' andgrade >=all (and grade>90select sno,sname from student (select grade

温馨提示

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

评论

0/150

提交评论