sql练习题+答案.docx

上传人:b****2 文档编号:1802647 上传时间:2023-05-01 格式:DOCX 页数:12 大小:18.85KB
下载 相关 举报
sql练习题+答案.docx_第1页
第1页 / 共12页
sql练习题+答案.docx_第2页
第2页 / 共12页
sql练习题+答案.docx_第3页
第3页 / 共12页
sql练习题+答案.docx_第4页
第4页 / 共12页
sql练习题+答案.docx_第5页
第5页 / 共12页
sql练习题+答案.docx_第6页
第6页 / 共12页
sql练习题+答案.docx_第7页
第7页 / 共12页
sql练习题+答案.docx_第8页
第8页 / 共12页
sql练习题+答案.docx_第9页
第9页 / 共12页
sql练习题+答案.docx_第10页
第10页 / 共12页
sql练习题+答案.docx_第11页
第11页 / 共12页
sql练习题+答案.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

sql练习题+答案.docx

《sql练习题+答案.docx》由会员分享,可在线阅读,更多相关《sql练习题+答案.docx(12页珍藏版)》请在冰点文库上搜索。

sql练习题+答案.docx

sql练习题+答案

(一)新建以下几个表

student(学生表):

sno

sname

sex

dept

birth

age

其中约束如下:

(1)学号不能存在相同的

(2)名字为非空

(3)性别的值只能是’男’或’女’

(4)系包括这几个:

信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系

(5)出生日期为日期格式

(6)年龄为数值型,且在0~100之间

