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

下载本文档

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

文档简介

1、(一)新建以下几个表student(学生表):snosnamesexdeptbirthage其中约束如下:(1)学号不能存在相同的(2)名字为非空(3)性别的值只能是男或女(4)系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系(5)出生日期为日期格式(6)年龄为数值型,且在0100之间createtablestudent(snosmallintconstraintaprimarykey,-设置学生学号为student的主键snamevarchar(10)notnull,sexvarchar(2)constraintbcheck(sexin('男',&#

2、39;女'),一检查约束一性别的值只能是男或女deptvarchar(20)constraintccheck(deptin('信息系,计算机科学系,数学系,管理系,中文系',外语系,法学系),-检查约束一源包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系birthdatetime,agesmallintconstraintdcheck(agebetween0and100)-检查约束一年龄为数值型,且在100之间)cs(成绩表):snocnocj其中约束如下:(1) sno和cno分别参照student和course表中的sno,cno的字段(2)

3、 cj(成绩)只能在0100之间,可以不输入值createtablecs(snosmallintnotnullreferencesstudent(sno),-定义成外键cnosmallintnotnullreferencescourse(cno),-定义成外键cjsmallintconstraintecheck(cjbetween0and100),-检查约束一-cj(成绩)只能在100之间,可以不输入值constraintfprimarykey(sno,cno)定义学生学号和课程号为sc表的主键)course(课程表)cnocname其约束如下:(1)课程号(cno)不能有重复的(2)课程名(

4、cname非空createtablecourse(cnosmallintnotnullconstraintgprimarykey,-设置课程号为course的主键cnamevarchar(20)notnull)(三)针对学生课程数据库查询(1) 查询全体学生的学号与姓名。Selectsno,snamefromstudent(2) 查询全体学生的姓名、学号、所在系,并用别名显示出结果。Selectsnameas'姓名',snoas'学号',deptas所在地'fromstudent(3) 查询全体学生的详细记录。select*fromstudent(4)

5、查全体学生的姓名及其出生年份。selectsname,birthfromstudent(5) 查询学校中有哪些系select distinctdept from student(6) 查询选修了课程的学生学号。selectsnofromcswherecnoisnotnull(7) 查询所有年龄在20岁以下的学生姓名及其年龄。selectsname,agefromstudentwhereage<20(8) 查询年龄在2023岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。selectsname,dept,agefromstudentwhereagebetween20and23(9)

6、查询年龄不在202野之间的学生姓名、系别和年龄。selectsname,dept,agefromstudentwhereage<20orage>23(10)查询信息系、数学系和计算机科学系生的姓名和性别。selectsname,sexfromstudentwheredept=信息系ordept='数学系'ordept='计算机科学系(11)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。selectsname,sexfromstudentwheredept!='信息系anddept!=数学系'anddept!=计算机科学系(1

7、2)查询所有姓刘学生的姓名、学号和性别。selectsname,sno,sexfromstudentwheresnamelike('刘')(13)查询学号为2009011的学生的详细情况。(具体的学号值根据表中数据确定)select*fromstudentwheresno=5(14)查询姓“欧阳”且全名为三个汉字的学生姓名selectsnamefromstudentwheresnamelike('欧阳)(15)查询名字中第2个字为“晨”字的学生的姓名和学号selectsname,snofromstudentwheresnamelike('_晨')(16)

8、查询所有不姓刘的学生姓名。selectsname,snofromstudentwheresnamenotlike('刘')(17)查询sql课程的课程号和学分。selectcnofromcoursewherecname='sql'(18)查询以“DB_"开头,且倒数第3个字符为i的课程的详细情况。select*fromcoursewherecnamelike('DBi_')(19)查询缺少成绩的学生的学号和相应的课程号。selectsno,cnofromcswherecjisnull(20)查所有有成绩的学生学号和课程号。selects

9、no,cnofromcswherecjisnotnull(21)查询计算机系年龄在20岁以下的学生姓名。selectsnamefromstudentwhereage<20anddept='计算机科学系(22)查询信息系、数学系和计算机科学系学生的姓名和性别。(使用多个条件表达式)selectsname,sexfromstudentwheredept=信息系ordept='数学系'ordept='计算机科学系(23)查询年龄在2023岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。(使用多个条件表达式)selectsname,dept,agefroms

10、tudentwhereagebetween20and23(24)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。selectsno,cjfromcswherecno=3orderbycjdesc(25)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。select*fromstudentorderbydeptasc,agedesc(26)查询学生总人数。selectcount(*)fromstudent(27)查询选修了课程的学生人数。selectcount(sno)fromcswherecnoisnotnull(28)计算1号课程的学生平均成绩。

11、selectavg(cj)fromcswherecno=1(29)查询选修1号课程的学生最高分数。selectmax(cj)fromcswherecno=1(30)求各个课程号及相应的选课人数。o,count(cs.sno)o=o(31)查询选修了3门以上课程的学生学号。selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>3(32)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。selectsno,count(cno)as'课程数'fro

12、mcswherecj>90groupbysnohavingcount(cno)>=3(33)查询学生2006011选修课程的总学分。selectsum(course)fromcourse,cswhereo=cs.snoandcs.sno=2006011(34)查询每个学生选修课程的总学分。selectsno,sum(cj)fromcs,courseo=ogroupbysnounionselectsno,0fromstudentwheresnonotin(selectsnofromcs)(35)查询每个学生及其选修课程的情况。selectcs.sno,course.*fromcs,c

13、oursewhereo=o(36)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名selectsno,snamefromstudentwheresno=(selectsnofromcswherecno=2andcj>90)(37)查询每个学生的学号、姓名、选修的课程名及成绩。selectstudent.sno,sname,course.course,cs.cjfromstudent,course,cswherestudent.sno=o=o(38)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)-嵌套查询select*fromstudentwheredeptin(s

14、electdeptfromstudentwhere sname ='刘晨select stui* from studentas stui , student as连接查询stu2where stul.dept =stu2 . deptand stu2 . sname =刘晨'-exists查询selectfrom student siwhere exists(select*fromstudents2wheresi.dept=s2.deptands2.sname='刘晨'(39)查询选修了课程名为“信息系统”的学生学号和姓名selectsno,snamefroms

15、tudentwheresnoin(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统')(40)查询其他系中比信息系任意一个(其中某一个)学生年龄小的学生姓名和年龄selectsname,agefromstudentwhereage<any(selectagefromstudentwheredept='信息系(41)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。分别用ALL胃词和集函数用ALLselectsname,agefromstudentwhereage<all(selec

16、tagefromstudentwheredept='信息系')聚合函数selectsname,agefromstudentwhereage<(selectmin(age)fromstudentwheredept='信息系)查询所有选修了 1号课程的学生姓名(42)- 嵌套查询 select sname(select sno-一连接查询select sname where student(分别用嵌套查询和连查询)from studentfrom cs wherefrom student.sno =cs . snowhere sno incno =1),csand c

17、s . cno = 1(43)查询没有选修1号课程的学生姓名。selectsnamefromstudentwheresnoin(selectsnofromcswherecno!=1)(44)查询选修了全部课程的学生姓名selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromcswherecs.sno=student.snoando=o)(45)查询至少选修了学生95002选修的全部课程的学生号码select distinctsno from sc scxwhere notexists(sel

18、ect* from cs scyand not exists(select* from sc sczwhere scy . sno ='95002where scz . sno =scx . snoand scz . cno =scy . cno )(46)查询计算机科学系的学生及年龄不大于19岁的学生的信息select * from studentwhere dept ='计算机科学系'orage<19(47)查询选修了课程1或者选修了课程2的学生的信息。selectstudent.*fromstudent,cswherestudent.sno=cs.snoan

19、d(o=1oro=2)(48)查询计算机科学系中年龄不大于19岁的学生的信息。select*fromstudentwhereage<=19anddept=计算机科学系(49)查询既选修了课程1又选修了课程2的学生的信息。select*fromstudentwheresnoin(selectsnofromcswherecno='003'andsnoin(selectsnofromcswherecno='004')-用exists查询select*fromstudentwhereexists(select*fromcswherestudent.sno=cs.snoandcno='003'andsnoin(selectsnofromcswherecno='004')(50)查询计算机科学系的学生与年龄不大于19岁的学生的差集。select*fromstud

温馨提示

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

评论

0/150

提交评论