纪念一下,是真的不看答案自己做出来的,另外还有七道目前不能不看答案做出来,各个击破,加油加油!!!!!
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select a.snum,b.sname ,a.score,c.score from (select * from sc where cnum='01') a left join student b on a.snum=b.snum inner join (select * from sc where cnum='02') c on a.snum=c.snum where a.score>c.score;
select * from sc a left join sc b on a.snum=b.snum and a.cnum='01' and b.cnum='02' where a.score>b.score group by a.snum;
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select * from (select * from sc where cnum='01') a left join student b on a.snum=b.snum inner join (select * from sc where cnum='02') c on a.snum=c.snum;
select * from sc a inner join sc b on a.snum=b.snum and a.cnum='01' and b.cnum='02';
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from (select * from sc where cnum='01') a left join student b on a.snum=b.snum left join (select * from sc where cnum='02') c on a.snum=c.snum;
select * from sc a left join sc b on a.snum=b.snum and b.cnum='02' where a.cnum='01' ;
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from (select * from sc where cnum='02') a left join student b on a.snum=b.snum left join (select * from sc where cnum='01') c on a.snum=c.snum where c.cnum is null;
select * from sc a left join sc b on a.snum=b.snum and b.cnum='01' where a.cnum='02' and b.cnum is null;
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select a.snum,b.sname,avg(score) as avg from sc a left join student b on a.snum=b.snum group by a.snum;
3.查询在 SC 表存在成绩的学生信息
select b.snum,a.sname from sc b left join student a on a.snum=b.snum group by b.snum;
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.snum,b.sname,count(*) as cons,sum(score) as con from sc a left join student b on a.snum=b.snum group by a.snum;
select a.snum,a.sname,b.* from student a left join (select snum,count(*) as cons,sum(score) as con from sc group by snum)b on a.snum=b.snum;
4.1 查有成绩的学生信息
select * from student where snum in (select distinct snum from sc where score is not null);
5.查询「李」姓老师的数量
select count(tname) as cons from teacher where tname like '李%';
6.查询学过「张三」老师授课的同学的信息
select * from student where snum in (select snum from sc where cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')));
select a.* from sc a inner join course b on a.cnum=b.cnum inner join teacher c on b.tnum=c.tnum
and c.tname='张三' group by a.snum;
7.查询没有学全所有课程的同学的信息
思路分解
课程总数小于cnum总数
select a.snum,b.* from student a inner join (select snum,count(cnum) as cons from sc group by snum having cons
select * from student where snum not in (select a.snum from sc a left join course b on a.cnum=b.cnum group by a.snum having count(a.cnum)=(select count(1) from course));
select distinct student.* from (select student.snum,course.cnum from student,course)t1 left join (select sc.snum,sc.cnum from sc) t2 on t1.snum=t2.snum and t1.cnum=t2.cnum,student where t2.snum is null and t1.snum=student.snum;
select * from (select student.snum,course.cnum from student,course)t1 left join (select sc.snum,sc.cnum from sc) t2 on t1.snum=t2.snum and t1.cnum=t2.cnum
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct a.snum,b.sname from sc a left join student b on a.snum=b.snum where a.cnum in (select cnum from sc where snum='01') and a.snum<>'01';
select distinct b.* from sc a left join student b on a.snum=b.snum where cnum in (select cnum from sc where snum='01') a.snum<>'01';
9.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select b.* from (select * from sc where snum in (select snum from sc where cnum in (select cnum from sc where snum='01')) and snum!='01') a left join student b on a.snum=b.snum group by a.snum having count(cnum)=(select count(cnum) from sc where snum='01');
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student where snum not in (select snum from sc where cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')));
select *from student where student.Snum not in (select student left join sc on student.snum=sc.snum where exists (select * from course a inner join teacher b on a.tnum=b.tnum where tname='张三' and course.cnum=sc.cnum));
11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.snum,b.sname,avg(score) from sc a left join student b on a.snum=b.snum where a.score<60 group by a.snum having count(a.cnum)>=2;
12、检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select a.*,b.* from sc a left join student b on a.snum=b.snum where a.cnum='01' and a.score<60
order by a.score;
13、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select snum,max(case cnum when '01' then score else null end)'01',
max(case cnum when '02' then score else null end)'02',
max(case cnum when '01' then score else null end)'03',avg(score) as avg
from sc group by snum order by avg desc;
14.查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select a.cnum,b.cname,count(*) as 选课人数,sum(case when score<=60 then 1 else 0 end)'[60-0]',concat(sum(case when score<=60 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=70 and score> 60 then 1 else 0 end)'[70-60]',concat(sum(case when score<=70 and score> 60 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=85 and score> 70 then 1 else 0 end)'[85-70]',concat(sum(case when score<=85 and score> 70 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=100 and score> 85 then 1 else 0 end)'[100-85]' ,concat(sum(case when score<=100 and score> 85 then 1 else 0 end)/count(*)*100,'%')占比 from sc a inner join course b on a.cnum=b.cnum group by a.cnum;
select a.cnum,b.cname,count(*) as 选课人数,sum(If (score<=60,1,0))'[60-0]',concat(sum(If (score<=60,1,0))/count(*)*100,'%')占比,sum(if(score<=70 and score> 60,1,0))'[70-60]',concat(sum(if(score<=70 and score> 60,1,0))/count(*)*100,'%')占比,sum(if(score<=85 and score>70,1,0))'[85-70]',concat(sum(if(score<=85 and score>70,1,0))/count(*)*100,'%')占比,sum(if(score<=100 and score>85,1,0))'[100-85]' ,concat(sum(if(score<=100 and score>85,1,0))/count(*)*100,'%')占比 from sc a inner join course b on a.cnum=b.cnum group by a.cnum;
15、按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
select *,rank()over(partition by cnum order by score desc) as rank from sc;
select snum,cnum,score,@rank:=@rank+1 as rn from sc ,(select @rank:=0) as t order by score desc;
select a.score,(select count(b.score) from sc b where b.score>a.score)+1 as rank from sc a order by rank ;
15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
select *,case when (@sco=score) then @rank else @rank:=@rank+1 end as rn,
@sco:=score from sc ,(select @rank:=0,@sco:=null) as t order by score desc;
select a.cnum, a.snum, a.score, count(b.score)+1 as rank from sc as a left join sc as b on a.score
select a.score,(select count(distinct b.score) from sc b where b.score>a.score)+1 as rank from sc a order by rank ;
select a.cnum, a.snum, a.score, count(b.score)+1 as rank from sc as a left join sc as b on a.score
19、查询每门课程被选修的学生数
select cnum ,count(cnum) from sc group by cnum;
20、查询出只选修两门课程的学生学号和姓名
select a.* from student a inner join (select snum,count(cnum) from sc group by snum having count(cnum)=2)b on a.snum=b.snum;
21.查询男生、女生人数
select ssex,count(1) from student group by ssex;
22、查询名字中含有「风」字的学生信息
select * from student where sname like '%风%';
23.查询同名同性学生名单,并统计同名同性人数;
24.查询 1990 年出生的学生名单
select * from student where year(sage)='1990';
25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select cnum,avg(cnum) as avg from sc group by cnum order by avg desc,cnum;
26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select a.snum,b.sname,avg(score) as avg from sc a left join student b on a.snum=b.snum
group by snum having avg>85;
27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select * from sc where cnum in (select cnum from course where cname='数学') and score<60;
28、查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select * from student a left join sc b on a.snum=b.snum group by a.snum;
29、查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select a.snum,b.sname,c.cname,a.score from sc a left join student b on a.snum=b.snum inner join course c on a.cnum=c.cnum where score>70;
30.查询存在不及格的课程
select distinct cnum from sc where score<60;
31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select * from student where snum in (select snum from sc where cnum='01' and score>80);
select student.Snum,student.Sname from student ,sc where sc.Cnum='01' and student.Snum=sc.Snum and sc.score>80
32、求每门课程的学生人数
select cnum,count(cnum) from sc group by cnum;
33、假设成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select a.*,b.score from sc b inner join student a on a.snum=b.snum where b.cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')) order by score desc limit 1
select a.*,b.score from student a inner join (select a.* from sc a left join course b on a.cnum=b.cnum inner join teacher c on b.tnum=c.tnum where tname='张三')b on a.snum=b.snum
order by b.score desc limit 1;
select * from sc left join student on student.snum=sc.snum left join course on course.cnum=sc.cnum left join teacher on teacher.tnum=course.tnum where teacher.tname='张三' order by score desc limit 1;
35、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select * from sc a left join sc b on a.snum=b.snum and a.score=b.score where a.cnum<>b.cnum
group by a.snum,a.cnum;
select * from sc a where exists(select * from sc b where a.snum=b.snum and a.cnum<>b.cnum and a.score=b.score);
37.统计每门课程的学生选修人数(超过 5 人的课程才统计)
select cnum,count(cnum) from sc group by cnum having count(cnum)>5;
38.检索至少选修两门课程的学生学号
select snum,count(cnum) from sc group by snum having count(cnum)>=2;
39、查询选修了全部课程的学生信息
select * from student where snum in (select snum from sc group by snum having count(cnum)
40.查询各学生的年龄,只按年份来算
select *,Year(now())-Year(sage) as age from student;
41、按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select *,case when substr(sage,6,5)=substr(now(),6,5) then Year(now())-Year(sage) else sage end '年龄' from student;
44.查询本月过生日的学生
select *,month(sage) as birth_month,month(now()) as now_month from student where month(sage)=month(now());
select * from student where EXTRACT(MONTH FROM student.Sage)=EXTRACT(MONTH FROM CURDATE());
45.查询下月过生日的学生
select *,month(sage) as birth_month,month(now()) as now_month from student where month(sage)=month(now())+1;
select * from student where EXTRACT(MONTH FROM student.Sage)=EXTRACT(MONTH FROM DATE_ADD(CURDATE(),INTERVAL 1 MONTH))