createtablestudent(

snosmallintconstraintaprimarykey,----设置学生学号为student的主键

snamevarchar(10)notnull,

sexvarchar

(2)constraintbcheck(sexin('男','女')),----检查约束——性别的值只能是’男’或’女’

deptvarchar(20)constraintccheck(deptin('信息系','计算机科学系','数学系','管理系','中文系','外语系','法学系')),----检查约束——系包括这几个:

信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系

birthdatetime,

agesmallintconstraintdcheck(agebetween0and100)----检查约束——年龄为数值型,且在~100之间

 

cs(成绩表):

sno

cno

cj

其中约束如下:

(1)sno和cno分别参照student和course表中的sno,cno的字段

(2)cj(成绩)只能在0~100之间,可以不输入值

createtablecs(

snosmallintnotnullreferencesstudent(sno),----定义成外键

cnosmallintnotnullreferencescourse(cno),----定义成外键

cjsmallintconstraintecheck(cjbetween0and100),----检查约束——cj(成绩)只能在~100之间,可以不输入值

constraintfprimarykey(sno,cno)----定义学生学号和课程号为sc表的主键

course(课程表)

cno

cname

其约束如下:

(1)课程号(cno)不能有重复的

(2)课程名(cname)非空

createtablecourse(

cnosmallintnotnullconstraintgprimarykey,----设置课程号为course的主键

cnamevarchar(20)notnull

(三)针对学生课程数据库查询

(1)查询全体学生的学号与姓名。

Selectsno,snamefromstudent

(2)查询全体学生的姓名、学号、所在系,并用别名显示出结果。

Selectsnameas'姓名',snoas'学号',deptas'所在地'fromstudent

(3)查询全体学生的详细记录。

select*fromstudent

(4)查全体学生的姓名及其出生年份。

selectsname,birthfromstudent

(5)查询学校中有哪些系。

selectdistinctdeptfromstudent

(6)查询选修了课程的学生学号。

selectsnofromcswherecnoisnotnull

(7)查询所有年龄在20岁以下的学生姓名及其年龄。

selectsname,agefromstudentwhereage<20

(8)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

selectsname,dept,agefromstudentwhereagebetween20and23

(9)查询年龄不在20~23岁之间的学生姓名、系别和年龄。

selectsname,dept,agefromstudentwhereage<20orage>23

(10)查询信息系、数学系和计算机科学系生的姓名和性别。

selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'

(11)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

selectsname,sexfromstudentwheredept!

='信息系'anddept!

='数学系'anddept!

='计算机科学系'

(12)查询所有姓刘学生的姓名、学号和性别。

selectsname,sno,sexfromstudentwheresnamelike('刘%')

(13)查询学号为2009011的学生的详细情况。

(具体的学号值根据表中数据确定)

select*fromstudentwheresno=5

(14)查询姓“欧阳”且全名为三个汉字的学生姓名

selectsnamefromstudentwheresnamelike('欧阳_')

(15)查询名字中第2个字为“晨”字的学生的姓名和学号

selectsname,snofromstudentwheresnamelike('_晨')

(16)查询所有不姓刘的学生姓名。

selectsname,snofromstudentwheresnamenotlike('刘%')

(17)查询sql课程的课程号和学分。

selectcnofromcoursewherecname='sql'

(18)查询以"DB_"开头,且倒数第3个字符为i的课程的详细情况。

select*fromcoursewherecnamelike('DB[_]%i__')

(19)查询缺少成绩的学生的学号和相应的课程号。

selectsno,cnofromcswherecjisnull

(20)查所有有成绩的学生学号和课程号。

selectsno,cnofromcswherecjisnotnull

(21)查询计算机系年龄在20岁以下的学生姓名。

selectsnamefromstudentwhereage<20anddept='计算机科学系'

(22)查询信息系、数学系和计算机科学系学生的姓名和性别。

(使用多个条件表达式)

selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'

(23)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。

(使用多个条件表达式)

selectsname,dept,agefromstudentwhereagebetween20and23

(24)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。

selectsno,cjfromcswherecno=3orderbycjdesc

(25)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select*fromstudentorderbydeptasc,agedesc

(26)查询学生总人数。

selectcount(*)fromstudent

(27)查询选修了课程的学生人数。

selectcount(sno)fromcswherecnoisnotnull

(28)计算1号课程的学生平均成绩。

selectavg(cj)fromcswherecno=1

(29)查询选修1号课程的学生最高分数。

selectmax(cj)fromcswherecno=1

(30)求各个课程号及相应的选课人数。

select,countfromcourseleftjoincs

on=groupby

(31)查询选修了3门以上课程的学生学号。

selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>3

(32)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。

selectsno,count(cno)as'课程数'fromcswherecj>90

groupbysnohavingcount(cno)>=3

(33)查询学生2006011选修课程的总学分。

selectsum(course)fromcourse,cswhere=and=2006011

(34)查询每个学生选修课程的总学分。

selectsno,sum(cj)fromcs,course

where=

groupbysno

union

selectsno,0fromstudent

wheresnonotin(selectsnofromcs)

(35)查询每个学生及其选修课程的情况。

select,course.*fromcs,coursewhere=

(36)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名

selectsno,snamefromstudentwheresno=(selectsnofromcswherecno=2andcj>90)

(37)查询每个学生的学号、姓名、选修的课程名及成绩。

select,sname,,

fromstudent,course,cswhere=and=

(38)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)

----嵌套查询

select*fromstudentwheredeptin

(selectdeptfromstudentwheresname='刘晨')

----连接查询

selectstu1.*fromstudentasstu1,studentasstu2

where=and='刘晨'

----exists查询

select*fromstudents1whereexists(

select*fromstudents2where=and

='刘晨'

(39)查询选修了课程名为“信息系统”的学生学号和姓名

selectsno,snamefromstudentwheresnoin

(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统'))

(40)查询其他系中比信息系任意一个(其中某一个)学生年龄小的学生姓名和年龄

selectsname,agefromstudentwhereage

(selectagefromstudentwheredept='信息系')

(41)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。

分别用ALL谓词和集函数

----用ALL

selectsname,agefromstudentwhereage

(selectagefromstudentwheredept='信息系')

----聚合函数

selectsname,agefromstudentwhereage<

(selectmin(age)fromstudentwheredept='信息系')

(42)查询所有选修了1号课程的学生姓名。

(分别用嵌套查询和连查询)

----嵌套查询

selectsnamefromstudentwheresnoin

(selectsnofromcswherecno=1)

----连接查询

selectsnamefromstudent,cs

where=and=1

(43)查询没有选修1号课程的学生姓名。

selectsnamefromstudentwheresnoin

(selectsnofromcswherecno!

=1)

(44)查询选修了全部课程的学生姓名。

selectsnamefromstudentwherenotexists

(select*fromcoursewherenotexists

(select*fromcswhere

=and

=)

(45)查询至少选修了学生95002选修的全部课程的学生号码。

selectdistinctsnofromscscxwherenotexists

(select*fromcsscywhere='95002'andnotexists

(select*fromscsczwhere=and=)

(46)查询计算机科学系的学生及年龄不大于19岁的学生的信息。

select*fromstudentwheredept='计算机科学系'orage<19

(47)查询选修了课程1或者选修了课程2的学生的信息。

selectstudent.*fromstudent,cswhere=and=1or=2)

(48)查询计算机科学系中年龄不大于19岁的学生的信息。

select*fromstudentwhereage<=19anddept='计算机科学系'

(49)查询既选修了课程1又选修了课程2的学生的信息。

select*fromstudentwheresnoin(

selectsnofromcswherecno='003'andsnoin(

selectsnofromcswherecno='004'

))

----用exists查询

select*fromstudentwhereexists(

select*fromcswhere=and

cno='003'andsnoin(

selectsnofromcswherecno='004'

))

(50)查询计算机科学系的学生与年龄不大于19岁的学生的差集。

select*fromstudentwheredept='计算机科学系'andage>19

(51)通过查询求学号为1学生的总分和平均分。

selectsum(cj)as'总分',avg(cj)'平均分'fromcswheresno=1

(52)求出每个系的学生数量

selectdept,count(sno)as'学生个数'fromstudentgroupbydept

(53)查询平均成绩大于85的学生学号及平均成绩。

selectsno,avg(cj)fromcsgroupbysnohavingavg(cj)>85

(54)要求查寻学生的所有信息,并且查询的信息按照年龄由高到低排序,如果年龄相等,则按照学号从低到高排序

select*fromstudentorderbyagedesc,snoasc

 

1.在SELECT语句中DISTINCT、ORDERBY、GROUPBY和HAVING子句的功能各是什么?

答各子句的功能如下。

DISTINCT:

查询唯一结果。

ORDERBY:

使查询结果有序显示。

GROUPBY:

对查询结果进行分组。

HAVING:

筛选分组结果。

 

2.在一个SELECT语句中,当WHERE子句、GROUPBY子句和HAVING子句同时出现在一个查询中时,SQL的执行顺序如何?

答其执行顺序如下:

(1)执行WHERE子句,从表中选取行。

(2)由GROUPBY对选取的行进行分组。

(3)执行聚合函数。

(4)执行HAVING子句选取满足条件的分组。

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2