最完整的sql练习+答案Word文档下载推荐.docx
《最完整的sql练习+答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《最完整的sql练习+答案Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
tsexvarchar
(2)notnull,
--教工出生日期
tbirthdaydatetime,
--职称
profvarchar(6),
--所在部门
departvarchar(10)
createtablecourse
--课程号
cnovarchar(5)notnullprimarykey,
--课程名称
cnamevarchar(10)notnull,
tnovarchar(3)referencesteacher(tno)
createtablescore
snovarchar(3)notnullreferencesstudent(sno),
cnovarchar(5)notnullreferencescourse(cno),
--成绩
degreedecimal(4,1)
insertintostudent
values('
108'
'
曾华'
男'
1977-09-01'
95033'
105'
匡明'
1975-10-02'
95031'
107'
王丽'
女'
1976-01-23'
101'
军'
1976-02-20'
109'
王芳'
1975-02-10'
103'
陆君'
1974-06-03'
insertintoteacher
804'
诚'
1958-12-02'
副教授'
计算机系'
856'
旭'
1969-03-12'
讲师'
电子工程系'
825'
王萍'
1972-05-05'
助教'
831'
冰'
1958-08-14'
insertintocourse
3-105'
计算机导论'
3-245'
操作系统'
6-166'
数字电路'
9-888'
高等数学'
insertintoscore
86'
75'
68'
92'
88'
76'
64'
91'
78'
85'
79'
81'
select*fromstudent
select*fromteacher
select*fromcourse
select*fromscore
--1、查询Student表中的所有记录的Sname、Ssex和Class列。
selectsname,ssex,classfromstudent
--2、查询教师所有的单位即不重复的Depart列。
selectdistinctdepartfromteacher
--3、查询Student表的所有记录。
--4、查询Score表中成绩在60到80之间的所有记录。
select*fromscorewheredegreebetween60and80
--5、查询Score表中成绩为85,86或88的记录。
select*fromscorewheredegree='
ordegree='
--6、查询Student表中“95031”班或性别为“女”的同学记录。
select*fromstudentwhereclass='
orssex='
--7、以Class降序查询Student表的所有记录。
select*fromstudentorderbyclassdesc
--8、以Cno升序、Degree降序查询Score表的所有记录。
select*fromscoreorderbyo,degreedesc
--9、查询“95031”班的学生人数。
selectcount(sno)fromstudentwhereclass='
--10、查询Score表中的最高分的学生学号和课程号。
selectsno,cno,degreefromscorewheredegreein(selectmax(degree)fromscore)
--11、查询‘3-105’号课程的平均分。
selectavg(degree)fromscorewhereo='
--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
selectavg(degree)fromscorewhereolike'
3%'
andoin(selectofromscoregroupbyohavingcount(cno)>
5)
--13、查询最低分大于70,最高分小于90的Sno列。
selectsnofromscorewheredegreebetween70and90
--14、查询所有学生的Sname、Cno和Degree列。
selectsname,cno,degreefromscore,studentwherestudent.sno=score.sno
--15、查询所有学生的Sno、Cname和Degree列。
selectame,student.sno,degreefromscore,student,coursewherestudent.sno=score.snoando=o
--16、查询所有学生的Sname、Cname和Degree列
selectsname,cname,degreefromscore,student,coursewherestudent.sno=score.snoando=o
--17、查询“95033”班所选课程的平均分。
select平均分=avg(degree)fromcourse,student,scorewhereclass='
ando=oandstudent.sno=score.sno
--18、假设使用如下命令建立了一个grade表:
--createtablegrade(lowint,uppint,rankvarchar
(1))
--insertintogradevalues(90,100,'
A'
--insertintogradevalues(80,89,'
B'
--insertintogradevalues(70,79,'
C'
--insertintogradevalues(60,69,'
D'
--insertintogradevalues(0,59,'
E'
--现查询所有同学的Sno、Cno和rank列。
selectstudent.sno,cno,rankfromscore,student,gradewherestudent.sno=score.snoanddegreebetweenlowandupp
--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
//无关子查询
selectscore.sno,sname,ssex,sbirthday,class,o,cname,degreefromscore,student,coursewherestudent.sno=score.snoando=oando='
anddegree>
(selectdegreefromscorewheresno='
ando='
--20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
selectsno,cno,degreefromscorewheredegreenotin(selectmax(degree)fromscoregroupbyo)orderbysno
--21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select*fromscorewheredegree>
ando='
--22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday)=
(selectyear(sbirthday)fromstudentwheresno='
--23、查询“旭“教师任课的学生成绩。
selectsno,o,degreefromscore,course,teacherwhereo=oandcourse.tno=teacher.tnoandtname='
--24、查询选修某课程的同学人数多于5人的教师。
selecttnamefromteacher,coursewhereteacher.tno=course.tnoandoin(selectofromscoregroupbyohavingcount(sno)>
--25、查询95033班和95031班全体学生的记录。
unionselect*fromstudentwhereclass='
--26、查询存在有85分以上成绩的课程Cno.
selectdistinctofromscorewheredegree>
85
--27、查询出“计算机系“教师所教课程的成绩表。
selectscore.sno,o,degreefromteacher,course,scorewhereteacher.tno=course.tnoando=oanddepart='
orderbysno
--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
selecttname,proffromteacherwheredepart='
andprofnotin(selectproffromteacherwheredepart='
)
union
--29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
selecto,sno,degreefromscorewhereo='
anddegree>
any(selectdegreefromscorewhereo='
)orderbydegreedesc
--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
all(selectdegreefromscorewhereo='
--31、查询所有教师和同学的name、sex和birthday.
selectname=tname,sex=tsex,birthday=tbirthdayfromteacherunionselectname=sname,sex=ssex,birthday=sbirthdayfromstudent
--32、查询所有“女”教师和“女”同学的name、sex和birthday.
selectname=tname,sex=tsex,birthday=tbirthdayfromteacherwheretsex='
unionselectname=sname,sex=ssex,birthday=sbirthdayfromstudentwheressex='
--33、查询成绩比该课程平均成绩低的同学的成绩表。
select*fromscorewheredegree<
any(selectavg(degree)fromscoregroupbyo)
--34、查询所有任课教师的Tname和Depart.
selecttname,departfromteacher
--35查询所有未讲课的教师的Tname和Depart.
selecttname,departfromteacher,coursewhereteacher.tno=course.tnoandoin(selectofromcoursewherenotexists(select*fromscorewhereo=o))
--36、查询至少有2名男生的班号。
selectclassfromstudentwheressex='
groupbyclasshavingcount(ssex)>
=2
--37、查询Student表中不姓“王”的同学记录。
select*fromstudentwheresnamenotlike'
王%'
--38、查询Student表中每个学生的和年龄。
selectsname,sage=(2011-year(sbirthday))fromstudent
--39、查询Student表中最大和最小的Sbirthday日期值。
selectmax(sbirthday)fromstudentunionselectmin(sbirthday)fromstudent
--40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
selectsno,sname,ssex,class,sage=(2011-year(sbirthday))fromstudentorderbyclassdesc,(2011-sbirthday)desc
--41、查询“男”教师及其所上的课程。
selecttname,tsex,cname,departfromteacher,coursewherecourse.tno=teacher.tnoandtsex='
--42、查询最高分同学的Sno、Cno和Degree列。
selectstudent.sno,cno,degreefromstudent,scorewherestudent.sno=score.snoanddegreein(selectmax(degree)fromscore)
--43、查询和“军”同性别的所有同学的Sname.
selects1.snamefromstudents1,students2wheres1.ssex=s2.ssexands2.sname='
军
--44、查询和“军”同性别并同班的同学Sname.
ands1.class=s2.class
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
selectscore.sno,o,degreefromcourse,student,scorewherestudent.sno=score.snoando=oandssex='
andame='
--46、查询score表中分数最高的学生的信息。
//多层嵌套
selectstudent.sno,sname,ssex,sbirthday,classfromstudent,scorewherestudent.sno=score.snoanddegreein(selectmax(degree)fromscore)
--47、查询score表中的平均分在80分以上的学生信息。
//相关查询。
无关查询
selectsno,sname,ssex,sbirthday,classfromstudentwheresnoin(selectsnofromscoregroupbysnohavingavg(degree)>
80)