answer1.docx
《answer1.docx》由会员分享,可在线阅读,更多相关《answer1.docx(14页珍藏版)》请在冰点文库上搜索。
![answer1.docx](https://file1.bingdoc.com/fileroot1/2023-5/27/b4f6706d-1054-4d0b-9044-c6983021c7b7/b4f6706d-1054-4d0b-9044-c6983021c7b71.gif)
answer1
SQL练习题一
表student,字段名name,number,sex,SQL2000,flash,net,其中SQL2000,flash,net为浮点型float
完成如下查询:
●输出所有男生的成绩
Select*fromstudentwheresex=’男’
●输出所有SQL成绩在90以上的女生的成绩
Select*fromstudentwheresex=’女’andsql2000>=90
●输出某一科目不合格所有的男生的成绩
Select*fromstudentwheresex=’男’and(sql2000<60orflash<60ornet<60)
●计算并显示每位同学各科的总分和平均分,并按总分从高到低排序
Selectname,number,sql2000+flash+net总分,(sql2000+flash+net)/3平均分
fromstudentorderbysql2000+flash+netdesc
●输出所有计算机网络成绩在70-79之间的同学
Select*fromstudentwherenetbetween70and79
Select*fromstudentwherenet>=70andnet<=79
●输出所有姓“陈”和姓“李”的男生
Select*fromstudentwheresex=’男’and(namelike‘陈%’ornamelike‘李%’)
Select*fromstudentwheresex=’男’andnamelike‘[陈李]%’
●输出所有学号为偶数的同学成绩
Select*fromstudentwherenumberlike‘%[02468]’
●输出Flash成绩最好的5位同学
Selecttop5*fromstudentorderbyflashdesc
●查询平均分最低的3位同学。
Selecttop3*fromstudentorderbysql2000+flash+net
●统计成绩表中平均分为90以上(含90分)人数
Selectcount(*)90分以上人数fromstudentwhere(sql2000+flash+net)/3>=90
SQL练习题二
表student,course,score,teacher如下:
STUDENT(SNO,SNAME,SSEX,SBIRTHDAY,CLASS)
COURSE(CNO,CNAME,TNO)
SCORE(SNO,CNO,DEGREE)
TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
1、查询Student表中的所有记录的Sname、Ssex和Class列。
2、查询教师所有的单位即不重复的Depart列。
selectdistinctdepartfromteacher
3、查询Student表的所有记录。
4、查询Score表中成绩在60到80之间的所有记录。
5、查询Score表中成绩为85,86或88的记录。
6、查询Student表中“95031”班或性别为“女”的同学记录。
7、以Class降序查询Student表的所有记录。
8、以Cno升序、Degree降序查询Score表的所有记录。
Select*fromscoreorderbycno,degreedesc
9、查询“95031”班的学生人数。
Selectcount(*)fromstudentwhereclass=’95031’
10、查询Score表中的最高分的学生学号和课程号。
Selecttop1sno,cnofromscoreorderbydegreedesc
Selectsno,cnofromscorewheredegree=(
Selectmax(degree)fromscore)
11、查询’3-105’号课程的平均分。
Selectavg(degree)fromscorewherecno=’3-105’
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
Selectcno,avg(degree)
Fromscore
Wherecnolike‘3%’
Groupbycno
Havingcount(*)>=5
13、查询最低分大于70,最高分小于90的Sno列及他们的平均分。
Selectsno,avg(degree)
Fromscore
Groupbysno
Havingmax(degree)<90andmin(degree)>70
14、查询所有学生的Sname、Cno和Degree列。
Selectsname,cno,degreefromstudent,score
Wherestudent.sno=score.sno
Selectsname,cno,degreefromstudentinnerjoinscoreonstudent.sno=score.sno
15、查询所有学生的Sno、Cname和Degree列。
Selectsno,cname,degreefromscore,coursewherescore.sno=o
16、查询所有学生的Sname、Cname和Degree列。
Selectsname,cname,degreefromstudent,score,course
Wherestudent.sno=score.snoando=o
17、查询“95033”班所选课程的平均分。
Selectavg(degree)
Fromstudent,score
Wherestudent.sno=score.snoandclass=’95033’
Selectcno,avg(degree)
Fromstudent,score
Wherestudent.sno=score.snoandclass=’95033’
Groupbycno
18、假设使用如下命令建立了一个grade表:
createtablegrade
(lownumber(3,0),uppnumber(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”号同学成绩的所有同学的记录。
Selectstudent.*fromstudent,scorewherestudent.sno=score.snoandcno=’3-105’anddegree>(
Selectdegreefromscorewherecno=’3-105’andsno=’109’)
Selectstudent.*fromstudent,scorewherestudent.sno=score.snoandcno=’3-105’anddegree>(
Selectmax(degree)fromscorewheresno=’109’)
Selectstudent.*fromstudent,scorewherestudent.sno=score.snoandcno=’3-105’anddegree>all(
Selectdegreefromscorewheresno=’109’)
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
Select*fromstudentwheresnoin(
selectsno
Fromscore
Wheresnonotin(Selectsnofromscorewheredegree=
(Selectmax(degree)fromscore))
Groupbysno
Havingcount(*)>1)
Select*fromstudentwheresnoin(selectsnoFromscore
Wheresnonotin(selectsnofromscore,(selectcno,max(degree)maxdegreefromscoregroupbycno)a
whereo=oandscore.degree=a.maxdegree)
Groupbysno
Havingcount(*)>1)
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
Selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday)=(
Selectyear(sbirthday)fromstudentwheresno=’108’)
23、查询“张旭“教师任课的学生成绩。
Select*fromscorewherecnoin(
Selectcnofromcoursewheretno=(
Selecttnofromteacherwheretname=’张旭’))
Selectscore.*
Fromteacher,course,score
Whereteacher.tno=course.tnoando=oandtname=’张旭’
24、查询选修某课程的同学人数多于5人的教师姓名。
Selecttnamefromteacherwheretnoin(
Selecttnofromcoursewherecnoin(
Selectcnofromscoregroupbycnohavingcount(*)>=5))
Selecttnamefromteacher,coursewhereteacher.tno=course.tno
Andcnoin(Selectcnofromscoregroupbycnohavingcount(*)>=5)
25、查询95033班和95031班全体学生的记录。
Select*fromstudentwhereclassin(‘95033’,’95031’)
Select*fromstudentwhereclass=‘95033’orclass=’95031’
26、查询存在有85分以上成绩的课程Cno.
Selectdistinctcno
Fromscorewheredegree>=85
27、查询出“计算机系“教师所教课程的成绩表。
Selecttname,cname,sno,degree
Fromteacher,course,score
Whereteacher.tno=course.tnoando=oanddepart=’计算机系’
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
Selecttname,proffromteacherwheredepart='信息'
andprofnotin(Selectproffromteacherwheredepart='经济')
union
Selecttname,proffromteacherwheredepart='经济'
andprofnotin(Selectproffromteacherwheredepart='信息')
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的某个同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
Selectcno,sno,degreefromscorewherecno=’3-105’anddegree>(
Selectmin(degree)fromscorewherecno=’3-245’)
Selectcno,sno,degreefromscorewherecno=’3-105’anddegree>any(
Selectdegreefromscorewherecno=’3-245’)
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
Selectcno,sno,degreefromscorewherecno=’3-105’anddegree>(
Selectmax(degree)fromscorewherecno=’3-245’)
Selectcno,sno,degreefromscorewherecno=’3-105’anddegree>all(
Selectdegreefromscorewherecno=’3-245’)
31、查询所有教师和同学的name、sex和birthday.
Selectsnamename,ssexsex,sbirthdaybirthday
Fromstudent
Union
Selecttnamename,tsexsex,tbirthdaybirthday
Fromteacher
32、查询所有“女”教师和“女”同学的name、sex和birthday.
Selectsnamename,ssexsex,sbirthdaybirthday
Fromstudent
Wheressex=’女’
Union
Selecttnamename,tsexsex,tbirthdaybirthday
Fromteacher
Wheretsex=’女’
33、查询成绩比该课程平均成绩低的同学的成绩表。
Selectscore.*
Fromscore,(Selectcno,avg(degree)avgdegreefromscoregroupbycno)a
Whereo=oandscore.degree34、查询所有任课教师的Tname和Depart.
Selecttname,departfromteacherwheretnoin(
Selectdistincttnofromcourse)
Selectdistincttname,departfromteacher,coursewhereteacher.tno=course.tno
35 查询所有未讲课的教师的Tname和Depart.
Selecttname,departfromteacherwheretnonotin(
Selectdistincttnofromcourse)
36、查询至少有2名男生的班号。
SelectclassFromstudentWheressex=’男’Groupbyclass
Havingcount(*)>=2
37、查询Student表中不姓“王”的同学记录。
Select*fromsstudentwheresnamenotlike‘王%’
38、查询Student表中每个学生的姓名和年龄。
Selectsname,year(getdate())-year(sbirthday)sagefromstudent
39、查询Student表中最大和最小的Sbirthday日期值。
Selectmax(sbirthday),min(sbirthday)fromstudent
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
Select*fromstudentorderbyclassdesc,sbirthday
41、查询“男”教师及其所上的课程。
Select*
Fromteacherleftjoincourse
Onteacher.tno=course.tnowheretsex=’男’
42、查询最高分同学的Sno、Cno和Degree列。
Selecttop1withties*
Fromscoreorderbydegreedesc
Select*fromscorewheredegree=(
Selectmax(degree)fromscore)
43、查询和“李军”同性别的所有同学的Sname.
Selectsnamefromstudentwheressex=(
Selectssexfromstudentwheresname=’李军’)
Selectb.sname
Fromstudenta,studentb
Wherea.姓名=’李军’anda.性别=b.性别
44、查询和“李军”同性别并同班的同学Sname.
Selectsnamefromstudentwheressex=(
Selectssexfromstudentwheresname=’李军’)
Andclass=(Selectlassfcromstudentwheresname=’李军’李军所在班级)
Selectb.sname
Fromstudenta,studentb
Wherea.sname=’李军’anda.class=b.classanda.ssex=b.ssex
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
Selectsname,score.*fromscore,studentwherescore.sno=student.sno
Andssex=’男’andscore.snoin(
Selectsnofromcourse,scorewhereo=oandcname=’计算机导论’)
查询所有选修“计算机导论”课程的“男”同学的成绩表
Select*fromscorewheresnoin(
Selectsnofromscorewherecno=(
Selectcnofromcoursewherecname=’计算机导论’))andsnoin(selectsnofromstudentwheressex=’男’)
SQL练习题三
1问题描述:
为管理岗位业务培训信息,建立3个表:
S (SNO,SN,SD,SA) SNO,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (CNO,CN ) CNO,CN 分别代表课程编号、课程名称
SC (SNO,CNO,G ) SNO,CNO,G 分别代表学号、所选修的课程编号、学习成绩
1.使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名
Selectsno,snfromswheresnoin(
Selectsnofromscwherecno=(
Selectcnofromcwherecn=’税收基础’))
Selects.sno,sn
Froms,sc,cwheres.sno=sc.snoando=oandcn=’税收基础’
2.使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
Selectsn,sdfromswheresnoin
(selectsnofromscwherecno=’c2’))
Selectsn,sdfroms,scwheres.sno=sc.snoandcno=’c2’
3.使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
Selectsn,sdfromswheresnonotin
(selectsnofromscwherecno=’c5’))
4.查询选修了课程的学员人数
Selectcount(distinctsno)
Fromsc
5.查询选修课程超过5门的学员学号和所属单位
Selctsno,sdfromswheresnoin(
Selectsno
Fromsc
Groupbysno
Havingcount(*)>5)
2问题描述:
已知关系模式:
S (SNO,SNAME) 学生关系。
SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。
CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。
SCGRADE 为成绩
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
Selectsnamefromswheresnonotin(
SelectdistinctsnofromScwherecnoin(
Selectcnofromcwherecteacher=’李明’))
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
列出有二门以上(含两门)不及格课程的学生sno及其平均成绩
Selectsno,avg(scgrade)fromscwheresnoin(
Selectsno
Fromsc
Wherescgrade<60
Groupbysno
Havingcount(*)>=2)groupbysno
Selectsname,avg(scgrade)fromsc,s
wheresc.sno=s.snoandsnoin(
Selectsno
Fromsc
Wherescgrade<60
Groupbysno
Havingcount(*)>=2)groupbysno,sname
3.列出既学过“1”号课程,又学过“2”号课