SQL语句练习及答案_第1页
SQL语句练习及答案_第2页
SQL语句练习及答案_第3页
SQL语句练习及答案_第4页
SQL语句练习及答案_第5页
免费预览已结束,剩余20页可下载查看

下载本文档

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

文档简介

1、selectsno,cnofromscwheregradeisnull;数据库有如下四个表格:student(sno,sname,sage,sse,x sdpt) 学生表系表( dptno,dname )course(cno,cname,gradet, tno) 课程表sc(sno,cno,score)成绩表teacher(tno,tname) 教师表要求:完成以下操作1. 查询姓 "欧阳 "且全名为三个汉字的学生的姓名。selectsnamefromstudent wheresnamelike 欧阳 ;2. 查询名字中第2个字为 "阳"字的学生的姓名和

2、学号。selectsname, snofromstudent wheresnamelike'_阳';3. 查询所有不姓刘的学生姓名。selectsname, sno, ssexfromstudentwheresnamenotlike “刘%” ;4. 查询db_design课程的课程号和学分。selectcno, ccredit fromcoursewherecnamelike'db_design'5. 查询以 "db_" 开头,且倒数第 3 个字符为 i 的课程的详细情况。select*fromcourse wherecnamelike&#

3、39;db%i_' ;6. 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。7. 查所有有成绩的学生学号和课程号。selectsno, cnofromscwheregradeisnotnull ;8. 查询计算机系年龄在20 岁以下的学生姓名。selectsnamefromstudent wheresdept='cs'andsage<20;9. 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。selectsno, gradefromsc wherecno='3' orderby

4、gradedesc;10. 查询学生总人数。selectcount(*)fromstudent ;11. 查询选修了课程的学生人数。selectcount(distinctsno)fromsc;12. 计算 1 号课程的学生平均成绩。selectavg(grade)fromsc wherecno='1;'13. 查询选修1 号课程的学生最高分数。selectmax(grade)fromsc wherecno='1;'14. 查询学生2 选修课程的总学分数。selectsum(grade)fromsc,coursewhere sno='2'and=

5、;15. 查询选修了 3 门以上课程的学生学号。selectsnofromscgroupbysno havingcount(*)>3;16. 查询每个学生及其选修课程的情况。select student.* , sc.*, course.* from student , sc , coursewhere = and =;17. 查询每个学生及其选修课程的情况包括没有选修课程的学生18. 查询选修 2 号课程且成绩在90 分以上的所有学生的学号、姓名select ,from student,scwhere = and =” 2and >90;19. 查询每个学生的学号、姓名、选修的课

