sql总是学完就忘,初级、进阶,课课学,课课忘,总的来说如果不多实践就会忘记。
在网上找练习做,这个套题是最火的了。做了两遍感觉以前的sql都白学了,从来就没这么熟练过。
新手小白们信我,多练才是法门,写完两遍感觉腰板都直了。
建表
总计涉及四张表:student,course ,teacher score
create table student(
s_id varchar(20),
s_name varchar(20) not null default'',
s_birth varchar(20) not null default'',
s_sex varchar(10) not null default'',
primary key(s_id)
);
create table `course`(
`c_id`varchar(20),
`c_name`varchar(20) not null default'',
`t_id`varchar(20)not null,
primary key(`c_id`)
);
create table `teacher`(
`t_id`varchar(20),
`t_name`varchar(20)not null default'',
primary key(`t_id`)
);
create table `score`(
`s_id` varchar(20),
`c_id`varchar(20),
`s_score`int(3),
primary key(`s_id`,`c_id`)
);
插入数据
insert into student values
('01','赵雷','1991-01-01','男'),
('02' , '钱电' , '1990-12-21' , '男');
('03' , '孙风' , '1990-05-20' , '男'),
('04' , '李云' , '1990-08-06' , '男'),
('05' , '周梅' , '1991-12-01' , '女'),
('06' , '吴兰' , '1992-03-01' , '女'),
('07' , '郑竹' , '1989-07-01' , '女'),
('08' , '王菊' , '1990-01-20' , '女');
insert into course values
('01' , '语文' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');
insert into teacher values
('01' , '张三'),
('02' , '李四'),
('03' , '王五');
insert into score values
('01' , '01' , 80),
('01' , '02' , 90),
('01' , '03' , 99),
('02' , '01' , 70),
('02' , '02' , 60),
('02' , '03' , 80),
('03' , '01' , 80),
('03' , '02' , 80),
('03' , '03' , 80),
('04' , '01' , 50),
('04' , '02' , 30),
('04' , '03' , 20),
('05' , '01' , 76),
('05' , '02' , 87),
('06' , '01' , 31),
('06' , '03' , 34),
('07' , '02' , 89),
('07' , '03' , 98);
正式开始
– 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select st.*,sc.s_score 01score,sc1.s_score 02score from score sc left join score sc1 on sc.s_id=sc1.s_id
left join student st on st.s_id=sc.s_id
where sc.c_id='01' and sc1.c_id='02' and sc.s_score>sc1.s_score;
– 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select st.*,sc.s_score 01score,sc1.s_score 02score from score sc left join score sc1 on sc.s_id=sc1.s_id
left join student st on st.s_id=sc.s_id
where sc.c_id='01' and sc1.c_id='02' and sc.s_score<sc1.s_score;
– 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select st.*,round(avg(sc.s_score),2) from student st
left join score sc on st.s_id=sc.s_id
group by st.s_id
having avg(sc.s_score)>=60;
– 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select st.*,round(avg(sc.s_score),2) from student st
left join score sc on st.s_id=sc.s_id
group by st.s_id
having avg(sc.s_score)<60 or avg(sc.s_score) is null;
– 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select st.s_id,st.s_name,count(1),round(
case when sum(sc.s_score)is null then 0
else sum(sc.s_score)
end)
from student st
left join score sc on st.s_id=sc.s_id
group by st.s_id;
– 6、查询"李"姓老师的数量
select count(1)from teacher where t_name like'%李%';
– 7、查询学过"张三"老师授课的同学的信息
select st.*from student st
left join score sc on sc.s_id=st.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='张三';
– 8、查询没学过"张三"老师授课的同学的信息
select *from student ss where ss.s_id not in (
select st.s_id from student st
left join score sc on sc.s_id=st.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='张三');
– 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select*from student ss where ss.s_id in (
select st.s_id from score sc left join score sc1 on sc.s_id=sc1.s_id
left join student st on st.s_id=sc.s_id
where sc.c_id='01' and sc1.c_id='02' );
– 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select*from student st where st.s_id in
(select sc.s_id from score sc where sc.c_id='01')
and st.s_id not in
(select sc1.s_id from score sc1 where sc1.c_id='02');
– 11、查询没有学全所有课程的同学的信息
select st.*from student st
left join score sc on st.s_id=sc.s_id
group by st.s_id
having count(1)!=3;
– 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct st.* from score sc1
left join student st on st.s_id=sc1.s_id
where sc1.c_id in
(select sc.c_id from score sc where sc.s_id='01');
– 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select*from student st where st.s_id in (
select sc.s_id from score sc group by sc.s_id having group_concat(sc.c_id)=
(select group_concat(sc1.c_id) from score sc1 where sc1.s_id='01' )
);
select*from student st where st.s_id in (
select sc.s_id from score sc group by sc.s_id having concat(sc.c_id)=
(select concat(sc1.c_id) from score sc1 where sc1.s_id='01' )
);
– 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select st.s_id,st.s_name from student st where st.s_id
not in(
select sc.s_id 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
where t.t_name='张三');
– 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.s_id,st.s_name,
(select avg(sc1.s_score)from score sc1
where sc.s_id=sc1.s_id group by sc1.s_id )
from score sc
left join student st on st.s_id=sc.s_id
where sc.s_score<60
group by sc.s_id
having count(1)>=2;
– 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select st.*,sc.s_score from student st
left join score sc on st.s_id=sc.s_id
where sc.c_id='01'and sc.s_score<60
order by sc.s_score desc;
– 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc1.*,round(a.平均分) from score sc1 left join
(select sc.s_id,avg(sc.s_score)'平均分'from score sc
group by sc.s_id)a on sc1.s_id=a.s_id
order by 平均分 desc,sc1.s_id,sc1.s_score desc;
– 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c.c_id,c.c_name,max(sc.s_score)'最高分',min(sc.s_score)'最低分',avg(sc.s_score)'平均分'
,((select count(1) from score sc where sc.s_score >=60 and sc.c_id=c.c_id)/(select count(1) from score sc where sc.c_id=c.c_id))'及格率'
,((select count(1) from score sc where sc.s_score>=70 and sc.s_score <80 and sc.c_id=c.c_id)/(select count(1) from score sc where sc.c_id=c.c_id))'中等率'
,((select count(1) from score sc where sc.s_score>=80 and sc.s_score <90 and sc.c_id=c.c_id)/(select count(1) from score sc where sc.c_id=c.c_id))'优良率'
,((select count(1) from score sc where sc.s_score>=90 and sc.c_id=c.c_id)/(select count(1) from score sc where sc.c_id=c.c_id))'优秀率'
from score sc
left join course c on sc.c_id=c.c_id
group by sc.c_id;
– 19、按各科成绩进行排序,并显示排名(实现不完全)
select c.*,@i:=@i+1 from (select*from score sc where sc.c_id='01' order by sc.s_score desc)c,
(select @i:=0)b
union
select c.*,@ii:=@ii+1 from (select*from score sc where sc.c_id='02' order by sc.s_score desc)c,
(select @ii:=0)b
union
select c.*,@iii:=@iii+1 from (select*from score sc where sc.c_id='03' order by sc.s_score desc)c,
(select @iii:=0)b;
– 20、查询学生的总成绩并进行排名
select @a:=0;
select aa.*,@a:=@a+1 from
(select sc.s_id,sum(sc.s_score)from score sc
group by sc.s_id
order by sum(sc.s_score) desc)aa;
show errors;
– 21、查询不同老师所教不同课程平均分从高到低显示
select t.t_id,t.t_name,avg(sc.s_score) from teacher t
left join course c on t.t_id=c.t_id
left join score sc on c.c_id=sc.c_id
group by t.t_name
order by avg(sc.s_score) desc;
– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select sc1.c_id,sc1.s_id,st.s_name,sc1.s_score from score sc1
left join score sc11 on sc1.c_id=sc11.c_id
left join student st on sc1.s_id=st.s_id
where sc1.s_score>=sc11.s_score
group by sc1.c_id,sc1.s_id
having count(*)>3 and count(*)<6
order by sc1.c_id,count(*) desc;
– 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select sc.c_id,c.c_name
,(select count(1) from score sc1 where sc1.s_score<60 and sc1.c_id=sc.c_id)/count(1)'[0,60]'
,(select count(1) from score sc1 where sc1.s_score<70 and sc1.s_score>=60 and sc1.c_id=sc.c_id)/count(1)'[60,70]'
,(select count(1) from score sc1 where sc1.s_score<85 and sc1.s_score>=70 and sc1.c_id=sc.c_id )/count(1)'[70,85]'
,(select count(1) from score sc1 where sc1.s_score>=85 and sc1.c_id=sc.c_id)/count(1)'[85,100]'
from score sc
left join course c on sc.c_id=c.c_id
group by sc.c_id;
– 24、查询学生平均成绩及其名次
select a.*,@i:=@i+1 from
(select sc.s_id,avg(sc.s_score) from score sc
left join student st on sc.s_id=st.s_id
group by sc.s_id
order by avg(sc.s_score)desc)a,
(select @i:=0)b;
– 25、查询各科成绩前三名的记录
select sc1.c_id,sc1.s_id,st.s_name,sc1.s_score from score sc1
left join score sc11 on sc1.c_id=sc11.c_id
left join student st on sc1.s_id=st.s_id
where sc1.s_score>=sc11.s_score
group by sc1.c_id,sc1.s_id
having count(*)>3 and count(*)<=6
order by sc1.c_id,count(*) desc;
– 26、查询每门课程被选修的学生数
select c.c_id,c.c_name,count(1) from score sc
left join course c on sc.c_id=c.c_id
group by sc.c_id;
– 27、查询出只有两门课程的全部学生的学号和姓名
select sc.s_id,st.s_name from score sc
left join student st on st.s_id=sc.s_id
group by sc.s_id having count(1)=2;
– 28、查询男生、女生人数
select st.s_sex ,count(1)from student st group by st.s_sex;
– 29、查询名字中含有"风"字的学生信息
select*from student st where st.s_name like'%风%';
– 30、查询同名同性学生名单,并统计同名人数
select*from student st left join student st1 on st.s_name=st1.s_name
group by st.s_id
having count(1)>1;
– 31、查询1990年出生的学生名单
select*from student where s_birth like '%1990%';
– 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc.c_id,avg(sc.s_score)from score sc group by sc.c_id
order by avg(sc.s_score) desc,sc.c_id;
– 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select sc.s_id,st.s_name,avg(sc.s_score)from score sc
left join student st on st.s_id=sc.s_id
group by sc.s_id having avg(sc.s_score)>=85;
– 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select st.s_name,sc.s_score from score sc
left join course c on sc.c_id=c.c_id
left join student st on sc.s_id=st.s_id
where c.c_name ='数学' and sc.s_score<60;
– 35、查询所有学生的课程及分数情况;
select st.s_name,c.c_name,sc.s_score from student st
left join score sc on st.s_id=sc.s_id
left join course c on c.c_id=sc.c_id;
– 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select st.s_name,c.c_name,sc.s_score from score sc
left join student st on sc.s_id=st.s_id
left join course c on sc.c_id=c.c_id
where sc.s_score>70;
– 37、查询不及格的课程
select*from score sc where sc.s_score<60;
– 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select st.s_id,st.s_name from score sc
left join student st on sc.s_id=st.s_id
where sc.s_score>=80 and sc.c_id='01';
– 39、求每门课程的学生人数
select sc.c_id,count(1) from score sc group by sc.c_id;
– 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select st.*,sc.s_score 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
left join student st on st.s_id=sc.s_id
where t.t_name='张三'
order by sc.s_score desc limit 0,1;
– 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select distinct a.*from (
select st.s_id,sc.c_id,sc.s_score from score sc
left join score sc1 on sc.s_score=sc1.s_score
left join student st on sc.s_id=st.s_id
where sc.s_id!=sc1.s_id)a;
– 42、查询每门功成绩最好的前两名
select sc1.c_id,sc1.s_id,st.s_name,sc1.s_score from score sc1
left join score sc11 on sc1.c_id=sc11.c_id
left join student st on sc1.s_id=st.s_id
where sc1.s_score>=sc11.s_score
group by sc1.c_id,sc1.s_id
having count(*)>4 and count(*)<=6
order by sc1.c_id,count(*) desc;
– 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
– 若人数相同,按课程号升序排列
select sc.c_id,count(1)from score sc group by sc.c_id having count(1)>5 order by count(1)desc,sc.c_id;
– 44、检索至少选修两门课程的学生学号
select sc.s_id from score sc group by sc.s_id having count(1)>=2;
– 45、查询选修了全部课程的学生信息
select st.*from score sc
left join student st on sc.s_id=st.s_id
group by sc.s_id
having count(1)=(select count(1) from course);
– 46、查询各学生的年龄
select st.s_name,(year(now())- year(date_format(st.s_birth,'%Y-%m-%d'))) from student st ;
– 47、查询本周过生日的学生
select st.s_name from student st
where week(concat(year(now()),'-',month(st.s_birth),'-',day(st.s_birth)))=week(now());
– 48、查询下周过生日的学生
select st.s_name from student st
where week(concat(year(now()),'-',month(st.s_birth),'-',day(st.s_birth)))=week(now())+1;
– 49、查询本月过生日的学生
select st.s_name from student st where month(st.s_birth)=month(now());
– 50、查询下月过生日的学生
select st.s_name from student st where
(case when month(now())=12 then month(st.s_birth)=1
else month(st.s_birth)=month(now())+1
end);