OracleSQL练习题及答案.docx
《OracleSQL练习题及答案.docx》由会员分享,可在线阅读,更多相关《OracleSQL练习题及答案.docx(19页珍藏版)》请在冰豆网上搜索。
OracleSQL练习题及答案
本文由NiCoBee奉献OracleSQL练习题
注:
这些查询题目涵盖了各种查询的技术,请大家认真做,做好了的查询语句之后,有兴趣
的同学可以再创建相应的视图和存储过程
createtablewkj_student(
snovarchar2(10)primarykey,
snamevarchar2(20),
sagenumber
(2),
ssexvarchar2(5)
);
createtablewkj_teacher(
tnovarchar2(10)primarykey,
tnamevarchar2(20)
);
createtablewkj_course(
cnovarchar2(10),
cnamevarchar2(20),
tnovarchar2(20),
constraintpk_courseprimarykey(cno,tno)
);
createtablewkj_sc(
snovarchar2(10),
cnovarchar2(10),
scorenumber(4,2),
constraintpk_scprimarykey(sno,cno)
);
/*******初始化学生表的数据******/
insertintostudentvalues('s001','张三',23,'男');
insertintostudentvalues('s002','李四',23,'男');
insertintostudentvalues('s003','吴鹏',25,'男');
insertintostudentvalues('s004','琴沁',20,'女');
insertintostudentvalues('s005','王丽',20,'女');
insertintostudentvalues('s006','李波',21,'男');
insertintostudentvalues('s007','刘玉',21,'男');
insertintostudentvalues('s008','萧蓉',21,'女');
insertintostudentvalues('s009','陈萧晓',23,'女');
insertintostudentvalues('s010','陈美',22,'女');
commit;
/******************初始化教师表***********************/
insertintoteachervalues('t001','刘阳');
insertintoteachervalues('t002','谌燕');
insertintoteachervalues('t003','胡明星');
commit;
/***************初始化课程表****************************/
insertintocoursevalues('c001','J2SE','t002');
insertintocoursevalues('c002','JavaWeb','t002');
insertintocoursevalues('c003','SSH','t001');
insertintocoursevalues('c004','Oracle','t001');
insertintocoursevalues('c005','SQLSERVER2005','t003');
insertintocoursevalues('c006','C#','t003');
insertintocoursevalues('c007','JavaScript','t002');
insertintocoursevalues('c008','DIV+CSS','t001');
insertintocoursevalues('c009','PHP','t003');
insertintocoursevalues('c010','EJB3.0','t002');
commit;
/***************初始化成绩表***********************/
insertintoscvalues('s001','c001',78.9);
insertintoscvalues('s002','c001',80.9);
insertintoscvalues('s003','c001',81.9);
insertintoscvalues('s004','c001',60.9);
insertintoscvalues('s001','c002',82.9);
insertintoscvalues('s002','c002',72.9);
insertintoscvalues('s003','c002',81.9);
insertintoscvalues('s001','c003','59');
insertintoscvalues('s002','c003','58');
commit;
练习:
注意:
以下练习中的数据是根据初始化到数据库中的数据来写的SQL语句,请大家务必注意。
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
2、查询平均成绩大于60分的同学的学号和平均成绩;
3、查询所有同学的学号、、选课数、总成绩;
4、查询姓“刘”的老师的个数;
5、查询没学过“谌燕”老师课的同学的学号、;
6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、;
7、查询学过“谌燕”老师所教的所有课的同学的学号、;
8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、;
9、查询所有课程成绩小于60分的同学的学号、;
10、查询没有学全所有课的同学的学号、;
11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和;
12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和;
13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和;
15、删除学习“谌燕”老师课的SC表记录;
16、向SC表中插入一些记录,这些记录要求符合以下条件:
没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
17、查询各科成绩最高和最低的分:
以如下形式显示:
课程ID,最高分,最低分
18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
19、查询不同老师所教不同课程平均分从高到低显示
20、统计列印各科成绩,各分数段人数:
课程ID,课程名称,[100-85],[85-70],[70-60],[<60]
21、查询各科成绩前三名的记录:
(不考虑成绩并列情况)
22、查询每门课程被选修的学生数
23、查询出只选修了一门课程的全部学生的学号和
24、查询男生、女生人数
25、查询姓“张”的学生名单
26、查询同名同性学生名单,并统计同名人数
27、1981年出生的学生名单(注:
Student表中Sage列的类型是number)
28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
29、查询平均成绩大于85的所有学生的学号、和平均成绩
30、查询课程名称为“数据库”,且分数低于60的学生和分数
31、查询所有学生的选课情况;
32、查询任何一门课程成绩在70分以上的、课程名称和分数;
33、查询不及格的课程,并按课程号从大到小排列
34、查询课程编号为c001且课程成绩在80分以上的学生的学号和;
35、求选了课程的学生人数
36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生及其成绩
37、查询各个课程及相应的选修人数
38、查询不同课程成绩相同的学生的学号、课程号、学生成绩
39、查询每门功课成绩最好的前两名
40、统计每门课程的学生选修人数〔超过10人的课程才统计〕。
要求输出课程号和选修人数,查询结果按人数降序排列,假设人数相同,按课程号升序排列
41、检索至少选修两门课程的学生学号
42、查询全部学生都选修的课程的课程号和课程名
43、查询没学过“谌燕”老师讲授的任一门课程的学生
44、查询两门以上不及格课程的同学的学号及其平均成绩
45、检索“c004”课程分数小于60,按分数降序排列的同学学号
46、删除“s002”同学的“c001”课程的成绩
1.查询“c001”课程比“c002”课程成绩高的所有学生的学号
*********************************
selecta.*from
(select*fromscawhereao='c001')a,
(select*fromscbwherebo='c002')b
wherea.sno=b.snoanda.score>b.score;
*********************************
select*fromsca
whereao='c001'
andexists(select*fromscbwherebo='c002'anda.score>b.score
anda.sno=b.sno)
*********************************
2.查询平均成绩大于60分的同学的学号和平均成绩;
*********************************
selectsno,avg(score)fromscgroupbysnohavingavg(score)>60;
*********************************
3.3、查询所有同学的学号、、选课数、总成绩;
*********************************
selecta.*,s.snamefrom(selectsno,sum(score),count(cno)fromscgroupbysno)a,studentswherea.sno=s.sno
错错误示范
selectst.*,count(sco),sum(sc.score)
fromstudentst,sc
wherest.sno=sc.sno
groupbyst.sno
selectcount(sco)sum(sc.score)不是单数分组,要先group,不然和selectstu.sno关联不起来
*********************************
4.4、查询姓“刘”的老师的个数;
*********************************
selectcount(*)fromteacherwheretnamelike'刘%';
*********************************
5.5、查询没学过“谌燕”老师课的同学的学号、;
*********************************
selecta.sno,a.snamefromstudenta
wherea.sno
notin
(selectdistincts.sno
fromscs,
(selectc.*
fromcoursec,
(selecttno
fromteachert
wheretname='谌燕')t
wherec.tno=t.tno)b
whereso=bo)
*********************************
select*fromstudentstwherest.snonotin
(selectdistinctsnofromscsjoincourseconso=co
jointeachertonc.tno=t.tnowheretname='谌燕')
*********************************
6.6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、;
*********************************
selectst.*fromstudentst
joinscaonst.sno=a.sno
joinscbona.sno=b.sno
whereao='c001'andbo='c002';
*********************************
7.7、查询学过“谌燕”老师所教的所有课的同学的学号、;
*********************************
selectst.*fromstudentstjoinscsonst.sno=s.sno
joincourseconso=co
jointeachertonc.tno=t.tno
wheret.tname='谌燕'
*********************************
8.8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、;
*********************************
select*fromstudentst
joinscaonst.sno=a.sno
joinscbonst.sno=b.sno
whereao='c002'andbo='c001'anda.score*********************************
9.9、查询所有课程成绩小于60分的同学的学号、
*********************************
selectst.*,s.scorefromstudentst
joinscsonst.sno=s.sno
joincourseconso=co
wheres.score<60
*********************************
10.查询没有学全所有课的同学的学号、
*********************************
selectstu.sno,stu.sname,count(sco)fromstudentstu
leftjoinsconstu.sno=sc.sno
groupbystu.sno,stu.sname
havingcount(sco)<(selectcount(distinctcno)fromcourse)
===================================
select*fromstudentwheresnoin
(selectsnofrom
(selectstu.sno,cofromstudentstu
crossjoincoursec
minus
selectsno,cnofromsc)
)
===================================
*********************************
11.查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和
*********************************
selectst.*fromstudentst,
(selectdistincta.snofrom
(select*fromsc)a,
(select*fromscwheresc.sno='s001')b
whereao=bo)h
wherest.sno=h.snoandst.sno<>'s001'
*********************************
12.查询至少学过学号为“s001”同学所有一门课的其他同学学号和;
*********************************
select*fromsc
leftjoinstudentst
onst.sno=sc.sno
wheresc.sno<>'s001'
andscoin
(selectcnofromsc
wheresno='s001')
*********************************
13.把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩
*********************************
updatesccsetscore=(selectavg(c.score)fromcoursea,teacherb
wherea.tno=b.tno
andb.tname='谌燕'
andao=co
groupbyco)
wherecnoin(
selectcnofromcoursea,teacherb
wherea.tno=b.tno
andb.tname='谌燕')
*********************************
不会做14.(原来是错的我改过来了)查询和“s001”号的同学学习的课程完全相同的其他同学学号和
*********************************
*********************************
15.删除学习“谌燕”老师课的SC表记录;
*********************************
deletefromsc
wherescoin
(
selectcnofromcoursec
leftjointeachertonc.tno=t.tno
wheret.tname='谌燕'
)
*********************************
16.16、向SC表中插入一些记录,这些记录要求符合以下条件:
没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;
*********************************
insertintosc(sno,cno,score)
selectdistinctst.sno,sco,(selectavg(score)fromscwherecno='c002')
fromstudentst,sc
wherenotexists
(select*fromscwherecno='c002'andsc.sno=st.sno)andsco='c002';
*********************************
17.17、查询各科成绩最高和最低的分:
以如下形式显示:
课程ID,最高分,最低分
*********************************
selectcno,max(score),min(score)fromscgroupbycno;
*********************************
18.18、按各科平均成绩从低到高和及格率的百分数从高到低顺序
*********************************
selectcno,avg(score),round(sum(casewhenscore>=60then1else0end)/count(*),2)*100||’%’
as及格率
fromscgroupbycno
orderbyavg(score),及格率desc
*********************************
19.19、查询不同老师所教不同课程平均分从高到低显示
*********************************
selectmax(t.tno),max(t.tname),max(co),max(came),co,avg(score)fromsc,coursec,teachert
wheresco=coandc.tno=t.tno
groupbyco
orderbyavg(score)desc
*********************************
20.20、统计列印各科成绩,各分数段人数:
课程ID,课程名称,[100-85],[85-70],[70-60],[<60]
*********************************答案是错的
selectsco,came,
sum(casewhenscorebetween100and85then1else0end)AS"[100-85]",
sum(casewhenscorebetween85and70then1else0end)AS"[85-70]",
sum(casewhenscorebetween70and60then1else0end)AS"[70-60]",
sum(casewhenscore<60then1else0end)AS"[<60]"
fromsc,coursec
wheresco=co
groupbysco,came;
改正后的
selectsco,came,
sum(casewhenscore>85andscore<100then1else0end)85到100,
sum(casewhenscore>70andscore<850then1else0end)70到85,
sum(casewhenscore>60andthen1else0end)60到70,
sum(casewhenscore<60then1else0end)60以下
fromsc,coursec
wheresco=co
groupbysco,came;
*********************************
21.查询各科成绩前三名的记录:
(不考虑成绩并列情况)
*********************************
select*from
(selectsno,cno,