sql练习题答案.docx

上传人:b****7 文档编号:8975651 上传时间:2023-02-02 格式:DOCX 页数:9 大小:16.89KB
下载 相关 举报
sql练习题答案.docx_第1页
第1页 / 共9页
sql练习题答案.docx_第2页
第2页 / 共9页
sql练习题答案.docx_第3页
第3页 / 共9页
sql练习题答案.docx_第4页
第4页 / 共9页
sql练习题答案.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

sql练习题答案.docx

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

sql练习题答案.docx

sql练习题答案

ACCESS中SQL专项训练题

(二)

(一)数据源情况:

以下各题操作表结构如下:

Student(学生表)

字段名

字段类型

说明

sno

文本型

学号,不能存在相同的

sname

文本型

姓名,非空

sex

文本型

性别,只能“男”或“女”

dept

文本型

系别,包括这几个:

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

birth

日期/时间型

出生日期

age

数值型

年龄,在0~100之间

cs(成绩表):

字段名

字段类型

说明

Sno

文本型

学号

Cno

文本型

课程号

Cj

数值型

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

course(课程表)

字段名

字段类型

说明

cno

文本型

课程号,不能有重复的

Cname

文本型

课程名,非空

Cval

数值型

学分

 

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

(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)求各个课程号及相应的选课人数。

selecto,count(cs.sno)fromcourseleftjoincsono=ogroupbyo

(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,cswhereo=cs.snoandcs.sno=2006011

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

selectsno,sum(cj)fromcs,course

whereo=o

groupbysno

union

selectsno,0fromstudent

wheresnonotin(selectsnofromcs)

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

selectcs.sno,course.*fromcs,coursewhereo=o

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

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

 

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

selectstudent.sno,sname,course.course,cs.cj

fromstudent,course,cswherestudent.sno=cs.snoando=o

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

----嵌套查询

select*fromstudentwheredeptin

(selectdeptfromstudentwheresname='刘晨')

----连接查询

selectstu1.*fromstudentasstu1,studentasstu2

wherestu1.dept=stu2.deptandstu2.sname='刘晨'

----exists查询

select*fromstudents1whereexists(

select*fromstudents2wheres1.dept=s2.deptand

s2.sname='刘晨')

(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

wherestudent.sno=cs.snoando=1

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

selectsnamefromstudentwheresnoin

(selectsnofromcswherecno!

=1)

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

selectsnamefromstudentwherenotexists

(select*fromcoursewherenotexists

(select*fromcswhere

cs.sno=student.snoand

o=o))

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

selectdistinctsnofromscscxwherenotexists

(select*fromcsscywherescy.sno='95002'andnotexists

(select*fromscsczwherescz.sno=scx.snoando=o))

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

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

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

selectstudent.*fromstudent,cswherestudent.sno=cs.snoand(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.snoand

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

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1