sql练习题+答案.docx
《sql练习题+答案.docx》由会员分享,可在线阅读,更多相关《sql练习题+答案.docx(12页珍藏版)》请在冰豆网上搜索。
sql练习题+答案
sql练习题+答案
(一)新建以下几个表
student(学生表):
sno
sname
sex
dept
birth
age
其中约束如下:
(1)学号不能存在相同的
(2)名字为非空
(3)性别的值只能是’男’或’女’
(4)系包括这几个:
信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系
(5)出生日期为日期格式
(6)年龄为数值型,且在0~100之间
(1)
(2)
(3)
selectdistinctdeptfromstudent
(4)查询选修了课程的学生学号。
selectsnofromcswherecnoisnotnull
(5)查询所有年龄在20岁以下的学生姓名及其年龄。
selectsname,agefromstudentwhereage<20
(6)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
selectsname,dept,agefromstudentwhereagebetween20and23
(7)查询年龄不在20~23岁之间的学生姓名、系别和年龄。
selectsname,dept,agefromstudentwhereage<20orage>23
(8)查询信息系、数学系和计算机科学系生的姓名和性别。
selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'
(9)查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。
selectsname,sexfromstudentwheredept!
='信息系'anddept!
='数学系'anddept!
='计算机科学系'
(10)查询所有姓刘学生的姓名、学号和性别。
selectsname,sno,sexfromstudentwheresnamelike('刘%')
(11)查询学号为2009011的学生的详细情况。
(具体的学号值根据表中数据确定)
select*fromstudentwheresno=5
(12)查询姓“欧阳”且全名为三个汉字的学生姓名
selectsnamefromstudentwheresnamelike('欧阳_')
(13)查询名字中第2个字为“晨”字的学生的姓名和学号
selectsname,snofromstudentwheresnamelike('_晨')
(14)查询所有不姓刘的学生姓名。
selectsname,snofromstudentwheresnamenotlike('刘%')
(15)查询sql课程的课程号和学分。
selectcnofromcoursewherecname='sql'
(16)查询以"DB_"开头,且倒数第3个字符为i的课程的详细情况。
select*fromcoursewherecnamelike('DB[_]%i__')
(17)查询缺少成绩的学生的学号和相应的课程号。
selectsno,cnofromcswherecjisnull
(18)查所有有成绩的学生学号和课程号。
selectsno,cnofromcswherecjisnotnull
(19)查询计算机系年龄在20岁以下的学生姓名。
selectsnamefromstudentwhereage<20anddept='计算机科学系'
(20)查询信息系、数学系和计算机科学系学生的姓名和性别。
(使用多个条件表达式)
selectsname,sexfromstudentwheredept='信息系'ordept='数学系'ordept='计算机科学系'
(21)查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
(使用多个条件表达式)
selectsname,dept,agefromstudentwhereagebetween20and23
(22)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
selectsno,cjfromcswherecno=3orderbycjdesc
(23)查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
select*fromstudentorderbydeptasc,agedesc
(24)查询学生总人数。
selectcount(*)fromstudent
(25)查询选修了课程的学生人数。
selectcount(sno)fromcswherecnoisnotnull
(26)计算1号课程的学生平均成绩。
selectavg(cj)fromcswherecno=1
(27)查询选修1号课程的学生最高分数。
selectmax(cj)fromcswherecno=1
(28)求各个课程号及相应的选课人数。
selecto,count(cs.sno)fromcourseleftjoincs
ono=ogroupbyo
(29)查询选修了3门以上课程的学生学号。
selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>3
(30)查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。
selectsno,count(cno)as'课程数'fromcswherecj>90
groupbysnohavingcount(cno)>=3
(31)查询学生2006011选修课程的总学分。
selectsum(course)fromcourse,cswhereo=cs.snoandcs.sno=2006011
(32)查询每个学生选修课程的总学分。
selectsno,sum(cj)fromcs,course
whereo=o
groupbysno
union
selectsno,0fromstudent
wheresnonotin(selectsnofromcs)
(33)查询每个学生及其选修课程的情况。
selectcs.sno,course.*fromcs,coursewhereo=o
(34)查询选修2号课程且成绩在90分以上的所有学生的学号、姓名
selectsno,snamefromstudentwheresno=(selectsnofromcswherecno=2andcj>90)
(35)查询每个学生的学号、姓名、选修的课程名及成绩。
selectstudent.sno,sname,course.course,cs.cj
fromstudent,course,cswherestudent.sno=cs.snoando=o
(36)查询与“刘晨”在同一个系学习的学生(分别用嵌套查询和连接查询)
----嵌套查询
select*fromstudentwheredeptin
(selectdeptfromstudentwheresname='刘晨')
----连接查询
selectstu1.*fromstudentasstu1,studentasstu2
wherestu1.dept=stu2.deptandstu2.sname='刘晨'
----exists查询
select*fromstudents1whereexists(
select*fromstudents2wheres1.dept=s2.deptand
s2.sname='刘晨'
)
(37)查询选修了课程名为“信息系统”的学生学号和姓名
selectsno,snamefromstudentwheresnoin
(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统'))
(38)查询其他系中比信息系任意一个(其中某一个)学生年龄小的学生姓名和年龄
selectsname,agefromstudentwhereage(selectagefromstudentwheredept='信息系')
(39)查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。
分别用ALL谓词和集函数
----用ALL
selectsname,agefromstudentwhereage(selectagefromstudentwheredept='信息系')
----聚合函数
selectsname,agefromstudentwhereage<
(selectmin(age)fromstudentwheredept='信息系')
(40)查询所有选修了1号课程的学生姓名。
(分别用嵌套查询和连查询)
----嵌套查询
selectsnamefromstudentwheresnoin
(selectsnofromcswherecno=1)
----连接查询
selectsnamefromstudent,cs
wherestudent.sno=cs.snoando=1
(41)查询没有选修1号课程的学生姓名。
selectsnamefromstudentwheresnoin
(selectsnofromcswherecno!
=1)
(42)查询选修了全部课程的学生姓名。
selectsnamefromstudentwherenotexists
(select*fromcoursewherenotexists
(select*fromcswhere
cs.sno=student.snoand
o=o))
(43)查询至少选修了学生95002选修的全部课程的学生号码。
selectdistinctsnofromscscxwherenotexists
(select*fromcsscywherescy.sno='95002'andnotexists
(select*fromscsczwherescz.sno=scx.snoando=o))
(44)查询计算机科学系的学生及年龄不大于19岁的学生的信息。
select*fromstudentwheredept='计算机科学系'orage<19
(45)查询选修了课程1或者选修了课程2的学生的信息。
selectstudent.*fromstudent,cswherestudent.sno=cs.snoand(o=1oro=2)
(46)查询计算机科学系中年龄不大于19岁的学生的信息。
select*fromstudentwhereage<=19anddept='计算机科学系'
(47)查询既选修了课程1又选修了课程2的学生的信息。
select*fromstudentwheresnoin(
selectsnofromcswherecno='003'andsnoin(
selectsnofromcswherecno='004'
))
----用exists查询
select*fromstudentwhereexists(
select*fromcswherestudent.sno=cs.snoand
cno='003'andsnoin(
selectsnofromcswherecno='004'
))
(48)查询计算机科学系的学生与年龄不大于19岁的学生的差集。
select*fromstudentwheredept='计算机科学系'andage>19
(49)通过查询求学号为1学生的总分和平均分。
selectsum(cj)as'总分',avg(cj)'平均分'fromcswheresno=1
(50)求出每个系的学生数量
selectdept,count(sno)as'学生个数'fromstudentgroupbydept
(51)查询平均成绩大于85的学生学号及平均成绩。
selectsno,avg(cj)fromcsgroupbysnohavingavg(cj)>85
(52)要求查寻学生的所有信息,并且查询的信息按照年龄由高到低排序,如果年龄相等,则按照学号从低到高排序
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子句选取满足条件的分组。