版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 oracle 数据库增删改查练习50例-答案 一、建表-学生表drop table student;create table student (sno varchar2(10,sname varchar2(10,sage date,ssex varchar2(10;insert into student values(01,赵雷,to_date(1990/01/01,yyyy/mm/dd,男;insert into student values(02,钱电,to_date(1990/12/21,yyyy/mm/dd,男;insert into student values(03,孙风,to_
2、date(1990/05/20,yyyy/mm/dd,男;insert into student values(04,李云,to_date(1990/08/06,yyyy/mm/dd,男;insert into student values(05,周梅,to_date(1991/12/01,yyyy/mm/dd,女;insert into student values(06,吴兰,to_date(1992/03/01,yyyy/mm/dd,女;insert into student values(07,郑竹,to_date(1989/07/01,yyyy/mm/dd,女;insert into
3、 student values(08,王菊,to_date(1990/01/20,yyyy/mm/dd,女;-课程表drop table course;create table course (cno varchar2(10,cname varchar2(10,tno varchar2(10;insert into course values (01,语文,02;insert into course values (02,数学,01;insert into course values (03,英语,03; -教师表drop table teacher;create table teacher
4、(tno varchar2(10,tname varchar2(10;insert into teacher values(01,张三;insert into teacher values(02,李四;insert into teacher values(03,王五; -成绩表drop table sc;create table sc (sno varchar2(10,cno varchar2(10,score number(18,1;insert into sc values(01,01,80.0;insert into sc values(01,02,90.0;insert into sc
5、 values(01,03,99.0;insert into sc values(02,01,70.0;insert into sc values(02,02,60.0;insert into sc values(02,03,80.0;insert into sc values(03,01,80.0;insert into sc values(03,02,80.0;insert into sc values(03,03,80.0;insert into sc values(04,01,50.0;insert into sc values(04,02,30.0;insert into sc va
6、lues(04,03,20.0;insert into sc values(05,01,76.0;insert into sc values(05,02,87.0;insert into sc values(06,01,31.0;insert into sc values(06,03,34.0;insert into sc values(07,02,89.0;insert into sc values(07,03,98.0; commit; 二、查询1.1、查询同时存在01课程和02课程的情况select s.sno, s.sname, s.sage, s.ssex, sc1.score, s
7、c2.score from student s, sc sc1, sc sc2 where s.sno = sc1.sno and s.sno = sc2.sno and o = 01 and o = 02; 1.2、查询必须存在01课程,02课程可以没有的情况 select t.*, s.score_01, s.score_02 from student t inner join (select a.sno, a.score score_01, b.score score_02 from sc a left join (select * from sc where c
8、no = 02 b on (a.sno = b.sno where o = 01 s on (t.sno = s.sno; 2.1、查询同时01课程比02课程分数低的数据 select s.sno, s.sname, s.sage, s.ssex, sc1.score, sc2.score from student s, sc sc1, sc sc2 where s.sno = sc1.sno and s.sno = sc2.sno and o = 01 and o = 02 and sc1.score sc2.score; 2.2、查询同时01课程比02课程分
9、数低或01缺考的数据select s.sno, s.sname, s.sage, s.ssex, t.score_01, t.score_02 from student s, (select b.sno, a.score score_01, b.score score_02 from (select * from sc where cno = 01 a, (select * from sc where cno = 02 b where a.sno(+ = b.sno t where s.sno = t.sno and (t.score_01 = 60 order by sno t where
10、s.sno = t.sno; 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩4.1、有考试成绩,且小于60分select s.sno, s.sname, t.avg_score avg_score from student s, (select sno, round(avg(score, 2 avg_score from sc group by sno having avg(score 60 order by sno t where s.sno = t.sno; 4.2、包括没有考试成绩的数据select g.* from (select s.sno, s.sname, nvl
11、(t.avg_score, 0 avg_score from student s, (select sno, round(avg(score, 2 avg_score from sc group by sno order by sno t where s.sno = t.sno(+ g where g.avg_score 60; 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩5.1、查询所有成绩的(不含缺考的)。select s.sno,s.sname,c.count_cno,c.sum_scorefrom student s,(select sno,count(cno co
12、unt_cno,sum(score sum_score from sc group by sno order by sno c where s.sno = c.sno 5.2、查询所有成绩的(包括缺考的。select a.s_sno, a.s_sname, nvl(a.c_cno, 0, a.s_score from (select s.sno s_sno, s.sname s_sname, c.count_cno c_cno, c.sum_score s_score from student s, (select sno, count(cno count_cno, sum(score sum
13、_score from sc group by sno order by sno c where s.sno = c.sno(+ a 6、查询李姓老师的数量 (有几个老师姓李)select count(tn from (select tno, substr(tname, 0, 1 tn from teacher where substr(tname, 0, 1 = 李 a; 7、哪些学生上过张三(老师)的课select st.* from student st, course co, teacher te, sc where te.tno = co.tno and o =
14、o and sc.sno = st.sno and te.tname = 张三 8、哪些学生没上过张三(老师)的课select * from studentminus select st.* from student st, course co, teacher te, sc where te.tno = co.tno and o = o and sc.sno = st.sno and te.tname = 张三 9、查询 01 02都学过的同学的信息select st.* from student st, (select * from sc where cno = 01
15、a, (select * from sc where cno = 02 b where st.sno = a.sno and st.sno = b.sno 10、查询学过编号为01但是没有学过编号为02的课程的同学的信息select st.* from student st, (select sno from sc where cno = 01 minus (select sno from sc where cno = 02 a where st.sno = a.sno; 11、查询没有学全所有课程的同学的信息11.1 学完所有课程的select st.* from student st, (
16、select sno, count(cno from sc group by sno having count(cno = 3 a where st.sno = a.sno; 11.2 没有学完所有课程的select st.* from student st, (select sno from student minus select sno from sc group by sno having count(cno = 3 a where st.sno = a.sno; 12、查询至少有一门课与学号为01的同学所学相同的同学的信息select st.* from student st, (s
17、elect distinct sno from sc where cno in (select cno from sc where sno = 01 and sno != 1 a where st.sno = a.sno; 13、查询和01号的同学学习的课程完全相同的其他同学的信息select st.* from student st, (select sno from (select sno, count(cno CNT1 from sc group by sno a, (select count(cno CNT2 from sc where sno = 01 b where a.CNT1
18、= b.CNT2 and a.sno != 01 c where st.sno = c.sno; 14、查询没学过张三老师讲授的任一门课程的学生姓名select st.* from student st, (select sno from student minus select sno from sc where cno = (select o from teacher t, course c where t.tno = c.tno and tname = 张三 a where st.sno = a.sno; 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩select
19、 a.sno, st.sname, a.avg_score from student st, (select sno, count(cno, round(avg(score, 2 avg_score from (select * from sc where score 60 group by sno a where st.sno = a.sno; 16、检索01课程分数小于60,按分数降序排列的学生信息select st.*, o, a.score from student st, (select sno, cno, score from sc where cno = 01 and s
20、core = 60 / (select count(1 from sc where cno = 01 * 100, 2 及格率, round(select count(1 from sc where cno = 01 and score = 70 and score = 80 and score = 90 / (select count(1 from sc where cno = 01 * 100, 2 优秀率 from dual union select 02 cno, round(select count(1 from sc where cno = 02 and score = 60 /
21、(select count(1 from sc where cno = 02 * 100, 2 及格率, round(select count(1 from sc where cno = 02 and score = 70 and score = 80 and score = 90 / (select count(1 from sc where cno = 02 * 100, 2 优秀率 from dual union select 03 cno, round(select count(1 from sc where cno = 03 and score = 60 / (select coun
22、t(1 from sc where cno = 03 * 100, 2 及格率, round(select count(1 from sc where cno = 03 and score = 70 and score = 80 and score 90 / (select count(1 from sc where cno = 03 * 100, 2 优秀率 from dual e where o = o and o = o and o = o and o = o; 19、按各科成绩进行排序,并显示排名select sno, c
23、no, score, rank( over(partition by cno order by score desc 名次 from sc; select sno, cno, score, dense_rank( over(partition by cno order by score desc 名次 from sc; 20、查询学生的总成绩并进行排名20.1 查询学生的总成绩select a.sno, a.sname, nvl(b.sum_score, 0 总成绩 from student a, (select sno, sum(score sum_score from sc group b
24、y sno order by sno b where a.sno = b.sno(+; 20.2 查询学生的总成绩并进行排名。select c.学生编号,c.学生姓名,c.总成绩,rank( over (order by c.总成绩 desc 排名from (select a.sno 学生编号, a.sname 学生姓名, nvl(b.sum_score, 0 总成绩 from student a, (select sno, sum(score sum_score from sc group by sno order by sno b where a.sno = b.sno(+ c 21、查询
25、不同老师所教不同课程平均分从高到低显示select a.tno, a.tname, c.avg_score 平均分 from teacher a, course b, (select cno, round(avg(score, 2 avg_score from sc group by cno c where a.tno = b.tno and o = o order by 平均分 desc; 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩Score 重复时保留名次空缺select * from (select sno, cno, score, rank( over
26、(partition by cno order by score desc order_sc from sc a where a.order_sc in (2, 3; Score 重复时合并名次 select * from (select sno, cno, score, dense_rank( over(partition by cno order by score desc order_sc from sc a where a.order_sc in (2, 3; 23 、 统 计 各 科 成 绩 各 分 数 段 人 数 : 课 程 编 号 , 课 程 名称,100-85,85-70,70
27、-60,0-60及所占百分比23.1 统 计 各 科 成 绩 各 分 数 段 人 数 : 课 程 编 号 , 课 程 名称,100-85,85-70,70-60,0-60select o, ame, b.85-100, b.70-85, b.60-70, b.0-60 from course a, (select 01 cno, (select count(1 from sc where cno = 01 and score = 85 and score = 70 and score = 60 and score 70 60-70, (select count(1 from s
28、c where cno = 01 and score = 85 and score = 70 and score = 60 and score 70 60-70, (select count(1 from sc where cno = 02 and score = 85 and score = 70 and score = 60 and score 70 60-70, (select count(1 from sc where cno = 03 and score 60 0-60 from dual b where o = o; 纵向显示 1(显示存在的分数段; select
29、o, ame, b.ff, b.c1 from course a, (select cno, c1, 0-60 ff from (select cno, count(1 c1 from sc where score = 60 and score = 70 and score = 85 and score = 100 group by cno b where o = o 23.2 统 计 各 科 成 绩 各 分 数 段 人 数 : 课 程 编 号 , 课 程 名称,100-85,85-70,70-60,60及所占百分比横向显示select o, aa.c
30、name, bb.0-60, bb.1百分比(%, bb.60-70, bb.2百分比(%, bb.70-85, bb.3百分比(%, bb.85-100, bb.4百分比(% from course aa, (select 01 cno, (select count(1 from sc where cno = 01 and score 60 0-60, round(select count(1 from sc where cno = 01 and score = 60 and score = 60 and score = 70 and score = 70 and score = 85 an
31、d score = 85 and score = 100 * 100 / (select count(1 from sc where cno = 01, 2 4百分比(% from dual union (select 02 cno, (select count(1 from sc where cno = 02 and score 60 0-60, round(select count(1 from sc where cno = 02 and score = 60 and score = 60 and score = 70 and score = 70 and score = 85 and s
32、core = 85 and score = 100 * 100 / (select count(1 from sc where cno = 02, 2 4百分比(% from dual union (select 03 cno, (select count(1 from sc where cno = 03 and score 60 0-60, round(select count(1 from sc where cno = 03 and score = 60 and score = 60 and score = 70 and score = 70 and score = 85 and scor
33、e = 85 and score = 85 then 85-100 when n.score = 70 and n.score = 60 and n.score = 85 then 85-100 when n.score = 70 and n.score = 60 and n.score 70 then 60-70 else 0-60 end order by o, ame, px t1, (select o, ame, count(1 all_num from course m, sc n where o = o group by o,
34、 ame order by o, ame t2 where o = o 24、 查询学生的平均成绩并进行排名select c.sno, st.sname, c.avg_score, c.paim from student st, (select b.*, rownum paim from (select a.sno, round(avg(score, 2 avg_score from (select s.sno sno, nvl(sc.score, 0 score from student s, sc where s.sno = sc.sno(+ a
35、 group by a.sno order by avg_score desc b c where st.sno = c.sno; 25、查询各科成绩前三名的记录25.1 分数重复时保留名次空缺select st.*, o, d.score from student st, (select a.* from (select * from sc where cno = 01 order by score desc a where rownum = 3 union all select b.* from (select * from sc where cno = 02 order by s
36、core desc b where rownum = 3 union all select c.* from (select * from sc where cno = 03 order by score desc c where rownum = 3 d where st.sno = d.sno 25.2 分数重复时不保留名次空缺,合并名次select st.sno,st.sname,st.sage,st.ssex,o,b.score,b.paimfrom student st,(select sno,cno,score,paim from (select sno, cno, sco
37、re, dense_rank( over(partition by cno order by score desc paim from sc awhere a.paim = 85 a where st.sno = a.sno; 34、查询课程名称为数学,且分数低于60的学生姓名和分数select st.sname,a.scorefrom student st,(select sno, score from sc where cno = (select cno from course where cname = 数学 and score = 70 a where st.sno = a.sno a
38、nd a.sno = sc.sno and o = o 37、查询不及格的课程 select st.*,ame,o,a.scorefrom student st,course c,(select * from sc where score = 80 awhere st.sno = a.snoand o = o 39、求每门课程的学生人数select o,ame,from course c,(select cno,count(sno cn from sc group by cno awhere o = o 40、查询选修张三老师所授课程的学生中,成绩最高的学生信息及其成绩select st.*, ame, o, b.score from student st, course c, (select * from sc where score = (select max(score from (select * from sc where cno = (select cno from course where tno = (select tno from
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二四年体育赛事赞助合同详细条款与权益分配3篇
- 2025年度跨国公司美金贷款合同
- 二零二五年度水稻种植基地建设合同
- 2025版离婚协议书范本:房产买卖合同分割及处理细则4篇
- 2025年度脱硫石膏复合材料销售协议3篇
- 2025年冰箱洗衣机节能补贴项目合作协议3篇
- 2025年度离婚协议书:陈飞与刘婷离婚财产分割及子女抚养费协议4篇
- 二零二五年度老旧小区消防隐患排查与整改承包合同2篇
- 二零二四云存储服务与云原生应用部署合同3篇
- 货物运输协议
- ICU常见药物课件
- CNAS实验室评审不符合项整改报告
- 农民工考勤表(模板)
- 承台混凝土施工技术交底
- 卧床患者更换床单-轴线翻身
- 计量基础知识培训教材201309
- 中考英语 短文填词、选词填空练习
- 一汽集团及各合资公司组织架构
- 阿特拉斯基本拧紧技术ppt课件
- 初一至初三数学全部知识点
- 新课程理念下的班主任工作艺术
评论
0/150
提交评论