sql习题及答案.docx
- 文档编号:13939163
- 上传时间:2023-06-19
- 格式:DOCX
- 页数:10
- 大小:17.39KB
sql习题及答案.docx
《sql习题及答案.docx》由会员分享,可在线阅读,更多相关《sql习题及答案.docx(10页珍藏版)》请在冰点文库上搜索。
sql习题及答案
题
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);
or
select*fromscorewheredegree=85ordegree=86ordegree=88;
6、select*fromstudentwhereclass=95031orssex='女';
7、select*fromstudentorderbyclassdesc;
8、
select*fromscoreorderbycnoasc,degreedesc;
or
select*fromscoreorderbycno,degreedesc;
9、
selectcount(*)fromstudentwhereclass=95031;
or
selectcount(sno)fromstudentwhereclass=95031;
10、selectSnoas'学号',cnoas'课程号',degreeas'最高分'fromscorewheredegree=(selectmax(degree)fromscore);
11、selectavg(degree)fromscorewherecno='3-105';
12、
selectcno,avg(degree)fromscorewherecnolike'3%'groupbycnohavingcount(sno)>5;
or
selectcno,avg(degree)fromscorewherecnolike'3%'groupbycnohavingcount(*)>5;
13、selectsnofromscoregroupbysnohavingmin(degree)>70andmax(degree)<90;
14、
selectstudent、sname,scoreo,score、degreefromstudent,scorewherestudent、sno=score、sno;
or
selectsname,cno,degreefromstudent,scorewherestudent、sno=score、sno;
or
selectx、sname,yo,y、degreefromstudentx,scoreywherex、sno=y、sno;
15、
Selectscore、sno,courseame,score、degreefromscore,coursewherescoreo=courseo;
or
selectsno,cname,degreefromscore,coursewherescoreo=courseo;
or
selectx、sno,yame,x、degreefromscorex,courseywherexo=yo;
16、
selectstudent、sname,courseame,score、degreefromstudent,course,scorewherestudent、sno=score、snoandcourseo=scoreo;
or
selectsname,cname,degreefromstudent,course,scorewherestudent、sno=score、snoandcourseo=scoreo;
or
selectx、sname,yame,z、degreefromstudentx,coursey,scorezwherex、sno=z、snoandyo=zo;
17、
selectcno,avg(degree)fromscore,studentwherestudent、sno=score、snoandclass=95033groupbycno;
or
selectyo,avg(y、degree)fromstudentx,scoreywherex、sno=y、snoandx、class=95033groupbyyo;
18、selectsno,cno,rankfromscore,gradewheredegreebetweenlowandupp[orderbyrank];[]表示可有可无
19、
select*fromscorewherecno='3-105'anddegree>(selectdegreefromscorewheresno='109'andcno='3-105');
or
selectx、*fromscorex,scoreywherexo='3-105'andx、degree>y、degreeandy、sno='109'andyo='3-105';
20、
分析:
1、成绩非本科最高select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno)
选学一门以上得学生成绩:
selectsnofromscoregroupbysnohavingcount(*)>1;
2、查询成绩非本科最高并且选1门以上得学生得成绩:
select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno)groupbysnohavingcount(*)>1;
or
select*from(select*fromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno))asaagroupbysnohavingcount(*)>=2;
通用答案:
selectsnofrom
( select*fromscore
wheredegreenotin
(selectmax(degree)fromscoregroupbycno))asaa
groupbysnohavingcount(*)>=2;
21、
select*fromscorewheredegree>(selectdegreefromscorewheresno=109andcno='3-105');
or
selectx、*fromscorex,scoreywherex、degree>y、degreeandy、sno=109andyo='3-105';
22、
selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday)=(selectyear(sbirthday)fromstudentwheresno=108);
23、
select*fromscorewherecnoin(selectcnofromcoursewheretno=(selecttnofromteacherwheretname='张旭'));
or
selectcno,sno,degreefromscorewherecno=(selectxofromcoursex,teacherywherex、tno=y、tnoandy、tname='张旭');
24、
selecttnamefromteacherwheretnoin(selectx、tnofromcoursex,scoreywherexo=yoandyoin(selectcnofromscoregroupbycnohavingcount(*)>5));
or
selecttnamefromteacherwheretnoin(selecttnofromcoursewherecnoin(selectcnofromscoregroupbycnohavingcount(*)>5));
or
selecttnamefromteacherwheretnoin(selectx、tnofromcoursex,scoreywherexo=yogroupbyx、tnohavingcount(x、tno)>5);
25、
select*fromstudentwhereclassin('95033','95031');
or
select*fromstudentwhereclass=95033orclass=95031;
26、selectdistinctcnofromscorewheredegreein(selectdegreefromscorewheredegree>85);
27、
select*fromscorewherecnoin(selectcnofromcoursewheretnoin(selecttnofromteacherwheredepart='计算机系'));
or
select*fromscorewherecnoin(selectxofromcoursex,teacherywherey、tno=x、tnoandy、depart='计算机系');
28、selecttname,proffromteacherwheredepart='计算机系'andprofnotin(selectproffromteacherwheredepart='电子工程系');
29、
select*fromscorewherecno='3-105'anddegree>(selectmin(degree)fromscorewherecno='3-245')orderbydegreedesc;
or
select*fromscorewherecno='3-105'anddegree>any(selectdegreefromscorewherecno='3-245')orderbydegreedesc;
30、
select*fromscorewherecno='3-105'anddegree>(selectmax(degree)fromscorewherecno='3-245');
or
select*fromscorewherecno='3-105'anddegree>all(selectdegreefromscorewherecno='3-245');
31、
selectsnameasname,ssexassex,sbirthdayasbirthdayfromstudent
union
selecttname,tsex,tbirthdayfromteacher;
32、
selectsnameasname,ssexassex,sbirthdayasbirthdayfromstudentwheressex='女'
union
selecttname,tsex,tbirthdayfromteacherwheretsex='女';
33、select*fromscoreawheredegree<(selectavg(degree)fromscorebwhereao=bo);
34、selecttname,departfromteacherwheretnoin(selecttnofromcourse);
35、selecttname,departfromteacherwheretnonotin(selecttnofromcourse);
36、selectclassfromstudentwheressex='男'groupbyclasshavingcount(sno)>=2;
37、select*fromstudentwheresnamenotlike'王%';
38、selectsnameas'姓名',2010-year(sbirthday)as'年龄'fromstudent;
39、
selectsbirthdayfromstudentwheresbirthdayin(selectmin(sbirthday)fromstudent)
union
selectsbirthdayfromstudentwheresbirthdayin(selectmax(sbirthday)fromstudent);
or
selectsbirthdayfromstudentwheresbirthday=(selectmin(sbirthday)fromstudent)orsbirthday=(selectmax(sbirthday)fromstudent);
40、select*fromstudentorderbyclassdesc,sbirthday;
41、
selectcname,tnamefromcourse,teacherwherecourse、tno=teacher、tnoandtsex='男';
or
selectcourseame,teacher、tnamefromcourse,teacherwherecourse、tno=teacher、tnoandtsex='男';
or
selectcname,tnamefromcoursex,teacherywherex、tno=y、tnoandy、tsex='男';
or
selectx、tname,yamefromteacherx,courseywherex、tno=y、tnoandx、tsex='男';
42、select*fromscorewheredegreein(selectmax(degree)fromscore);
43、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军');
44、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军')andclass=(selectclassfromstudentwheresname='李军');
45、select*fromscorewheresnoin(selectsnofromstudentwheressex='男')andcnoin(selectcnofromcoursewherecname='计算机导论');
注意:
20题得前两个答案在sqlserver中不支持,在mysql中支持
如果题答案中有错误,请记得及时通知我让我纠正错误!
我得邮箱地址:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sql 习题 答案
![提示](https://static.bingdoc.com/images/bang_tan.gif)