6、程名及成绩。select , sname, ssex, sage, sdept, cno, gradefrom student left outjoin sco on= ;20. 查询与“刘晨”在同一个系学习的学生。selectsno, sname, sdeptfrom studentwhere sdept in(select sdept from student where sname= ”刘晨);21. 查询选修了课程名为“信息系统”的学生学号和姓名select sno, sname from student where sno in(select sno from sc where cn

7、o in22. 找出每个学生超过他选修课程平均成绩的课程号。select sno, cno from sc x where grade>=(select avg(grade) from sc y where = ;23. 将一个新学生记录(学号:8;姓名:陈冬;性别:男;所在系:is;年龄:18岁)插入到 student 表中。insert into student values ('8' , '陈冬 ' , '男', 'is', 18);24. 将学生 1 的年龄改为 22 岁。update student setsage

8、=22 where sno='1;'25. 将所有学生的年龄增加 1 岁。update student setsage=sage+1;26. 将计算机科学系全体学生的成绩置零。update sc set grade=0 where exits(selete * from student where = and sdept=” 计算机科学系 ”);27. 删除学号为的学生记录delete from student where sno= ” ;8'28. 删除所有的学生选课记录。delete from sc;29. 删除 2 号课程的所有选课记录。delete from s

9、c where cno='2';30. 删除计算机科学系所有学生的选课记录。delete from sc where sno in(selete sno from student where sdept=” 计算机科学系”);31. 建立信息系学生的视图。create view is_student asselect sno, sname, sage from student where sdept='is' ;sql 语句练习题3设教学数据库education,有三个关系:学生关系 s( sno, sname, age, sex, sdept) ;学习关系 s

10、c( sno, cno, grade) ;课程关系 c( cno, cname, cdept , tname )查询问题:1:查所有年龄在20 岁以下的学生姓名及年龄。select sname, sagefrom swhere sage<20;(not age>=20);2:查考试成绩有不及格的学生的学号select distinct snofrom scwhere grade<60;3:查所年龄在20 至 23 岁之间的学生姓名、系别及年龄。select sname, sdept, sagefrom swhere sage between 20 and 23;4:查计算机系

11、、数学系、信息系的学生姓名、性别。select sname, ssex from s where sdept in( cs,is,math );5:查既不是计算机系、数学系、又不是信息系的学生姓名、性别select sname, ssex from s where sdept not in ( cs,is,math);6:查所有姓“刘”的学生的姓名、学号和性别。select sname, sno, ssex from s where sname like'刘 ';7:查姓“上官”且全名为3 个汉字的学生姓名。select sname from s where sname lik

12、e 上官 ;8:查所有不姓“张”的学生的姓名。select sname, sno, ssex from s where sname not like 张 %;9:查db_design 课程的课程号。select cno from c where cname likedb_design;10:查缺考的学生的学号和课程号。select sno, cno from sc where grade is nul;l11:查年龄为空值的学生的学号和姓名。select sno, sname from s where sage is nul;l12:查计算机系 20 岁以下的学生的学号和姓名。select s

13、no, snamefrom swhere sdept= cs and sage<20;13:查计算机系、数学系、信息系的学生姓名、性别。select sname, ssexfrom swhere sdept= cs or sdept= is or sdept= math ;14:查询选修了 c3 课程的学生的学号和成绩,其结果按分数的降序排列。select sno, gradefrom scwhere cno= c3order by grade desc;15: 查询全体学生的情况, 查询结果按所在系升序排列, 对同一系中的学生按年龄降序排列。select *from sorder by

14、 sdep, sage desc;16:查询学生总人数。select count(*) from s;17:查询选修了课程的学生人数。select count(distinct sno) from sc18:计算选修了 c1 课程的学生平均成绩。select avg(grade) from sc where cno= c1 ;19:查询学习 c3 课程的学生最高分数。select max(grade)from scwhere cno= c3;20:查询各个课程号与相应的选课人数。select cno, count(sno)from scgroup by cno;21:查询计算机系选修了 3 门

15、以上课程的学生的学号。select snofrom scwhere sdept= csgroup by snohaving count(*)>3;22:求基本表s 中男同学的每一年龄组(超过50 人)有多少人要求查询结果按人数升序排列,人数相同按年龄降序排列。select sage, count ( sno)from swhere ssex='m'group by sagehaving count(* ) >50 order by 2, sage desc;23:查询每个学生及其选修课程的情况。select , sname, sage, ssex, sdept, c

16、no, gradefrom s, scwhere =;24:查询选修了c2 课程且成绩在90 分以上的所有学生。select , snamefrom s , scwhere =and = c2and >90;25:查询每个学生选修的课程名及其成绩。select , sname, cname,from s , sc, cwhere = and =26:统计每一年龄选修课程的学生人数。select sage, count ( distinct )from s , scwhere =group by sage;27:查询选修了 c2课程的学生姓名。select sname from s whe

17、re sno in(select sno from sc where cno= c2);28:查询与“张三”在同一个系学习的学生学号、姓名和系别。select sno, sname, sdept from where sdept=( select sdept from s where sname张三);29:查询选修课程名为“数据库”的学生学号和姓名。select sno, sname from s where sno in (select sno from sc where cno in ( select cno from c where cname= db);30:查询与“张三”在同一个系

18、学习的学生学号、姓名和系别。select sno, sname, sdept from s where sdept=( select sdept from s where sname='张三');31:查询选修课程名为“数据库”的学生学号和姓名。select sno, sname from s where sno inselect sno from sc where cno=select cno from c where cname= db );32:查询选修了c2 课程的学生姓名。1. select sname from s where sno inselect sno fr

19、om sc where cno= c2);2. select sname from s where existsselect * from sc where = and cno= c2);33:查询选修了全部课程的学生姓名。select sname from s where not exists( select * from c where not exists( select * from sc where = and =;)36:查询所学课程包含学生s3 所学课程的学生学号select distinct sno from sc as x where not exists( select *

20、 from sc as y where = s3 and not exists( select * from sc as z where = and =;)sql 语句练习题3一、简单查询1、列出全部学生的信息。select * from 学生2、列出软件专业全部学生的学号及姓名。select学号,姓名from学生where专业="软件"3、列出所有必修课的课号。select distinct 课号 from 必修课4、求1 号课成绩大于80 分的学生的学号及成绩,并按成绩由高到低列出。select 学号,成绩 from 选课 where 课号="1"a

21、nd 成绩 >80 order by 成绩 desc5、列出非软件专业学生的名单。方法一: select 姓名 from 学生 where 专业 <>"软件 "方法二:select姓名from学生where not专业="软件"方法三:select姓名from学生where专业!="软件"6、查询成绩在7080 分之间的学生选课得分情况方法一:select*from 选课where 成绩 >=70and 成绩 <=80方法二:select*from 选课where 成绩between70and80不在此范

22、围内的查询:(注意写出和以下语句等价的语句)select * from 选课 where 成绩 not between70and807、列出选修1 号课或 3 号课的全体学生的学号和成绩。方法一:select学号,成绩from选课where课号="1"or课号="3" 方法二:select学号,成绩from选课where课号in("1","3")相反条件查询:select学号,成绩from选课where课号notin("1","3")8、列出所有98级学生的学生成绩情况 se

23、lect*from 选课 where 学号 like"98%"select*from 选课 where 学号 like"98"相反条件查询:select*from选课where学号notlike"98%"9、列出成绩为空值(或不为空值)的学生的学号和课号。答案一:select学号,课号from 选课 where 成绩isnull答案二:select学号,课号from 选课 where 成绩isnotnull10、求出所有学生的总成绩。select sum喊绩)as总成绩 from 选课11、列出每个学生的平均成绩。select学号,a

24、vg娥绩)as平均成绩from 选课group by学号12、列出各科的平均成绩、最高成绩、最低成绩和选课人数。select课号,avg娥绩)as平均成绩,max(成绩)as最高分,;min(成绩)as最低分,count(学号)as 选课人数 from 选课 group by 课号二、连接查询(一)简单连接1、列出选修1号课的学生姓名及成绩。select姓名,成绩from学生,选课 where学生.学号=选课.学号and课号="1"2、列出选修1号课的学生的学号、姓名及成绩。select学生.学号,姓名,成绩from学生s,选课xwheres.学号=乂学号and课号=&qu

