1.5号oracle50题链接在此
drop database if exists mysql_test cascade;
create database mysql_test;
use mysql_test;
create table student (
s_id int,
s_name varchar(8),
s_birth date,
s_sex varchar(4)
);
insert into student values
(1,'赵雷','1990-01-01','男'),
(2,'钱电','1990-12-21','男'),
(3,'孙风','1990-05-20','男'),
(4,'李云','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吴兰','1992-03-01','女'),
(7,'郑竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');
create table course (
c_id int,
c_name varchar(8),
t_id int
);
insert into course values
(1,'语文',2),
(2,'数学',1),
(3,'英语',3);
create table teacher (
t_id int,
t_name varchar(8)
);
insert into teacher values
(1,'张三'),
(2,'李四'),
(3,'王五');
create table score (
s_id int,
c_id int,
s_score int
);
insert into score values
(1,1,80),
(1,2,90),
(1,3,99),
(2,1,70),
(2,2,60),
(2,3,65),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,40),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);
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;
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;
select stu.s_id,stu.s_name,sc.avgscore from student stu
inner join (select score.s_id,avg(s_score) avgscore from score group by s_id having avgscore >= 60) sc
on stu.s_id = sc.s_id;
select stu.s_id,stu.s_name,sc.avgscore from student stu
inner join (select score.s_id,avg(s_score) avgscore from score
group by s_id having avgscore < 60) sc
on stu.s_id = sc.s_id;
select stu.s_id,stu.s_name,count(sc.c_id),ifnull(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;
select count(*) 李姓老师的数量 from teacher where t_name like '李%';
select student.* from student inner join score s on student.s_id = s.s_id
inner join
(select teacher.t_id,c_id from teacher left join course c on teacher.t_id = c.t_id
where t_name = '张三') t on s.c_id = t.c_id;
select stu.* from student stu where stu.s_id not in
(select student.s_id from student inner join score s on student.s_id = s.s_id
inner join
(select teacher.t_id,c_id from teacher left join course c on teacher.t_id = c.t_id
where t_name = '张三') t on s.c_id = t.c_id);
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;
select * from student where s_id in (select s_id from score where score.c_id = 01)
and s_id not in (select s_id from score where score.c_id = 02);
select stu.*,count(sc.c_id) stucount from score sc right join student stu on sc.s_id = stu.s_id
group by stu.s_id, s_name, s_birth, s_sex having stucount < (select count(course.c_id) from course);
select distinct stu.* from student stu left join score sc on stu.s_id = sc.s_id
where sc.c_id in(select sc1.c_id from score sc1 where sc1.s_id = 1);
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 ;
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 = '张三');
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;
select * from student stu left join score sc on stu.s_id = sc.s_id where sc.s_score < 60
and sc.c_id = 1 order by sc.s_score desc;
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 ) 英语,
ifnull(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 ;
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;
select score.*,rank() over (partition by score.c_id order by s_score desc)
from score order by score.c_id,s_score desc ;
select stu.s_name,sum(sc.s_score) 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;
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 ;
(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 limit 2)
union (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 limit 2)
union (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 limit 2) ;
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;
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 ;
(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 limit 3)
union (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 limit 3)
union (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 limit 3);
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;
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;
select s_sex,count(*) 人数 from student group by s_sex;
select * from student where s_name like '%风%';
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;
select * from student where s_birth like '%1990%';
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;
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 avgscore >=85;
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;
select stu.s_id,s_name,
sum(case sc.c_id when 1 then sc.s_score end ) 语文,
sum(case sc.c_id when 2 then sc.s_score end ) 数学,
sum(case sc.c_id when 3 then sc.s_score end ) 英语
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;
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;
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;
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;
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;
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;
select sc1.s_id, sc1.c_id, sc1.s_score from score sc1 join score sc2
where sc1.s_score = sc2.s_score and sc1.s_id != sc2.s_id and sc1.c_id != sc2.c_id;
(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
having max(sc.s_score) order by sc.s_score desc limit 2)
union (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
having max(sc.s_score) order by sc.s_score desc limit 2)
union (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
having max(sc.s_score) order by sc.s_score desc limit 2);
select sc.c_id,count(sc.s_id) 选修人数 from score sc
group by sc.c_id having 选修人数 > 5 order by 选修人数 desc ,sc.c_id asc;
select s_id from score group by s_id having count(score.c_id) >= 2;
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);
select s_name,year(now())-year(s_birth) 年龄 from student
select s_name from student where week(s_birth) = week(now());
select s_name from student where week(s_birth) = week(now()) + 1;
select * from student where month(s_birth) = month(now());
select * from student where month(s_birth) = month(now()) -11;