SQL数据库查询语言练习.docx
《SQL数据库查询语言练习.docx》由会员分享,可在线阅读,更多相关《SQL数据库查询语言练习.docx(9页珍藏版)》请在冰豆网上搜索。
SQL数据库查询语言练习
SQL数据库查询语言练习:
一、建立一个数据库表student,数据表computer,字段名name,number,sex,SQL2000,flash,net,其中SQL2000,flash,net设置为数值型数据
1、输出所有男生的成绩
selectyuyanasSQL数据库,flashas网络动画,netas计算机网络;
fromcomputer;
wheresex
2、输出所有SQL成绩在90以上的女生的成绩
selectSQL2000asSQL数据库fromcomputer;
wherenotsexandSQL2000>=90
3、输出某一科目不合格所有的男生的成绩
selectyuyanasSQL数据库,flashas网络动画,netas计算机网络;
fromcomputer;
wheresexandSQL2000<60orflash<60ornet<60
4、计算并显示每位同学各科的总分和平均分,并按总分从高到低排序
selectSQL2000+flash+netas总分,(SQL2000+flash+net/3)as平均分;
fromcomputer;
orderbySQL2000+flash+netdesc
5、输出所有计算机网络成绩在70-79之间的同学
select*fromcomputer;
whereflashbetween70and79
6、输出所有姓“陈”和姓“李”的男生
select*fromcomputer;
wheresexandleft(name,1)in('李','陈')
或者usestudent
select*fromcomputer
wheresex='男'(andnamelike'李__'ornamelike'陈__')
7、输出所有学号为偶数的同学成绩
selectnumas学号,SQL2000asSQL数据库,flashas网络动画,netas计算机网络;
fromcomputer;
wherenum%2=0
8、输出Flash成绩最好的5位同学
selecttop5*fromcomputerorderbyflashdesc
9、更新同学的成绩,把计算机网络成绩在55-59之间的同学该科的成绩调整为60分
updatecomputersetnet=60wherenetbetween55and59
10、删除平均分最低的3位同学
selecttop3*,(SQL2000+flash+net)/3as平均分fromcomputer;
orderby(SQL2000+flash+net)/3
deletefromcomputerwherenumberin(033001,033003,033011)
11、统计成绩表中平均分为90以上(含90分)人数
selectcount(*)fromcomputerwhere(SQL2000+flash+net)/3>=90
二、
问题描述:
已知关系模式:
s(sno,sname)学生关系。
sno为学号,
sname为姓名
c(cno,cname,cteacher)课程关系。
cno为课程号,
cname为课程名,
cteacher为任课教师
sc(sno,cno,scgrade)选课关系。
scgrade为成绩
要求实现如下5个处理:
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
--实现代码:
selectsnamefroms;
wherenotexists;
(select*fromsc,c;
where=and='李明'and=
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--实现代码:
select,,avg_scgrade=avg;
froms,sc,(;
selectsnofromsc;
wherescgrade<60;
groupbysnohavingcount(distinctcno)>=2;
)awhere=and=;
groupby,
3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
--实现代码:
select,;
froms,(;
select;
fromsc,c;
where=;
andin('1','2');
groupbysno;
havingcount(distinctcno)=2;
)scwhere=
4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
--实现代码:
select,;
froms,scsc1,scsc2;
where='1';
and='2';
and=;
and>
5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
--实现代码:
select,[1号课成绩]=,[2号课成绩]=;
fromscsc1,scsc2;
where='1';
and='2';
and=;
and>
练习题目
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(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’);
commit;
现查询所有同学的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、selectdistinctdepartfromteacher;
3、selectSnoas'学号',Snameas'姓名',Ssexas'性别',Sbirthdayas'出生日期',Classas'班号'fromstudent;
或selectSnoas学号,Snameas姓名,Ssexas性别,Sbirthdayas出生日期,Classas班号fromstudent;
4、select*fromscorewheredegreebetween60and80;或select*fromscorewheredegree>=60anddegree<=80;
5、select*fromscorewheredegreein(85,86,88);
6、select*fromstudentwhereclass='95031'orSsex='女';
7、select*fromstudentorderbyclassdesc;
8、select*fromscoreorderbycnoasc,degreedesc;或select*fromscoreorderbycno,degreedesc;
9、selectcount(*)asCNTfromstudentwhereclass='95031';
10、selectSnoas'学号',cnoas'课程号',degreeas'最高分'fromscorewheredegree=(selectmax(degree)from
score)
11、selectavg(degree)as课程平均分fromscorewherecno='3-105';
12、selectcno,avg(degree)fromscorewherecnolike'3%'groupbycnohavingcount(*)>5;
13、selectSnofromscoregroupbySnohavingmin(degree)>70andmax(degree)<90;
14、select,,fromstudent,scorewhere=;
15、select,,fromscorex,courseywhere=;
16、select,,fromstudentx,coursey,scorezwhere=and=;
17、select,avgfromstudentx,scoreywhere=and='95033'groupby;
18、selectSno,Cno,rankfromscore,gradewheredegreebetweenlowandupporderbyrank;
19、select,,fromscorex,scoreywhere='3-105'and>and
='109'and='3-105';
20、
1,查询成绩非本科最高select*fromscorebwheredegree<(selectmax(degree)fromscoreawhere
=;
2,查询成绩非本科最高并且选2门以上的学生的成绩:
21、select,,fromscorex,scoreywhere>and='109'and='3-105';
selectcno,sno,degreefromscorewheredegree>(selectdegreefromscorewheresno='109'andcno='3-105')
22、selectsno,sname,sbirthdayfromstudentwhereto_char(sbirthday,'yyyy')=(selectto_char(sbirthday,'yyyy')
fromstudentwheresno='108');
23、selectcno,sno,degreefromscorewherecno=(selectfromcoursex,teacherywhere=and
='张旭');
24、selecttnamefromteacherwheretnoin(selectfromcoursex,scoreywhere=groupby
havingcount>5);
25、select*fromstudentwhereclassin('95033','95031');
26、selectdistinctcnofromscorewheredegreein(selectdegreefromscorewheredegree>85);
27、select*fromscorewherecnoin(selectfromcoursex,teacherywhere=and='计算
机系');
28、selecttname,proffromteacherwheredepart='计算机系'andprofnotin(selectproffromteacherwhere
depart='电子工程系');
29、select*fromscorewherecno='3-105'anddegree>any(selectdegreefromscorewherecno='3-245')orderby
degreedesc;
30、select*fromscorewherecno='3-105'anddegree>all(selectdegreefromscorewherecno='3-245');
31、selecttname,tsex,tbirthdayfromteacher
unionselectsname,ssex,sbirthdayfromstudent;
32、selecttname,tsex,tbirthdayfromteacherwheretsex='女'
unionselectsname,ssex,sbirthdayfromstudentwheressex='女';
33、select*fromscoreawheredegree<(selectavg(degree)
fromscorebwhere=;
34、selecttname,departfromteacherawhereexists
(select*fromcoursebwhere=;
35、selecttname,departfromteacherawherenotexists
(select*fromcoursebwhere=;
36、selectclassfromstudentwheressex='男'groupbyclasshavingcount(*)>=2;
37、select*fromstudentwheresnamenotlike'王_';
38、selectsnameas姓名,(to_char(sysdate,'yyyy')-to_char(sbirthday,'yyyy'))as年龄fromstudent
39、selectsname,sbirthdayas最大fromstudentwheresbirthday=(selectmin(sbirthday)fromstudent)
unionselectsname,sbirthdayas最小fromstudentwheresbirthday=(selectmax(sbirthday)fromstudent)
40、selectclass,sname,sbirthdayfromstudentorderbyclassdesc,sbirthday;
41、select,fromteacherx,courseywhere=and='男';
42、select*fromscorewheredegree=(selectmax(degree)fromscore);
43、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军');
44、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军')andclass=(selectclass
fromstudentwheresname='李军');
45、select*fromscorewheresnoin(selectsnofromstudentwheressex='男')andcno=(selectcnofromcourse
wherecname='计算机导论');