25、ot;1"3、求出总分大于150的学生的学号、姓名及总成绩。select学生.学号,姓名,sum(成绩)as总成绩from学生,选课;where 学生.学号 =4课.学号 groupby 选课.学号 havingsum娥绩)150(二)自连接查询1、列出那些专业相同的学生相应的姓名及专业信息。select a.姓名,b.姓名,专业from 学生a,学生bwherea.学号<>b.学号anda.专业也专 业2、求至少选修1 号课和 2 号课的学生的学号。selectx.学号 from 选课 x,选课 ywherex.学号=y.学号 andx.课号="1"

26、andy.课号="2"3、有以下表币种1代码c(2)、币种2代码c(2)、买入价n(8,4)、卖出价n(8,4)外汇汇率.dbf币种1c(4)、币种2c(4)、买入价n(8,4)、卖出价n(8,4)外汇代码.dbf外汇名称c(10)、外汇代码c(10)要求:将所有 “外汇汇率 ”表中的数据插入rate 表中并且顺序不变,由于 “外汇汇率 ”中的币种 1 和币种 2 存放的是外币名称,而rate 表中的币种 1 代码和币种 2代码应该存放外币代码,所以插入时要做相应的改动,外币名称与外向代码的对应关系存储在 “外汇代码 ”表中。selecta.外币彳t码as币种1代码,b.外

