oracle数据库增删改查练习50例答案精.docx
《oracle数据库增删改查练习50例答案精.docx》由会员分享,可在线阅读,更多相关《oracle数据库增删改查练习50例答案精.docx(13页珍藏版)》请在冰豆网上搜索。
oracle数据库增删改查练习50例答案精
oracle数据库增删改查练习50例-答案
一、建表--学生表droptablestudent;createtablestudent(snovarchar2(10,snamevarchar2(10,sagedate,ssexvarchar2(10;insertintostudentvalues('0赵雷',to_date('1990/01/01','yyyy/mm/dd',男';insertintostudentvalues('02'钱电',to_date('1990/12/21','yyyy/mm/dd',男';insertintostudentvalues('03'孙风',to_date('1990/05/20','yyyy/mm/dd',男';insertintostudentvalues('04'李云',to_date('1990/08/06','yyyy/mm/dd',男';insertintostudentvalues('05'周梅',to_date('1991/12/01','yyyy/mm/dd',女';insertintostudentvalues('06'吴兰',to_date('1992/03/01','yyyy/mm/dd',女';insertintostudentvalues('07'郑竹',to_date('1989/07/01','yyyy/mm/dd',女';insertintostudentvalues('08'王菊',to_date('1990/01/20','yyyy/mm/dd',女';--课程表droptablecourse;createtablecourse(enovarchar2(10,cnamevarchar2(10,tnovarchar2(10;insertintocoursevalues('0语文','02';insertintocoursevalues('02'数学','01';insertintocoursevalues('03'英语','03';
--教师表droptableteacher;createtableteacher(tnovarchar2(10,tnamevarchar2(10;insertintoteachervalues('01张三';insertintoteachervalues('02'李四[insertintoteachervalues('O3'王五';
--成绩表droptablesc;createtablesc(snovarchar2(10,cnovarchar2(10,scorenumber(18,1;insertintoscvalues('01','01',80.0;insertintoscvalues('01','02',90.0;insertintoscvalues('01','03',99.0;insertintoscvalues('02','01',70.0;insertintoscvalues('02','02',60.0;insertintoscvalues('02','03',80.0;insertintosc
values('03','01',80.0;insertintoscvalues('03','02',80.0;insertintoscvalues('03','03',80.0;insertintoscvalues('04','01',50.0;insertintoscvalues('04','02',30.0;insertintoscvalues('04','03',20.0;insertintoscvalues('05','01',76.0;insertintoscvalues('05','02',87.0;insertintoscvalues('06','01',31.0;insertintoscvalues('06','03',34.0;insertintoscvalues('07','02',89.0;insertintoscvalues('07','03',98.0;
commit;
二、查询1.1、查询同时存在"01"课程和"02"课程的情况selects.sno,s.sname,s.sage,s.ssex,scl.score,sc2.scorefromstudents,scsc1,scsc2wheres.sno=scl.snoands.sno=sc2.snoandscl.cno='01'andsc2.cno='02';
1.2、查询必须存在"01"课程,"02"课程可以没有的情况
selectt.*,s.score_01,s.score_02fromstudenttinnerjoin(selecta.sno,a.scorescore_01,b.scorescore_02fromscaleftjoin(select*fromscwhereeno='02'bon(a.sno=b.snowherea.cno='01'son(t.sno=s.sno;
2.1、查询同时'01'课程比'02'课程分数低的数据
selects.sno,s.sname,s.sage,s.ssex,sc1.score,sc2.scorefromstudents,scsc1,scsc2wheres.sno=sc1.snoands.sno=sc2.snoandsc1.cno='01'andsc2.cno='02'andsc1.score2.2、查询同时'01'课程比'02'课程分数低或'01缺考的数据selects.sno,s.sname,s.sage,s.ssex,t.score_01,t.score_02fromstudents,(selectb.sno,a.scorescore_01,
b.scorescore_02from(select*fromscwhereeno='01'a,(select*fromscwhereeno='02'bwherea.sno(+=b.snotwheres.sno=t.snoand(t.score_013、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩selects.sno,s.sname,t.avg_scoreavg_scorefromstudents,(selectsno,round(avg(score,2avg_scorefromscgroupbysnohavingavg(score>=60orderbysnotwheres.sno=t.sno;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩4.1、有
考试成绩,且小于60分selects.sno,s.sname,t.avg_scoreavg_scorefromstudents,
(selectsno,round(avg(score,2avg_scorefromscgroupbysnohavingavg(score<60orderbysnotwheres.sno=t.sno;
4.2、包括没有考试成绩的数据selectg.*from(selects.sno,s.sname,nvl(t.avg_score,0avg_scorefromstudents,(selectsno,round(avg(score,2avg_scorefromscgroupbysnoorderbysnotwheres.sno=t.sno(+gwhereg.avg_score<60;
5.1、
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
查询所有成绩的(不含缺考的)。
selects.sno,s.sname,c.count_cno,c.sum_scorefromstudents,(selectsno,count(cnocount_cno,sum(scoresum_scorefromscgroupbysnoorderbysnoc
wheres.sno=c.sno
5.2、查询所有成绩的(包括缺考的。
selecta.s_sno,a.s_sname,nvl(a.c_cno,0,a.s_scorefrom(selects.snos_sno,s.snames_sname,c.count_cnoc_cno,c.sum_scores_scorefromstudents,(selectsno,count(cnocount_cno,sum(scoresum_scorefromscgroupbysnoorderbysnocwheres.sno=c.sno(+a
&查询"李"姓老师的数量(有几个老师姓李)selectcount(tnfrom(selecttno,substr(tname,0,1tnfromteacherwheresubstr(tname,0,1李'a;
7、哪些学生上过张三(老师)的课selectst.*fromstudentst,courseco,teacherte,scwherete.tno=co.tnoandco.cno=sc.cnoandsc.sno=st.snoandte.tnamefe三'
8、哪些学生没上过张三(老师)的课select*fromstudentminusselectst.*fromstudentst,courseco,teacherte,scwherete.tno=co.tnoandco.cno=sc.cnoandsc.sno=st.snoandte.tname=张三'
9、查询'01''02都学过的同学的信息selectst.*fromstudentst,(select*fromscwhereeno='01'a,(select*fromscwhereeno='02'bwherest.sno=a.snoandst.sno=b.sno
10、查询学过编号为'01但是没有学过编号为’02'的课程的同学的信息selectst.*fromstudentst,((selectsnofromscwhereeno='01'minus(selectsnofromscwhereeno='02'awherest.sno=a.sno;
11、查询没有学全所有课程的同学的信息11.1学完所有课程的selectst.*fromstudentst,(selectsno,count(cnofromscgroupbysnohavingcount(cno=3awherest.sno=a.sno;
11.2没有学完所有课程的selectst.*fromstudentst,
(selectsnofromstudentminusselectsnofromscgroupbysnohavingcount(cno=3awherest.sno=a.sno;
12、查询至少有一门课与学号为'01'的同学所学相同的同学的信息selectst.*fromstudentst,(selectdistinetsnofromscwhereenoin(selectenofromscwheresno='01'andsno匸1awherest.sno=a.sno;
13、查询和'01'号的同学学习的课程完全相同的其他同学的信息selectst.*fromstudentst,(selectsnofrom(selectsno,count(cnoCNT1fromscgroupbysnoa,(selectcount(cnoCNT2fromscwheresno='01'bwherea.CNT1=b.CNT2anda.sno!
='01'cwherest.sno=c.sno;
14、查询没学过"张三"老师讲授的任一门课程的学生姓名selectst.*fromstudentst,(selectsnofromstudentminusselectsnofromscwhereeno=(selectc.cnofromteachert,coursecwheret.tno=c.tnoandtname张三'awherest.sno=a.sno;
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩selecta.sno,st.sname,a.avg_scorefromstudentst,(selectsno,count(cno,round(avg(score,2avg_scorefrom(select*fromscwherescore<60groupbysnoawherest.sno=a.sno;
16、检索'01'课程分数小于60,按分数降序排列的学生信息selectst.*,o.
a.scorefromstudentst,(selectsno,eno,scorefromscwhereeno='01'andscore<60orderbyscoredescawherest.sno=a.sno;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩selectst.sno,st.sname,c.score,d.score,e.score,a.avg_scorefromstudentst,(selectsno,round(avg(score,2avg_scorefromscgroupbysno
orderbyavg_scoredesca,(select*fromscwhereeno='01'c,(select*fromscwhereeno='02'd,(select*fromscwhereeno='03'ewherest.sno=a.sno(+andst.sno=c.sno(+andst.sno=d.sno(+andst.sno=e.sno(+
18、查询各科成绩最高分、最低分和平均分:
以如下形式显示:
课程ID,课
程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率selecto"课程编号",ame"课程名称",b.max_score最高分",c.min_score"最低分",d.avg_socre平均分",e."及格率",e."中等率",e."优良率",e."优秀率"fromcoursea,(selecteno,max(scoremax_scorefromscgroupbyenob,(selecteno,min(scoremin_scorefromscgroupbyenoc,(selecteno,round(avg(score,2avg_socrefromscgroupbyenod,(select'01'eno,round((selectcount(1fromscwhereeno='01'andscore>=60/(selectcount(1fromscwhereeno='01'*100,2及格率",round((selectcount(1fromscwhereeno='01'andscore>=70andscore<80/(selectcount(1fromscwhereeno='01'*100,2"中等率",round((selectcount(1fromscwhereeno='01'andscore>=80andscore<90/(selectcount(1fromscwhereeno='01'*100,2优良率",round((selectcount(1fromscwhereeno='01'andscore>=90/(selectcount(1fromscwhereeno='01'*100,2"优秀率"fromdualunionselect'02'cno,round((selectcount(1fromscwhereeno='02'andscore>=60/(selectcount(1fromscwhereeno='02'*100,
2"及格率",round((selectcount(1fromscwhereeno='02'andscore>=70andscore<
80/(selectcount(1fromscwhereeno='02'*100,2中等率",
round((selectcount(1fromscwhereeno='02'andscore>=80andscore<90/(selectcount(1fromscwhereeno='02'*100,2忧良率",round((selectcount(1fromscwhereeno='02'andscore>=90/(selectcount(1fromscwhereeno='02'*100,2优秀率"fromdualunionselect'03'cno,round((selectcount(1fromscwhereeno='03'andscore>=60/(selectcount(1fromscwhereeno='03'*100,2及格率",round((selectcount(1fromscwhereeno='03'andscore>=70andscore<80/(selectcount(1fromscwhereeno='03'*100,2"中等率",round((selectcount(1fromscwhereeno='03'andscore>=80andscore<90/(selectcount(1fromscwhereeno='03'*100,2优良率",round((selectcount(1fromscwhereeno='03'andscore>90/(selectcount(1fromsc
whereeno='03'*100,2'优秀率"fromdualewhereo=oando=oanda.cno=d.cnoanda.cno=e.cno;
19、按各科成绩进行排序,并显示排名selectsno,eno,score,
rank(over(partitionbyenoorderbyscoredesc名次"fromsc;
selectsno,eno,score,dense_rank(over(partitionbyenoorderbyscoredes名次"
fromsc;
20、查询学生的总成绩并进行排名20.1查询学生的总成绩selecta.sno,a.sname,nvl(b.sum_score,0总、成绩"fromstudenta,(selectsno,sum(scoresum_scorefromscgroupbysnoorderbysnobwherea.sno=b.sno(+;
20.2查询学生的总成绩并进行排名。
selectc."学生编号",c."学生姓名",c."总成
绩",rank(over(orderbyc."总成绩"desc"排名"from(selecta.sno"学生编号",a.sname"学生姓名",nvl(b.sum_score,0"总成绩"fromstudenta,(selectsno,sum(scoresum_scorefromscgroupbysnoorderbysnobwherea.sno=b.sno(+c
21、查询不同老师所教不同课程平均分从高到低显示selecta.tno,a.tname,
c.avg_score平均分"fromteachera,courseb,(selecteno,round(avg(score,2avg_scorefromscgroupbyenocwherea.tno=b.tnoandb.cno=c.cnoorderby平均分"desc;
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩Score重复
时保留名次空缺select*from(selectsno,eno,score,rank(over(partition