上次发表的是写的时候遇到的问题,这次备份一下自己的代码(很冗余,也不保证正确率)
mysql50题链接在此
Oracle重写sql经典50题
- 创建数据库和表
- oracle数据库一个账户就一个库,不需要创建
- 学生表 student
- 创建学生表 student
- 插入学生数据
- 课程表 course
- 创建课程表 course
- 插入课程数据
- 教师表 teacher
- 创建教师表 teacher
- 插入教师数据
- 成绩表 score
- 创建成绩表 score
- 插入成绩数据
- 经典练习 50 题
- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
- 6、查询"李"姓老师的数量
- 7、查询学过"张三"老师授课的同学的信息
- 8、查询没学过"张三"老师授课的同学的信息
- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
- 11、查询没有学全所有课程的同学的信息
- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
- 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
- 19、按各科成绩进行排序,并显示排名
- 20、查询学生的总成绩并进行排名
- 21、查询不同老师所教不同课程平均分从高到低显示
- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
- 23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
- 24、查询学生平均成绩及其名次
- 25、查询各科成绩前三名的记录
- 26、查询每门课程被选修的学生数
- 27、查询出只有两门课程的全部学生的学号和姓名
- 28、查询男生、女生人数
- 29、查询名字中含有"风"字的学生信息
- 30、查询同名同性学生名单,并统计同名人数
- 31、查询1990年出生的学生名单
- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
- 35、查询所有学生的课程及分数情况;
- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
- 37、查询课程不及格的学生
- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
- 39、求每门课程的学生人数
- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
- 42、查询每门功课成绩最好的前两名
- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
- 44、检索至少选修两门课程的学生学号
- 45、查询选修了全部课程的学生信息
- 46、查询各学生的年龄
- 47、查询本周过生日的学生
- 48、查询下周过生日的学生
- 49、查询本月过生日的学生
- 50、查询下月过生日的学生
创建数据库和表
oracle数据库一个账户就一个库,不需要创建
学生表 student
创建学生表 student
create table student (
s_id int,
s_name varchar(8),
s_birth date,
s_sex varchar(4)
);
插入学生数据
注意!!一条一条执行,并提交!!!
insert into Student values(1 , '赵雷' , to_date('1990-01-01','yyyy-MM-dd') , '男');
insert into Student values(2 , '钱电' , to_date('1990-12-21','yyyy-MM-dd') , '男');
insert into Student values(3 , '孙风' , to_date('1990-05-20','yyyy-MM-dd') , '男');
insert into Student values(4 , '李云' , to_date('1990-08-06','yyyy-MM-dd') , '男');
insert into Student values(5 , '周梅' , to_date('1991-12-01','yyyy-MM-dd') , '女');
insert into Student values(6 , '吴兰' , to_date('1992-03-01','yyyy-MM-dd') , '女');
insert into Student values(7 , '郑竹' , to_date('1989-07-01','yyyy-MM-dd') , '女');
insert into Student values(8 , '王菊' , to_date('1990-01-20','yyyy-MM-dd') , '女');
课程表 course
创建课程表 course
create table course (
c_id int,
c_name varchar(8),
t_id int
);
插入课程数据
注意!!一条一条执行,并提交!!!
insert into course values (1,'语文',2);
insert into course values (2,'数学',1);
insert into course values (3,'英语',3);
教师表 teacher
创建教师表 teacher
create table teacher (
t_id int,
t_name varchar(8)
);
插入教师数据
注意!!一条一条执行,并提交!!!
insert into teacher values (1,'张三');
insert into teacher values (2,'李四');
insert into teacher values (3,'王五');
成绩表 score
创建成绩表 score
create table score (
s_id int,
c_id int,
s_score int
);
插入成绩数据
注意!!一条一条执行,并提交!!!
insert into score values (1,1,80);
insert into score values (1,2,90);
insert into score values (1,3,99);
insert into score values (2,1,70);
insert into score values (2,2,60);
insert into score values (2,3,65);
insert into score values (3,1,80);
insert into score values (3,2,80);
insert into score values (3,3,80);
insert into score values (4,1,50);
insert into score values (4,2,30);
insert into score values (4,3,40);
insert into score values (5,1,76);
insert into score values (5,2,87);
insert into score values (6,1,31);
insert into score values (6,3,34);
insert into score values (7,2,89);
insert into score values (7,3,98);
经典练习 50 题
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select stu.*,sc0.s_score from student stu inner join (select sc1.* from score sc1 inner join score sc2 on sc1.s_id
= sc2.s_id and sc1.c_id = 01 and sc2.c_id = 02 where sc1.s_score > sc2.s_score) sc0
on stu.s_id = sc0.s_id;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select stu.*,sc0.s_score from student stu inner join (select sc1.* from score sc1 inner join score sc2 on sc1.s_id
= sc2.s_id and sc1.c_id = 01 and sc2.c_id = 02 where sc1.s_score < sc2.s_score) sc0
on stu.s_id = sc0.s_id;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select stu.s_id,stu.s_name,avg(sc.s_score) as avgscore
from score sc left join student stu on sc.s_id = stu.s_id
group by stu.s_id, stu.s_name having avg(s_score) >= 60 order by s_id;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select stu.s_id,stu.s_name,avg(sc.s_score) as avgscore
from score sc left join student stu on sc.s_id = stu.s_id
group by stu.s_id, stu.s_name having avg(s_score) < 60 order by s_id;
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select stu.s_id,stu.s_name,count(sc.c_id),nvl(sum(sc.s_score),0) sumscore
from student stu left join score sc on stu.s_id = sc.s_id group by stu.s_id, stu.s_name order by stu.s_id;
6、查询"李"姓老师的数量
select count(*) 李姓老师的数量 from teacher where t_name like '李%';
7、查询学过"张三"老师授课的同学的信息
select stu.* from student stu
left join SCORE sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
left join teacher t on c.t_id = t.t_id
where t_name = '张三';
8、查询没学过"张三"老师授课的同学的信息
select stu.* from student stu where stu.s_id not in
(select stu.s_id from student stu
left join SCORE sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
left join teacher t on c.t_id = t.t_id
where t_name = '张三');
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select s.* from score sc1 inner join score sc2 on sc1.s_id = sc2.s_id and sc1.c_id = 01
and sc2.c_id = 02 left join student s on sc1.s_id = s.s_id;
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from student stu where s_id in (select s_id from score where c_id = 01)
and s_id not in (select s_id from score where c_id = 02);
11、查询没有学全所有课程的同学的信息
select stu.* from student stu
left join score sc on stu.s_id = sc.s_id
group by stu.s_id,s_name,s_birth,s_sex
having count(sc.c_id) < (select count(course.c_id) from course)
order by stu.s_id;
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct stu.* from student stu left join score sc on sc.s_id = stu.s_id
where sc.c_id in (select c_id from score where s_id = 01) order by stu.s_id;
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select * from student inner join (select sc1.s_id from score sc1 inner join (select c_id from score where s_id = 1) s1
on sc1.c_id = s1.c_id where s_id != 1
group by sc1.s_id having count(sc1.c_id) = (select count(score.c_id) from score where s_id = 1)) t
on student.s_id = t.s_id ;
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s.* from student s where s.s_id not in (select stu.s_id from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
left join teacher t on c.t_id = t.t_id where t.t_name = '张三');
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select stu.s_id,stu.s_name,avg(sc.s_score) from student stu
left join score sc on stu.s_id = sc.s_id where sc.s_score < 60
group by stu.s_id, stu.s_name having count(sc.c_id) >= 2;
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select stu.* from student stu left join score sc on stu.s_id = sc.s_id
where sc.c_id = 01 and sc.s_score < 60 order by sc.s_score desc;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select stu.s_name 学生姓名,
sum(case sc.c_id when 1 then sc.s_score else 0 end ) 语文,
sum(case sc.c_id when 2 then sc.s_score else 0 end ) 数学,
sum(case sc.c_id when 3 then sc.s_score else 0 end ) 英语,
nvl(avg(sc.s_score),0) avgscore
from student stu left join score sc on stu.s_id = sc.s_id
group by stu.s_name order by avgscore desc ;
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c.c_id 课程ID,c_name 课程name,
max(sc.s_score) 最高分,min(sc.s_score) 最低分,
avg(sc.s_score) 平均分,
sum(case when sc.s_score > 60 then 1 else 0 end ) /count(sc.s_id) 及格率,
sum(case when sc.s_score between 70 and 80 then 1 else 0 end ) /count(sc.s_id) 中等率,
sum(case when sc.s_score between 80 and 90 then 1 else 0 end ) /count(sc.s_id) 优良率,
sum(case when sc.s_score >= 90 then 1 else 0 end ) /count(sc.s_id) 优秀率
from score sc left join course c on sc.c_id = c.c_id group by c.c_id, c_name order by 课程ID;
19、按各科成绩进行排序,并显示排名
select stu.s_name,sc.c_id,sc.s_score,rank() over (partition by sc.c_id order by s_score desc) 排名
from score sc left join student stu on sc.s_id = stu.s_id order by sc.c_id,s_score desc ;
20、查询学生的总成绩并进行排名
select stu.s_name,nvl(sum(sc.s_score),0) sumscore from student stu
left join score sc on stu.s_id = sc.s_id
group by stu.s_name,stu.s_id order by sumscore desc;
21、查询不同老师所教不同课程平均分从高到低显示
select t.t_name,c.c_name,avg(sc.s_score) avgscore from score sc
left join course c on sc.c_id = c.c_id
left join teacher t on c.t_id = t.t_id
group by t.t_name, c.c_name order by avgscore desc ;
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select * from (select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 1 order by sc.s_score desc) where ROWNUM <= 2
union select * from (select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 2 order by sc.s_score desc) where rownum <= 2
union select * from (select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 3 order by sc.s_score desc) where rownum <= 2 ;
23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
select sc.c_id 课程编号,c.c_name 课程名称,
sum(case when s_score between 85 and 100 then 1 else 0 end) "100-85",
sum(case when s_score between 85 and 100 then 1 else 0 end) /count(sc.s_id)*100 "100-85占比",
sum(case when s_score between 70 and 85 then 1 else 0 end) "100-85",
sum(case when s_score between 70 and 85 then 1 else 0 end) /count(sc.s_id)*100 "85-70占比",
sum(case when s_score between 60 and 70 then 1 else 0 end) "100-85",
sum(case when s_score between 60 and 70 then 1 else 0 end) /count(sc.s_id)*100 "70-60占比",
sum(case when s_score between 0 and 60 then 1 else 0 end) "100-85",
sum(case when s_score between 0 and 60 then 1 else 0 end) /count(sc.s_id)*100 "0-60占比"
from score sc left join course c on sc.c_id = c.c_id group by sc.c_id, c.c_name order by 课程编号;
24、查询学生平均成绩及其名次
select sc.s_id,stu.s_name,avg(sc.s_score) avgscore,
rank() over (order by avg(sc.s_score) desc) 排名 from score sc
left join student stu on sc.s_id = stu.s_id
group by stu.s_name,sc.s_id order by avgscore desc ;
25、查询各科成绩前三名的记录
select * from (select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
left join student stu on sc.s_id = stu.s_id
left join course c on sc.c_id = c.c_id where c.c_id = 1
group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc,sc.s_id)
where rownum <= 3
union all select * from (select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
left join student stu on sc.s_id = stu.s_id
left join course c on sc.c_id = c.c_id where c.c_id = 2
group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc,sc.s_id)
where rownum <= 3
union all select * from (select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
left join student stu on sc.s_id = stu.s_id
left join course c on sc.c_id = c.c_id where c.c_id = 3
group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc,sc.s_id)
where rownum <=3 ;
26、查询每门课程被选修的学生数
select c.c_name,count(sc.c_id) 学生数 from score sc
left join course c on sc.c_id = c.c_id group by c.c_name;
27、查询出只有两门课程的全部学生的学号和姓名
select stu.s_id,stu.s_name from score sc
left join student stu on sc.s_id = stu.s_id
group by stu.s_id, stu.s_name having count(sc.c_id) = 2;
28、查询男生、女生人数
select s_sex,count(*) 人数 from student group by s_sex;
29、查询名字中含有"风"字的学生信息
select * from student where s_name like '%风%';
30、查询同名同性学生名单,并统计同名人数
select count(*) 同名同性学生人数 from student stu1
join student stu2 on stu1.s_id=stu2.s_id and stu1.s_sex = stu2.s_sex
and stu1.s_id != stu2.s_id;
31、查询1990年出生的学生名单
select * from student where to_char(s_birth,'yyyy-MM-dd') like '%1990%';
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc.c_id,c.c_name, avg(sc.s_score) avgscore from score sc
left join course c on sc.c_id = c.c_id
group by sc.c_id,c.c_name order by avgscore desc ,sc.c_id asc;
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select stu.s_id, s_name, avg(sc.s_score) avgscore from student stu
left join score sc on stu.s_id = sc.s_id
group by stu.s_id, s_name having avg(sc.s_score) >=85;
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select stu.s_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
where c_name = '数学' and s_score < 60;
35、查询所有学生的课程及分数情况;
select stu.s_id,s_name,
nvl(sum(case sc.c_id when 1 then sc.s_score end ),0) 语文,
nvl(sum(case sc.c_id when 2 then sc.s_score end ),0) 数学,
nvl(sum(case sc.c_id when 3 then sc.s_score end ),0) 英语
from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
group by stu.s_id, s_name order by s_id;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select stu.s_name,c.c_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
where s_score > 70;
37、查询课程不及格的学生
select stu.s_name,c.c_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
where s_score < 60;
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select stu.s_id,stu.s_name from student stu
left join score sc on stu.s_id = sc.s_id
where sc.c_id = 1 and sc.s_score >= 80;
39、求每门课程的学生人数
select c.c_name,count(sc.s_id) 学生人数 from score sc
left join course c on sc.c_id = c.c_id
group by c.c_name;
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select stu.*,select1.maxstu from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
left join
(select c.c_id,max(s_score) maxstu from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
left join teacher t on c.t_id = t.t_id
where t.t_name = '张三' group by c.c_id) select1 on c.c_id = select1.c_id
where sc.s_score = select1.maxstu;
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
Select sc1.s_id,sc1.c_id,sc1.s_score From score sc1 left join
(select sc.s_score from score sc
group by sc.s_score having count(sc.s_score)>1) b on sc1.s_score = b.s_score
Where sc1.s_score = b.s_score;
42、查询每门功课成绩最好的前两名
select * from (select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id where sc.c_id = 1
group by stu.s_id, stu.s_name, c.c_name, sc.s_score
order by sc.s_score desc,s_id) where rownum <= 2
union all select * from (select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id where sc.c_id = 2
group by stu.s_id, stu.s_name, c.c_name, sc.s_score
order by sc.s_score desc,s_id) where rownum <=2
union all select * from (select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
left join score sc on stu.s_id = sc.s_id
left join course c on sc.c_id = c.c_id where sc.c_id = 3
group by stu.s_id, stu.s_name, c.c_name, sc.s_score
order by sc.s_score desc,s_id) where rownum <= 2;
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.c_id,count(sc.s_id) 选修人数 from score sc
group by sc.c_id having count(sc.s_id) > 5 order by 选修人数 desc ,sc.c_id asc;
44、检索至少选修两门课程的学生学号
select s_id from score group by s_id having count(score.c_id) >= 2 order by s_id;
45、查询选修了全部课程的学生信息
select stu.* from student stu
left join score sc on stu.s_id = sc.s_id
group by stu.s_id,s_name,s_birth,s_sex
having count(sc.c_id) = (select count(*) from course) order by stu.s_id;
46、查询各学生的年龄
select s_name,to_char(sysdate,'yyyy')-to_char(s_birth,'yyyy') 年龄 from student;
47、查询本周过生日的学生
select * from student where to_char(s_birth,'Iw') = to_char(sysdate,'Iw');
48、查询下周过生日的学生
select * from student where to_char(s_birth,'Iw') =to_char(sysdate,'Iw')+ 1;
49、查询本月过生日的学生
select * from student where to_char(s_birth,'MM') = to_char(sysdate,'MM');
50、查询下月过生日的学生
select * from student where to_char(s_birth,'MM') = to_char(sysdate,'MM') + 1;
select * from student;