27、币代码as币种2代码,;买入价渎出价from外汇代码a,外汇汇率,外汇代码b;wherea.外币名称二外汇汇率.币种1andb.外币名称二外汇汇率.币种2intotablerate4、假定有 雇员”表(雇员号c (2),雇员姓名c (6),经理号c (2),根据 雇员关系列出上一级经理及其所领导的职员清单。(教案中的例题)select"领导",s.雇员姓名,"雇员",e.雇员姓名from雇员s,雇员ewheres.雇员号=e.经 理(三)超连接1、列出选修1 号课的学生姓名及成绩。方法一:(使用简单连接查询格式)select姓名,成绩from学生,选课w

28、here学生.学号也课.学号and课号="1"方法二:(使用内部连接格式)select姓名,成绩from学生innerjoin选课on学生.学号=选课.学号where课号="1"方法三:内部连接的 inner 短语可以省略。(与方法二等价)select姓名,成绩from学生join选课on学生.学号=选课.学号where课号="1"2、查询订货管理数据库中数据的仓库号、城市、供应商名和地址信息。方法一:使用简单连接格式。select 仓库 .仓库号,城市 ,供应商名,地址from 供应商 ,订购单,职工,仓库 ;where 供应商 .

29、供应商号=订购单.供应商号and 订购单.职工号=职工.职工号 ;and 职工 .仓库号=仓库 .仓库号方法二:使用超连接的内部连接格式。(注意连接条件的顺序)select 仓库 .仓库号,城市 ,供应商名,地址from 供应商 join 订购单 join 职工 join 仓库;on 职工 .仓库号=仓库 .仓库号 on 订购单 .职工号=职工.职工号 on 供应商 .供应商号=订购单 .供应商号3、查询没有选修任何课程的学生姓名。方法一:使用嵌套查询select 姓名 from 学生 where 学号 notin(select 学号 from 选课 )方法二:使用超连接的右连接。select

30、 姓名 from 选课 rightjoin 学生 on 选课 .学号 =学生 .学号 where 选课 .学号 <> 学生 .学号方法三:使用超连接的左连接。(注意表名顺序和方法二的不同)select姓名from学生leftjoin选课on选课.学号=学生.学号where选课.学号 <>学 生 .学号三、嵌套查询exists1、列出选修汇编语言课的学生的学号。方法一:select学号from选课where课号=(select课号from课程where课名="汇编语言")方法二:使用谓词exists。注意和方法一格式上的不同。select 学号 fro

31、m 选课 whereexist(select*from 课程 ;where 课名 ="汇编语言"and 选课 .课号 =课程.课号 )2、求软件专业所有必修课的课程信息。方法一:select*from 课程 where 课号 in;(select 课号 from 必修课 where 必修专业="软件 ")方法二:select*from 课程 whereexist(select*from 必修课 where 必修专业="软件 "and 课程 .课号 =必修课.课号 )(二)量词any、 some、 all1、求选修2 号课的学生中,成绩

32、比选修1 号课的最低成绩要高的学生的学号和成绩。方法一:select 学号 ,成绩 from 选课 where 课号 ="2"and 成绩 >(selectmin(成绩)from 选课 where 课号="1")方法二:any等价于 some,所以可将 any换成 some。select 学号 ,成绩 from 选课 where 课号 ="2"and 成绩 >any;(select 成绩 from 选课 where 课号="1")2、求选修2 号课的学生中,成绩比选修1 号课的任何学生的成绩都要高的那些

33、学生的学号和成绩。方法一:select 学号 ,成绩from 选课 where 课号 ="2"and 成绩 >(selectmax戚绩)from 选课 where 课号="1")方法二:select 学号 ,成绩from 选课 where 课号 ="2"and 成绩 >all;(select 成绩 from 选课 where 课号="1")(三)内外层互相关嵌套(外层依赖于内层的查询结果,内层依赖于外层来进一步查询)1、列出每门课程中成绩最高的选课信息。select*from 选课 awhere 成绩=

34、(selectmax(®绩)from 选课 bwherea.课号=b.课号)2、列出每个学生中成绩低于本人平均成绩的选课信息。select*from 选课 awhere 成绩 <(selectavg帧绩)from 选课 bwherea.学号二b.学号)3、列出表“订购单 ”( 其内容就是在订购单表的基础上增加一个总金额字段)中每个职工经手的具有最高总金额的订购单信息。(教案中例题)selectout.职工号,out.供应商号,out.订货单号,out.订货日期,out.总金额;from 订购单 outwhere 总金额=(selectmax(总金额)from 订购单inner1;whereout.职工号 =inner1.职工号)四、操作功能1、在课程表中插入新的元组(5,大学英语)。insertinto课程(课号,课名)values("5","大学英语")2、给学生表中男生的记录加上删除标记。deletefrom 学生 where 性别 ="男"3、 将选课表中所有课程的成绩分别提高 5 分。 将课号为“ 4的课程的成绩置为空”值。update 选课 set 成绩 =成绩 +5update 选课 set 成绩 =nullwhere 课号 ="4"五

温馨提示

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

评论

0/150

提交评论