基础知识
之前看到的很多相关信息都是mysql、sqlserver完成习题的,所以就想做一份oracle完成习题的文档,供自己参考也供别人参考。
用到的表信息
学生信息表
Student(Sid,Sname,Sage,Ssex)
Sid 学生学号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
课程表
Course(Cid,Cname,Tid)
Cid 课程编号,Cname 课程名称,T# 教师编号
教师表
Teacher(Tid,Tname)
Tid 教师编号,Tname 教师姓名
成绩表
SC(Sid,Cid,score)
Sid 学生编号,Cid 课程编号,score 分数
常见问题
1." 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
select a.sid,a.score,b.sid from
(select sid,score from sc where cid1='01')a
join
(select sid,score from sc where cid1='02') b
on a.sid=b.sid and a.score>b.score
2.查询同时存在" 01 “课程和” 02 "课程的情况
select a.sid,b.sid from
(select sid from sc where cid='01' and score is not null) a
join
(select sid from sc where cid='02' and score is not null) b
on (a.sid=b.sid)
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
select a.sid,a.score,b.score from
(select sid,score from sc where cid='01' ) a
left join
(select sid,score from sc where cid='02') b
on (a.sid=b.sid)
4.查询不存在" 01 “课程但存在” 02 "课程的情况
select sid,score from sc where cid='02'
and sid not in (select sid from sc where cid='01' )
5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select a.sid,b.sname,a.score from
(select sid,avg(score) as score from sc group by sid having avg(score)>=60) a
join
(select sid,sname from student ) b
on a.sid =b.sid
6.查询在 SC 表存在成绩的学生信息
select * from student where sid in (select sid from sc)
7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.sid,a.sname,b.cid1,b.score from
(select sid,sname from student )a
left join
(select sid,count(cid) as cid1,sum(score) as score from sc group by sid) b
on a.sid=b.sid
8.查有成绩的学生信息
select * from student where sid in (select sid from sc);
9.查询「李」姓老师的数量
select count(1) from teacher where tname like '李%';
10.查询学过「张三」老师授课的同学的信息
select sid,cid,score from sc
where cid in (select cid from Course where tid in (select tid from teacher where tname='张三'))
11.查询没有学全所有课程的同学的信息
个人观点:分两种情况(选课少于三的,还有一种是没选课的)
select sid from sc
group by sid having count(cid)<(select count(1) from course)
union all
select sid from student where sid not in (select sid from sc)
12.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select ss.sname,sc1.sid from
(select sid from sc where cid in (select cid from sc where sid='01') group by sid) sc1
join
(select sid,sname from student )ss
on (sc1.sid=ss.sid)
13.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
select * from student where sid in (
select sid from sc
where sid <> '01' and cid in (select cid from sc where sid='01')
group by sid having count(cid) =(select count(1) from sc where sid='01'))
14.查询没学过"张三"老师讲授的任一门课程的学生姓名
方法一
select sname from student where sid not in (
select sid from sc where cid in
(select cid from course where tid in (select tid from teacher where tname='张三')))
方法二
select sname from student
where sid not in (
select sid from sc s , course c ,teacher t
where s.cid = c.cid and c.tid=t.tid
and t.tname='张三')
15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select ss.sid ,ss.sname ,sd.score from
(select sid,sname from student ) ss
join
(select sid,avg(score) score from sc where sid in (
select sid from sc where score < 60 group by sid having count(*)>=2
) group by sid ) sd
on ss.sid=sd.sid
16.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select sid ,cid ,score from sc where cid='01' and score<60 order by score desc
17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sid,
sum(case when cid='01' then score else 0 end ) ,
sum(case when cid='02' then score else 0 end ),
sum(case when cid='03' then score else 0 end ),
avg (score)
from sc group by sid
18.查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
--方法一
select a.cid ,g.cname,maxscore,minscore,avgscore,
case when b.aa =0 then 0 else c.aa/b.aa end jgrate,
case when b.aa =0 then 0 else d.aa/b.aa end zdrate,
case when b.aa =0 then 0 else e.aa/b.aa end ylrate,
case when b.aa =0 then 0 else f.aa/b.aa end yxrate from
(select cid,max(score) maxscore ,min(score) minscore, avg(score) avgscore from sc group by cid)a
left join
(select cid, count(1) aa from sc group by cid) b
on a.cid=b.cid
left join
(select cid,count(1) aa from sc where score >= '60' group by cid)c
on a.cid = c.cid
left join
(select cid,count(1) aa from sc where score >= '70' and score < '80' group by cid)d
on a.cid = d.cid
left join
(select cid,count(1) aa from sc where score >= '80' and score < '90' group by cid)e
on a.cid = e.cid
left join
(select cid,count(1) aa from sc where score >= '90' group by cid)f
on a.cid = f.cid
join
(select * from course )g on a.cid = g.cid
--方法二
select
a.cid,b.cname,
max(score),
min(score),
avg(score),
sum(case when score>=60 then 1 else 0 end)/count(*),
sum(case when score>=70 and score<80 then 1 else 0 end)/count(*),
sum(case when score>=80 and score<90 then 1 else 0 end)/count(*),
sum(case when score>=90 then 1 else 0 end)/count(1)
from sc a,course b where a.cid=b.cid
group by a.cid,b.cname
19.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
--重复发生时:1,2,3,3,5,5,7
select sid,cid,score,rank() over (partition by cid order by score desc) as a from sc;
--重复发生时:1,2,3,4,5,6,7
select sid,cid,score,row_number() over (partition by cid order by score desc) as a from sc;
20.按各科成绩进行排序,并显示排名, Score 重复时合并名次
--重复发生时:1,2,3,3,4,4,5
select sid,cid,score,dense_rank() over (partition by cid order by score desc) as a from sc ;
21.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
select sid,sum(score) ,rank() over (order by sum(score) desc) as a from sc group by sid
22.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select sid,sum(score) ,dense_rank() over (order by sum(score) desc) as a from sc group by sid;
23.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select
a.cid,b.cname,
sum(case when score>=85 and score<=100 then 1 else 0 end)/count(*),
sum(case when score>=70 and score<=85 then 1 else 0 end)/count(*),
sum(case when score>=60 and score<=70 then 1 else 0 end)/count(*),
sum(case when score>=0 and score<=60 then 1 else 0 end)/count(1)
from sc a,course b where a.cid=b.cid
group by a.cid,b.cname
24.查询各科成绩前三名的记录
select sid,cid,score,a from (
select sid,cid,score,row_number() over (partition by cid order by score desc) as a from sc
)where a<=3;
25.查询每门课程被选修的学生数
select cid,count(1) from sc group by cid
26.查询出只选修两门课程的学生学号和姓名
select sid,sname from student where sid in(
select sid,count(1) from sc group by sid having count(*)=2);
27.查询男生、女生人数
select ssex,count(1) from student group by ssex
28.查询名字中含有「风」字的学生信息
select sid,ssex,sname from student where sname like '%风%'
29.查询同名同性学生名单,并统计同名人数
select sname,count(1) from student group by sname having count(*)>1
30.查询 1990 年出生的学生名单
select * from student where substr(sage,1,4)='1990'
31.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select cid , avg(score) , row_number() over(order by avg(score) desc,cid asc) from sc group by cid
32.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select st.sid,st.sname,sd.score from
(select * from student) st
join(select sid,avg(score) as score from sc group by sid having avg(score)>=85)sd
on st.sid=sd.sid
33.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select st.sname,sd.score from (select * from student) st join
(select sid,score from sc where cid in (select cid from course where cname='数学') and score<60)sd
on st.sid=sd.sid
34.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
--理解一
select
a.sid,a.sname,
sum(case when b.cid='01' then score else 0 end ),
sum(case when b.cid='02' then score else 0 end ),
sum(case when b.cid='03' then score else 0 end )
from student a
left outer join sc b
on a.sid=b.sid
group by a.sid,a.sname
--理解二
select a.sid,a.sname,b.cid,b.score from
student a left outer join sc b
on (a.sid =b.sid)
35.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select b.sid,a.sname,c.cname,b.score from student a ,sc b, course c
where a.sid=b.sid and b.cid=c.cid and b.sid in (select sid from sc where score>70 group by sid)
36.查询不及格的课程
select * from sc where score<60
37.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select sid,sname from student sid in (
select sid from sc where cid='01' and score>80)
38.求每门课程的学生人数
select cid,count(1) from sc group by cid
39.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select * from
(select * from student) c join
(select sid,score from sc where cid in (select cid from course c,teacher t where c.tid=t.tid and tname='张三') )a
on a.sid =c.sid join
(select max(score) score from sc where cid in (select cid from course c,teacher t where c.tid=t.tid and tname='张三') ) b
on (a.score = b.score)
40.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select a.sid,a.sname,b.maxscore from
(select * from student) a left join
(select sid,max(score) maxscore,rank() over(order by max(score) desc) as numb
from sc where cid in (select cid from course c,teacher t where c.tid=t.tid and tname='张三') group by sid)b
on a.sid = b.sid where b.numb=1
41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select
sid ,cid,score
from sc where score in( select score from sc group by score having count(*)>1 and count(cid)>1)
42.查询每门功课成绩最好的前两名
select sid,cid,score,a from (
select sid,cid,score,row_number() over(partition by cid order by score desc)
as a from sc ) where a<=2
43.统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select cid,count(1) from sc group by cid having count(*)>5
44.检索至少选修两门课程的学生学号
select sid ,count(1) from sc group by sid having count(*)>=2
45.查询选修了全部课程的学生信息
select sid ,count(1) from sc group by sid having count(*)=(select count(1) from course)
46.查询各学生的年龄,只按年份来算
–获取年份 extract(year from (时间字段)) - extract(year from (时间字段))
select sid,sname,sage,ssex,EXTRACT(year FROM(sysdate))-EXTRACT(year FROM to_date(sage,'yyyy-mm-dd')) from student
47.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select sid,sname,sage,ssex,
case when to_date(to_char(sysdate,'mm-dd'),'mm-dd')
48.查询本周过生日的学生
select * from student where to_date(substr(sage,6,10),'mm-dd')>= trunc(next_day(sysdate-8,1)+1)
and to_date(substr(sage,6,10),'mm-dd') < trunc(next_day(sysdate-8,1)+7)+1;
49.查询下周过生日的学生
select * from student where to_date(substr(sage,6,10),'mm-dd')>= trunc(next_day(sysdate-8,1)+8)
and to_date(substr(sage,6,10),'mm-dd') < trunc(next_day(sysdate-8,1)+14)+1;
50.查询本月过生日的学生
select * from student where extract(month from(sysdate)) = extract(month from to_date(sage,'yyyy-mm-dd'))
51.查询下月过生日的学生
select * from student where (extract(month from(sysdate))+1) = extract(month from to_date(sage,'yyyy-mm-dd'))