oracle数据库增删改查练习50例答案精.docx

上传人:b****5 文档编号:7279788 上传时间:2023-05-11 格式:DOCX 页数:8 大小:19.09KB
下载 相关 举报
oracle数据库增删改查练习50例答案精.docx_第1页
第1页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第2页
第2页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第3页
第3页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第4页
第4页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第5页
第5页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第6页
第6页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第7页
第7页 / 共8页
oracle数据库增删改查练习50例答案精.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

oracle数据库增删改查练习50例答案精.docx

《oracle数据库增删改查练习50例答案精.docx》由会员分享,可在线阅读,更多相关《oracle数据库增删改查练习50例答案精.docx(8页珍藏版)》请在冰点文库上搜索。

oracle数据库增删改查练习50例答案精.docx

oracle数据库增删改查练习50例答案精

oracle数据库增删改查练习50例-答案

一、建表--学生表droptablestudent;createtablestudent(snovarchar2(10,snamevarchar2(10,sagedate,ssexvarchar2(10;insertintostudentvalues('01','赵雷',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(cnovarchar2(10,cnamevarchar2(10,tnovarchar2(10;insertintocoursevalues('01','语文','02';insertintocoursevalues('02','数学','01';insertintocoursevalues('03','英语','03';

--教师表droptableteacher;createtableteacher(tnovarchar2(10,tnamevarchar2(10;insertintoteachervalues('01','张三';insertintoteachervalues('02','李四';insertintoteachervalues('03','王五';

--成绩表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;insertintoscvalues('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,sc1.score,sc2.scorefromstudents,scsc1,scsc2wheres.sno=sc1.snoands.sno=sc2.snoando='01'ando='02';

1.2、查询必须存在"01"课程,"02"课程可以没有的情况

selectt.*,s.score_01,s.score_02fromstudenttinnerjoin(selecta.sno,a.scorescore_01,b.scorescore_02fromscaleftjoin(select*fromscwherecno='02'bon(a.sno=b.snowhereo='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.snoando='01'ando='02'andsc1.score

2.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*fromscwherecno='01'a,(select*fromscwherecno='02'bwherea.sno(+=b.snotwheres.sno=t.snoand(t.score_01

3、查询平均成绩大于等于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、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩5.1、查询所有成绩的(不含缺考的)。

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

6、查询"李"姓老师的数量(有几个老师姓李)selectcount(tnfrom(selecttno,substr(tname,0,1tnfromteacherwheresubstr(tname,0,1='李'a;

7、哪些学生上过张三(老师)的课selectst.*fromstudentst,courseco,teacherte,scwherete.tno=co.tnoando=oandsc.sno=st.snoandte.tname='张三'

8、哪些学生没上过张三(老师)的课select*fromstudentminusselectst.*fromstudentst,courseco,teacherte,scwherete.tno=co.tnoando=oandsc.sno=st.snoandte.tname='张三'

9、查询'01''02'都学过的同学的信息selectst.*fromstudentst,(select*fromscwherecno='01'a,(select*fromscwherecno='02'bwherest.sno=a.snoandst.sno=b.sno

10、查询学过编号为'01'但是没有学过编号为'02'的课程的同学的信息selectst.*fromstudentst,((selectsnofromscwherecno='01'minus(selectsnofromscwherecno='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,(selectdistinctsnofromscwherecnoin(selectcnofromscwheresno='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,(selectsnofromstudentminusselectsnofromscwherecno=(selectofromteachert,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,cno,scorefromscwherecno='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*fromscwherecno='01'c,(select*fromscwherecno='02'd,(select*fromscwherecno='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,(selectcno,max(scoremax_scorefromscgroupbycnob,(selectcno,min(scoremin_scorefromscgroupbycnoc,(selectcno,round(avg(score,2avg_socrefromscgroupbycnod,(select'01'cno,round((selectcount(1fromscwherecno='01'andscore>=60/(selectcount(1fromscwherecno='01'*100,2"及格率",round((selectcount(1fromscwherecno='01'andscore>=70andscore<80/(selectcount(1fromscwherecno='01'*100,2"中等率",round((selectcount(1fromscwherecno='01'andscore>=80andscore<90/(selectcount(1fromscwherecno='01'*100,2"优良率",round((selectcount(1fromscwherecno='01'andscore>=90/(selectcount(1fromscwherecno='01'*100,2"优秀率"fromdualunionselect'02'cno,round((selectcount(1fromscwherecno='02'andscore>=60/(selectcount(1fromscwherecno='02'*100,2"及格率",round((selectcount(1fromscwherecno='02'andscore>=70andscore<80/(selectcount(1fromscwherecno='02'*100,2"中等率",

round((selectcount(1fromscwherecno='02'andscore>=80andscore<90/(selectcount(1fromscwherecno='02'*100,2"优良率",round((selectcount(1fromscwherecno='02'andscore>=90/(selectcount(1fromscwherecno='02'*100,2"优秀率"fromdualunionselect'03'cno,round((selectcount(1fromscwherecno='03'andscore>=60/(selectcount(1fromscwherecno='03'*100,2"及格率",round((selectcount(1fromscwherecno='03'andscore>=70andscore<80/(selectcount(1fromscwherecno='03'*100,2"中等率",round((selectcount(1fromscwherecno='03'andscore>=80andscore<90/(selectcount(1fromscwherecno='03'*100,2"优良率",round((selectcount(1fromscwherecno='03'andscore>90/(selectcount(1fromscwherecno='03'*100,2"优秀率"fromdualewhereo=oando=oando=oando=o;

19、按各科成绩进行排序,并显示排名selectsno,cno,score,rank(over(partitionbycnoorderbyscoredesc"名次"fromsc;

selectsno,cno,score,dense_rank(over(partitionbycnoorderbyscoredesc"名次"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,(selectcno,round(avg(score,2avg_scorefromscgroupbycnocwherea.tno=b.tnoando=oorderby"平均分"desc;

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩Score重复时保留名次空缺select*from(selectsno,cno,score,rank(over(partitionbycnoorderbyscoredescorder_scfromscawherea.order_scin(2,3;

Score重复时合并名次select*from(selectsno,cno,score,dense_rank(over(partitionbycnoorderbyscoredescorder_scfromscawherea.order_scin

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 笔试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2