----------------------50进阶题------------------------------
--1查询" 10 "课程比" 33 "课程成绩高的学生的信息及课程分数
select student.* ,a.sc_score from student
join (select * from sc where sc_course_id=10) a
on student.student_id=a.sc_student_id
join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id
where a.sc_score>b.sc_score;
--1.1 查询同时存在" 10 "课程和" 33 "课程的情况
select a.sc_student_id,a.sc_course_id,a.sc_score,b.sc_course_id,b.sc_score
from
(select * from sc where sc_course_id=10) a
join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id;
--1.2 查询存在" 10 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null )
select a.*,b.*
from (select * from sc where sc_course_id=10) a
left join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id ;
--1.3 查询不存在" 10 "课程但存在" 33 "课程的情况
select a.*,b.*
from (select * from sc where sc_course_id=33) a
left join (select * from sc where sc_course_id=10) b
on a.sc_student_id=b.sc_student_id ;
--2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select student.student_id,student_name ,sc_score from
(select sc_student_id,sc_score,avg(sc_score)from sc
group by sc_student_id,sc_score) avg
left join student on avg.sc_student_id=student.student_id
where avg.sc_score>60;
--
select sc_student_id,student.student_name,sc_score from student join
(
select sc_student_id,sc_score from sc
group by sc_student_id,sc_score
having sc_score>60
) b
on student.student_id=b.sc_student_id;
--3. 查询在 SC 表存在成绩的学生信息
select student_id,sc_score from student
right join sc
on sc.sc_student_id=student.student_id;
--4. 查询所有同学的学生编号、学生姓名、
--选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.*,student_name from
(select sc_student_id,count(sc_course_id) c,sum(sc_score) s
from student left join sc on student.student_id=sc_student_id
group by sc_student_id,student_name
) a
right join student on a.sc_student_id=student.student_id;
--4.1 查有成绩的学生信息
select student.*,sc.sc_score
from sc join student on sc.sc_student_id=student.student_id;
--5. 查询「李」姓老师的数量
select count(teacher_id) from teacher
where teacher_name like '李%';
--6. 查询学过「张三」老师授课的同学的信息
select student.* from student where student_id in
( select sc_student_id from sc where sc_course_id in
(select course_id from course where course_teacher_id in
(select teacher_id from teacher
where teacher_name='赵四')))
;
--7. 查询没有学全所有课程的同学的信息
select student.* from student right join
(select student_id from sc
right join student on sc.sc_student_id=student.student_id
group by student_id
having count(sc_course_id)<(select count(course_id)from course))a
on a.student_id=student.student_id
order by student.student_id
;
--8. 查询至少有一门课与学号为" 1001 "的同学所学相同的课程的同学的信息
select sc_student_id ,sc_course_id from sc where sc_course_id in
(select sc_course_id from sc where sc_student_id='1001');
--9. 查询和" 1008 "号的同学学习的课程完全相同的其他同学的信息
select sc_student_id from sc
where sc_course_id in
(select distinct sc_course_id from sc where sc_student_id='1008')
group by sc_student_id
having count(sc_course_id)>=2;
--10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select student.* from student where student_id not in
( select sc_student_id from sc where sc_course_id in
(select course_id from course where course_teacher_id in
(select teacher_id from teacher
where teacher_name='赵四')));
--11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student_id,student_name,a.* from student
join (select sc_student_id,avg(sc_score)
from sc where sc_score>60
group by sc_student_id
having count(sc_score)>=2)a
on a.sc_student_id=student.student_id
;
--12. 检索" 10 "课程分数小于 60,按分数降序排列的学生信息
select sc_student_id,sc_score from sc
where sc_score<60
order by sc_score desc ;
--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc_score,sc.sc_course_id,a.* from
(
select sc_student_id,avg(sc_score) from sc
group by sc_student_id
)a
join sc on a.sc_student_id=sc.sc_student_id
order by sc_score desc;
--14. 查询各科成绩最高分、最低分和平均分:
--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列a
select sc_course_id,max(sc_score)"最大值",min(sc_score)"最小值",avg(sc_score)"平均分",
(
(select count(*)from sc where sc_score>60)/(select count(*)from sc )
) "及格率"
from sc
group by sc_course_id
order by sc_course_id;
--15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
--16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
select a.sc_student_id,a.总成绩,rank() over(order by 总成绩 desc )排名 from
(select sc.sc_student_id,sum(sc_score)总成绩 from sc group by sc_student_id)a;
--(未完成)17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select
sc_course_id "课程编号",
course_name "课程名称",
(
(select count(*)from sc where sc_score>=85 and sc_score<=100)
/(select count(*)from sc )
)"[100-85]"
from sc join course on sc. sc_course_id=course.course_id
;
--18. 查询各科成绩前三名的记录
select * from sc a
where (
select count(*)from sc
where a.sc_course_id=sc_course_id and sc_score>a.sc_score)<3
order by sc_course_id,sc_student_id
;
--19. 查询每门课程被选修的学生数
select sc_course_id,count(sc_student_id) from sc
group by sc_course_id;
--20. 查询出只选修两门课程的学生学号和姓名
select student_id,student_name from student join
(select sc_student_id from sc
group by sc_student_id
having count(sc_course_id)=2)a
on student.student_id=a.sc_student_id;
--21. 查询男生、女生人数
select student_sex, count(*) from student
group by student_sex;
--22. 查询名字中含有「花」字的学生信息
select student_name from student
where student_name like '%花%';
--23. 查询同名同性学生名单,并统计同名人数
select student_name,student_sex,count(*)"人数" from student
group by student_name,student_sex
having count(*)>1;
--24. 查询 1996 年出生的学生名单 to_char(date,'YYYY')--年
select * from student
where to_char(student_age, 'YYYY')='1996';
--25. 查询每门课程的平均成绩,
--结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc_course_id,avg(sc_score) from sc
group by sc_course_id
order by avg(sc_score)desc ,sc_course_id asc;
--26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select student_id,student_name,avg(sc_score)
from sc
join student on student.student_id=sc_student_id
group by student_id,student_name
having avg(sc_score)>=85;
--27. 查询课程名称为「数学」,且分数高于 60 的学生姓名和分数
select student_name,sc_score from student join
(select sc.sc_student_id,sc_score from sc
where sc_score>60 and
sc_course_id in
( select course_id from course where course_name='体育'))a
on a.sc_student_id=student.student_id;
--28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select student_id,sc_course_id,sc.sc_score from sc
right join student on student.student_id=sc.sc_student_id
order by sc_student_id,sc.sc_course_id;
--29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select student_name,course_name,sc_score
from student,sc,course
where sc.sc_student_id=student.student_id
and sc.sc_course_id=course.course_id
group by student_name,course_name,sc_score
having sc_score>70;
--30. 查询不及格的课程
select * from sc where sc_score<60;
--31. 查询课程编号为 32 且课程成绩在 60 分以上的学生的学号和姓名
select student_id ,student_name
from student,sc
where sc.sc_student_id=student.student_id
and sc_course_id=32 and sc_score>60;
--32. 求每门课程的学生人数
select sc_course_id, count(sc_student_id) from sc
group by sc_course_id;
--33. 成绩不重复,查询选修「吴石」老师所授课程的学生中,成绩最高的学生信
select sc_course_id,max(sc_score)from sc,teacher,course
where sc.sc_course_id=course.course_id
and teacher.teacher_id=course.course_teacher_id
and teacher.teacher_name='吴石'
group by sc_course_id;
--35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select s1.sc_student_id,s1.sc_course_id,s1.sc_score
from sc s1 join sc s2
on s1.sc_student_id=s2.sc_student_id
where s1.sc_score=s2.sc_score
order by s2.sc_score asc;
--36. 查询每门功成绩最好的前两名
select * from
(select sc.*,ROW_NUMBER()over(partition by sc_course_id order by sc_score desc)A
from SC) b
where b.A<3;
--37. 统计每门课程的学生选修人数(超过 3人的课程才统计)。
select sc_course_id,count(sc_student_id)from sc
group by sc_course_id
having count(sc_student_id)>3;
--38. 检索至少选修两门课程的学生学号
select sc_student_id,count(sc_course_id) from sc
group by sc_student_id
having count(sc_course_id)>=2;
--39. 查询选修了全部课程的学生信息
select sc_student_id from sc
group by sc_student_id
having count(sc_course_id)=(
select count(*) from sc
);
--40. 查询各学生的年龄,只按年份来算
select student_name,
(to_char(sysdate,'YYYY')-to_char(student_age,'YYYY'))
from student;
--41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select student_name
from student
where to_char(sysdate,'dd')<to_char(student_age,'dd')
and to_char(sysdate,'mm')<to_char(student_age,'mm');
--42. 查询今天过生日的学生
select student_name
from student
where (to_char(sysdate,'dd')-to_char(student_age,'dd'))=0
and (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--43. 查询明天过生日的学生
select student_name
from student
where (to_char(sysdate,'dd')-to_char(student_age,'dd'))=-1
and (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--44. 查询本月过生日的学生
select student_name
from student
where (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--45. 查询下月过生日的学生
select student_name
from student
where (to_char(sysdate,'mm')-to_char(student_age,'mm'))=-1;
--1查询" 10 "课程比" 33 "课程成绩高的学生的信息及课程分数
select student.* ,a.sc_score from student
join (select * from sc where sc_course_id=10) a
on student.student_id=a.sc_student_id
join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id
where a.sc_score>b.sc_score;
--1.1 查询同时存在" 10 "课程和" 33 "课程的情况
select a.sc_student_id,a.sc_course_id,a.sc_score,b.sc_course_id,b.sc_score
from
(select * from sc where sc_course_id=10) a
join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id;
--1.2 查询存在" 10 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null )
select a.*,b.*
from (select * from sc where sc_course_id=10) a
left join (select * from sc where sc_course_id=33) b
on a.sc_student_id=b.sc_student_id ;
--1.3 查询不存在" 10 "课程但存在" 33 "课程的情况
select a.*,b.*
from (select * from sc where sc_course_id=33) a
left join (select * from sc where sc_course_id=10) b
on a.sc_student_id=b.sc_student_id ;
--2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select student.student_id,student_name ,sc_score from
(select sc_student_id,sc_score,avg(sc_score)from sc
group by sc_student_id,sc_score) avg
left join student on avg.sc_student_id=student.student_id
where avg.sc_score>60;
--
select sc_student_id,student.student_name,sc_score from student join
(
select sc_student_id,sc_score from sc
group by sc_student_id,sc_score
having sc_score>60
) b
on student.student_id=b.sc_student_id;
--3. 查询在 SC 表存在成绩的学生信息
select student_id,sc_score from student
right join sc
on sc.sc_student_id=student.student_id;
--4. 查询所有同学的学生编号、学生姓名、
--选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.*,student_name from
(select sc_student_id,count(sc_course_id) c,sum(sc_score) s
from student left join sc on student.student_id=sc_student_id
group by sc_student_id,student_name
) a
right join student on a.sc_student_id=student.student_id;
--4.1 查有成绩的学生信息
select student.*,sc.sc_score
from sc join student on sc.sc_student_id=student.student_id;
--5. 查询「李」姓老师的数量
select count(teacher_id) from teacher
where teacher_name like '李%';
--6. 查询学过「张三」老师授课的同学的信息
select student.* from student where student_id in
( select sc_student_id from sc where sc_course_id in
(select course_id from course where course_teacher_id in
(select teacher_id from teacher
where teacher_name='赵四')))
;
--7. 查询没有学全所有课程的同学的信息
select student.* from student right join
(select student_id from sc
right join student on sc.sc_student_id=student.student_id
group by student_id
having count(sc_course_id)<(select count(course_id)from course))a
on a.student_id=student.student_id
order by student.student_id
;
--8. 查询至少有一门课与学号为" 1001 "的同学所学相同的课程的同学的信息
select sc_student_id ,sc_course_id from sc where sc_course_id in
(select sc_course_id from sc where sc_student_id='1001');
--9. 查询和" 1008 "号的同学学习的课程完全相同的其他同学的信息
select sc_student_id from sc
where sc_course_id in
(select distinct sc_course_id from sc where sc_student_id='1008')
group by sc_student_id
having count(sc_course_id)>=2;
--10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select student.* from student where student_id not in
( select sc_student_id from sc where sc_course_id in
(select course_id from course where course_teacher_id in
(select teacher_id from teacher
where teacher_name='赵四')));
--11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student_id,student_name,a.* from student
join (select sc_student_id,avg(sc_score)
from sc where sc_score>60
group by sc_student_id
having count(sc_score)>=2)a
on a.sc_student_id=student.student_id
;
--12. 检索" 10 "课程分数小于 60,按分数降序排列的学生信息
select sc_student_id,sc_score from sc
where sc_score<60
order by sc_score desc ;
--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc_score,sc.sc_course_id,a.* from
(
select sc_student_id,avg(sc_score) from sc
group by sc_student_id
)a
join sc on a.sc_student_id=sc.sc_student_id
order by sc_score desc;
--14. 查询各科成绩最高分、最低分和平均分:
--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列a
select sc_course_id,max(sc_score)"最大值",min(sc_score)"最小值",avg(sc_score)"平均分",
(
(select count(*)from sc where sc_score>60)/(select count(*)from sc )
) "及格率"
from sc
group by sc_course_id
order by sc_course_id;
--15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
--16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
select a.sc_student_id,a.总成绩,rank() over(order by 总成绩 desc )排名 from
(select sc.sc_student_id,sum(sc_score)总成绩 from sc group by sc_student_id)a;
--(未完成)17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select
sc_course_id "课程编号",
course_name "课程名称",
(
(select count(*)from sc where sc_score>=85 and sc_score<=100)
/(select count(*)from sc )
)"[100-85]"
from sc join course on sc. sc_course_id=course.course_id
;
--18. 查询各科成绩前三名的记录
select * from sc a
where (
select count(*)from sc
where a.sc_course_id=sc_course_id and sc_score>a.sc_score)<3
order by sc_course_id,sc_student_id
;
--19. 查询每门课程被选修的学生数
select sc_course_id,count(sc_student_id) from sc
group by sc_course_id;
--20. 查询出只选修两门课程的学生学号和姓名
select student_id,student_name from student join
(select sc_student_id from sc
group by sc_student_id
having count(sc_course_id)=2)a
on student.student_id=a.sc_student_id;
--21. 查询男生、女生人数
select student_sex, count(*) from student
group by student_sex;
--22. 查询名字中含有「花」字的学生信息
select student_name from student
where student_name like '%花%';
--23. 查询同名同性学生名单,并统计同名人数
select student_name,student_sex,count(*)"人数" from student
group by student_name,student_sex
having count(*)>1;
--24. 查询 1996 年出生的学生名单 to_char(date,'YYYY')--年
select * from student
where to_char(student_age, 'YYYY')='1996';
--25. 查询每门课程的平均成绩,
--结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc_course_id,avg(sc_score) from sc
group by sc_course_id
order by avg(sc_score)desc ,sc_course_id asc;
--26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select student_id,student_name,avg(sc_score)
from sc
join student on student.student_id=sc_student_id
group by student_id,student_name
having avg(sc_score)>=85;
--27. 查询课程名称为「数学」,且分数高于 60 的学生姓名和分数
select student_name,sc_score from student join
(select sc.sc_student_id,sc_score from sc
where sc_score>60 and
sc_course_id in
( select course_id from course where course_name='体育'))a
on a.sc_student_id=student.student_id;
--28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select student_id,sc_course_id,sc.sc_score from sc
right join student on student.student_id=sc.sc_student_id
order by sc_student_id,sc.sc_course_id;
--29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select student_name,course_name,sc_score
from student,sc,course
where sc.sc_student_id=student.student_id
and sc.sc_course_id=course.course_id
group by student_name,course_name,sc_score
having sc_score>70;
--30. 查询不及格的课程
select * from sc where sc_score<60;
--31. 查询课程编号为 32 且课程成绩在 60 分以上的学生的学号和姓名
select student_id ,student_name
from student,sc
where sc.sc_student_id=student.student_id
and sc_course_id=32 and sc_score>60;
--32. 求每门课程的学生人数
select sc_course_id, count(sc_student_id) from sc
group by sc_course_id;
--33. 成绩不重复,查询选修「吴石」老师所授课程的学生中,成绩最高的学生信
select sc_course_id,max(sc_score)from sc,teacher,course
where sc.sc_course_id=course.course_id
and teacher.teacher_id=course.course_teacher_id
and teacher.teacher_name='吴石'
group by sc_course_id;
--35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select s1.sc_student_id,s1.sc_course_id,s1.sc_score
from sc s1 join sc s2
on s1.sc_student_id=s2.sc_student_id
where s1.sc_score=s2.sc_score
order by s2.sc_score asc;
--36. 查询每门功成绩最好的前两名
select * from
(select sc.*,ROW_NUMBER()over(partition by sc_course_id order by sc_score desc)A
from SC) b
where b.A<3;
--37. 统计每门课程的学生选修人数(超过 3人的课程才统计)。
select sc_course_id,count(sc_student_id)from sc
group by sc_course_id
having count(sc_student_id)>3;
--38. 检索至少选修两门课程的学生学号
select sc_student_id,count(sc_course_id) from sc
group by sc_student_id
having count(sc_course_id)>=2;
--39. 查询选修了全部课程的学生信息
select sc_student_id from sc
group by sc_student_id
having count(sc_course_id)=(
select count(*) from sc
);
--40. 查询各学生的年龄,只按年份来算
select student_name,
(to_char(sysdate,'YYYY')-to_char(student_age,'YYYY'))
from student;
--41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select student_name
from student
where to_char(sysdate,'dd')<to_char(student_age,'dd')
and to_char(sysdate,'mm')<to_char(student_age,'mm');
--42. 查询今天过生日的学生
select student_name
from student
where (to_char(sysdate,'dd')-to_char(student_age,'dd'))=0
and (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--43. 查询明天过生日的学生
select student_name
from student
where (to_char(sysdate,'dd')-to_char(student_age,'dd'))=-1
and (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--44. 查询本月过生日的学生
select student_name
from student
where (to_char(sysdate,'mm')-to_char(student_age,'mm'))=0;
--45. 查询下月过生日的学生
select student_name
from student
where (to_char(sysdate,'mm')-to_char(student_age,'mm'))=-1;