sql习题及答案Word文件下载.docx
《sql习题及答案Word文件下载.docx》由会员分享,可在线阅读,更多相关《sql习题及答案Word文件下载.docx(11页珍藏版)》请在冰豆网上搜索。
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列。
2、查询教师所有的单位即不重复的Depart列。
3、查询Student表的所有记录。
4、查询Score表中成绩在60到80之间的所有记录。
5、查询Score表中成绩为85,86或88的记录。
6、查询Student表中“95031”班或性别为“女”的同学记录。
7、以Class降序查询Student表的所有记录。
8、以Cno升序、Degree降序查询Score表的所有记录。
9、查询“95031”班的学生人数。
10、查询Score表中的最高分的学生学号和课程号。
11、查询‘3-105’号课程的平均分。
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
13、查询最低分大于70,最高分小于90的Sno列。
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
16、查询所有学生的Sname、Cname和Degree列。
17、查询“95033”班所选课程的平均分。
18、假设使用如下命令建立了一个grade表:
createtablegrade(lownumeric(3,0),uppnumeric(3),rankchar
(1));
insertintogradevalues(90,100,'
A'
);
insertintogradevalues(80,89,'
B'
insertintogradevalues(70,79,'
C'
insertintogradevalues(60,69,'
D'
insertintogradevalues(0,59,'
E'
现查询所有同学的Sno、Cno和rank列。
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
23、查询“张旭“教师任课的学生成绩。
24、查询选修某课程的同学人数多于5人的教师姓名。
25、查询95033班和95031班全体学生的记录。
26、查询存在有85分以上成绩的课程Cno.
27、查询出“计算机系“教师所教课程的成绩表。
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
31、查询所有教师和同学的name、sex和birthday.
32、查询所有“女”教师和“女”同学的name、sex和birthday.
33、查询成绩比该课程平均成绩低的同学的成绩表。
34、查询所有任课教师的Tname和Depart.
35查询所有未讲课的教师的Tname和Depart.
36、查询至少有2名男生的班号。
37、查询Student表中不姓“王”的同学记录。
38、查询Student表中每个学生的姓名和年龄。
39、查询Student表中最大和最小的Sbirthday日期值。
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
41、查询“男”教师及其所上的课程。
42、查询最高分同学的Sno、Cno和Degree列。
43、查询和“李军”同性别的所有同学的Sname.
44、查询和“李军”同性别并同班的同学Sname.
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
下面是参考答案:
SQL语句练习题参考答案
1.selectsname,ssex,classfromstudent;
2.
selectdistinct(depart)fromteacher;
or
selectdistinctdepartfromteacher;
3.select*fromstudent;
4.
select*fromscorewheredegreebetween60and80;
or
select*fromscorewheredegree>
=60anddegree<
=80;
5.
select*fromscorewheredegreein(85,86,88);
select*fromscorewheredegree=85ordegree=86ordegree=88;
6.select*fromstudentwhereclass=95031orssex='
;
7.select*fromstudentorderbyclassdesc;
8.
select*fromscoreorderbycnoasc,degreedesc;
select*fromscoreorderbycno,degreedesc;
9.
selectcount(*)fromstudentwhereclass=95031;
selectcount(sno)fromstudentwhereclass=95031;
10.selectSnoas'
学号'
cnoas'
课程号'
degreeas'
最高分'
fromscorewheredegree=(selectmax(degree)fromscore);
11.selectavg(degree)fromscorewherecno='
12.
selectcno,avg(degree)fromscorewherecnolike'
3%'
groupbycnohavingcount(sno)>
5;
groupbycnohavingcount(*)>
13.selectsnofromscoregroupbysnohavingmin(degree)>
70andmax(degree)<
90;
14.
selectstudent.sname,o,score.degreefromstudent,scorewherestudent.sno=score.sno;
selectsname,cno,degreefromstudent,scorewherestudent.sno=score.sno;
selectx.sname,o,y.degreefromstudentx,scoreywherex.sno=y.sno;
15.
Selectscore.sno,ame,score.degreefromscore,coursewhereo=o;
selectsno,cname,degreefromscore,coursewhereo=o;
selectx.sno,ame,x.degreefromscorex,courseywhereo=o;
16.
selectstudent.sname,ame,score.degreefromstudent,course,scorewherestudent.sno=score.snoando=o;
selectsname,cname,degreefromstudent,course,scorewherestudent.sno=score.snoando=o;
selectx.sname,ame,z.degreefromstudentx,coursey,scorezwherex.sno=z.snoando=o;
17.
selectcno,avg(degree)fromscore,studentwherestudent.sno=score.snoandclass=95033groupbycno;
selecto,avg(y.degree)fromstudentx,scoreywherex.sno=y.snoandx.class=95033groupbyo;
18.selectsno,cno,rankfromscore,gradewheredegreebetweenlowandupp[orderbyrank];
[]表示可有可无
19.
select*fromscorewherecno='
anddegree>
(selectdegreefromscorewheresno='
andcno='
selectx.*fromscorex,scoreywhereo='
andx.degree>
y.degreeandy.sno='
ando='
20.
分析:
1.成绩非本科最高select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno)
选学一门以上的学生成绩:
selectsnofromscoregroupbysnohavingcount(*)>
1;
2.查询成绩非本科最高并且选1门以上的学生的成绩:
select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno)groupbysnohavingcount(*)>
select*from(select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno))asaagroupbysnohavingcount(*)>
=2;
通用答案:
selectsnofrom
(
select*fromscore
wheredegreenotin
(selectmax(degree)fromscoregroupbycno))asaa
groupbysnohavingcount(*)>
21.
(selectdegreefromscorewheresno=109andcno='
selectx.*fromscorex,scoreywherex.degree>
y.degreeandy.sno=109ando='
22.
selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday)=(selectyear(sbirthday)fromstudentwheresno=108);
23.
select*fromscorewherecnoin(selectcnofromcoursewheretno=(selecttnofromteacherwheretname='
));
selectcno,sno,degreefromscorewherecno=(selectofromcoursex,teacherywherex.tno=y.tnoandy.tname='
24.
selecttnamefromteacherwheretnoin(selectx.tnofromcoursex,scoreywhereo=oandoin(selectcnofromscoregroupbycnohavingcount(*)>
5));
selecttnamefromteacherwheretnoin(selecttnofromcoursewherecnoin(selectcnofromscoregroupbycnohavingcount(*)>
selecttnamefromteacherwheretnoin(selectx.tnofromcoursex,scoreywhereo=ogroupbyx.tnohavingcount(x.tno)>
5);
25.
select*fromstudentwhereclassin('
select*fromstudentwhereclass=95033orclass=95031;
26.selectdistinctcnofromscorewheredegreein(selectdegreefromscorewheredegree>
85);
27.
select*fromscorewherecnoin(selectcnofromcoursewheretnoin(selecttnofromteacherwheredepart='
select*fromscorewherecnoin(selectofromcoursex,teacherywherey.tno=x.tnoandy.depart='
28.selecttname,proffromteacherwheredepart='
andprofnotin(selectproffromteacherwheredepart='
29.
(selectmin(degree)fromscorewherecno='
)orderbydegreedesc;
anddegree>
any(selectdegreefromscorewherecno='
30.
(selectmax(degree)fromscorewherecno='
all(selectdegreefromscorewherecno='
31.
selectsnameasname,ssexassex,sbirthdayasbirthdayfromstudent
union
selecttname,tsex,tbirthdayfromteacher;
32.
selectsnameasname,ssexassex,sbirthdayasbirthdayfromstudentwheressex='
selecttname,tsex,tbirthdayfromteacherwheretsex='
33.select*fromscoreawheredegree<
(selectavg(degree)fromscorebwhereo=o);
34.selecttname,departfromteacherwheretnoin(selecttnofromcourse);
35.selecttname,departfromteacherwheretnonotin(selecttnofromcourse);
36.selectclassfromstudentwheressex='
groupbyc