数据准备:
学生表 Student:
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');
科目表 Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表 SC
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
45题预览:
题目
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
3.查询在 SC 表存在成绩的学生信息
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
4.1 查有成绩的学生信息
5.查询「李」姓老师的数量
6.查询学过「张三」老师授课的同学的信息
7.查询没有学全所有课程的同学的信息
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
14.查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
18.查询各科成绩前三名的记录
19.查询每门课程被选修的学生数
20.查询出只选修两门课程的学生学号和姓名
21.查询男生、女生人数
22.查询名字中含有「风」字的学生信息
23.查询同名同性学生名单,并统计同名人数
24.查询 1990 年出生的学生名单
25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
30.查询不及格的课程
31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
32.求每门课程的学生人数
33.假设成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
34.假设成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
36.查询每门功成绩最好的前两名
37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。
38.检索至少选修两门课程的学生学号
39.查询选修了全部课程的学生信息
40.查询各学生的年龄,只按年份来算
41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
42.查询本周过生日的学生
43.查询下周过生日的学生
44.查询本月过生日的学生
45.查询下月过生日的学生
详细
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
理清思路:
(1)学生信息和课程分数分布对应于student表和sc表
所以,我们需要把它们关联起来
select * from student a inner join sc b
on a.sid=b.sid;
(2)需要比较同一学生的不同成绩,所以需要再关联成绩表sc
select * from
student a inner join sc b on a.sid=b.sid
inner join sc c
on a.sid=c.sid ;
(3)利用where进行筛选
select * from
student a inner join sc b on a.sid=b.sid
inner join sc c
on a.sid=c.sid and b.cid=01 and c.cid=02
where b.score>c.score;
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
?什么叫做同时存在‘01‘和’02‘课程
?这里是指cid=’01‘ and cid ='02'?
理清思路:同一个学生既选了01课程,又选了02课程
select * from sc a inner join sc b
on a.sid=b.sid and
a.cid='01' and b.cid='02';
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
?如何让不存在某课程时显示为null
利用左关联(left join)
?case when--no
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
思路:找到不存在01课程的记录后,
select * from sc where sid not in (select sid from sc where cid='01');
然后再进行关联(Inner join)找存在02的课程
select *
from (select * from sc where sid not in (select sid from sc where cid='01')) a inner join sc b
on a.sid=b.sid and b.cid='02';
或者
select *
from sc a
where sid not in (select sid from sc where cid='01')
and cid='02';
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
思路:
成绩在sc表,学生姓名在student表,二者可以通过学生编号sid进行关联。
首先,从sc表中查询平均成绩大于60的学生的信息
select * from sc where avg(score)>60;
ERROR 1111 (HY000): Invalid use of group function
出现这个错就要往group by这里考虑,要记得:group by +having+聚合函数。
一些错误尝试:
成功查询:
select * from sc group by sid having avg(score)>60;
接着:通过学生编号sid与student表进行关联
select b.sid, sname ,avg(score ) from
(select * from sc group by sid having avg(score)>60) a
inner join student b on a.sid=b.sid;
发现结果明显不对,
继续修改:
注意:多个表进行关联时要明确字段的归属表;查询聚合函数的结果时,比如avg(score),最好进行重命名。
select b.sid, b.sname ,a.s_avg from
(select sid,avg(score) as s_avg from sc group by sid having avg(score)>60) a
inner join student b on a.sid=b.sid;
存在疑问:
上面两种写法的差别就在于 子查询的avg(score)是否重命名,但是二者的结果明显有差异,想请问是为什么?是不是哪里没注意到?
解答:
mysql对于group by的特殊规定:
如果用了group by,select语句里除了group by后面跟着的列名以外,只能有聚合函数。
如果除了group by后面跟着的列名和聚合函数还有其他列名,sql就会只输出第一行,而且如果是'*',也会只输出第一行记录。
举例说明:
select sid from sc group by sid having avg(score)>60;
select avg(score) from sc group by sid having avg(score)>60;
select sid,avg(score) from sc group by sid having avg(score)>60;
select score from sc group by sid having avg(score)>60;
总结:
如果用了group by +聚合函数,那么在查询的时候最好是只查询聚合函数(+group by 后的列名)避免在查询其余字段时只显示一条记录的情况。
注意:聚合函数是一定要查询的。
3.查询在 SC 表存在成绩的学生信息
思路:
(1)首先确定表:sc表和student表
select * from sc a left join student b
on a.sid=b.sid;
发现名字有重复,
(2)利用group by sid去除重复的学生信息查询,具体如下:
select b.* from sc a left join student b
on a.sid=b.sid
group by a.sid;
注意:更严谨的写法:
(1)先写查询分组字段的子查询
select sid from sc group by sid;
(2)将(1)中的字段进行嵌套查询
select
b.*
from
(select sid from sc group by sid) a
left join student b
on a.sid=b.sid;
注意:group by是分开写的
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null
(1)确定涉及的表的范围:
student,sc
(2)
思路:
select a.sid,count(b.cid),sum(score) from
(select sid from student group by sid)
a left join sc b
on a.sid=b.sid;
修改:
(2)
select sid,count(cid) as cons,sum(score) as sum_score
from sc group by sid;
(3)
select a.sid,a.sname,b.cons,b.sum_score from student a
left join
(select sid,count(cid) as cons,sum(score) as sum_score
from sc group by sid) b
on a.sid=b.sid;
也可以考虑先连接在查询,但还是建议上述这种方式。
4.1 查有成绩的学生信息
(1)首先确定使用的表范围
sc和student
(2)首先获得成绩表上的学生id
select sid from sc
group by sid;
(3)根据id去查学生信息
select b.* from
(select sid from sc
group by sid) a
left join
student b
on a.sid=b.sid;
?这个和第三题是完全一样的吗
回答:思路基本一致。
参考答案:
思路是:看学生表上的id是否在sc表的id中(即,判断存在)in/exists
select *
from student a
where a.sid in (select sid from sc group by sid );
5.查询「李」姓老师的数量
select count(*) from teacher
where tname like '李%';
6.查询学过「张三」老师授课的同学的信息
(1)四张表都要用到
(2)四个嵌套
select * from student
where sid in
(select sid from sc
where cid in
(select cid from course
where tid in
(select tid from teacher
where tname='张三')));
有什么简便方法吗?
回答:参考答案也是逐步嵌套查询实现的。
7.查询没有学全所有课程的同学的信息
(1)关键词:‘所有课程’在course表,‘同学信息’在student表,但是二者需要sid进行关联,所以还需要用到sc表
(2)如何筛选'所有课程’??
利用课程数目来进行限定。
a.course表获取总课程数
select count(*) from course;
b.按照sid分组,对sc表中的课程数进行计数
select count(1) from sc
group by sid;
c.接着从sc中筛选出课程数=3的学生sid
select sid from sc
group by sid
having count(1)<(select count(*) from course);
d.查询(3)中对应学生sid的学生信息
select a.* from student a
inner join
(select sid from sc
group by sid
having count(1)<(select count(*) from course)) b
on a.sid=b.sid;
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
(1)关键词:
‘至少有一门课’--课程in学号01同学的课程,‘学号01同学的课程’--course表,‘同学信息’--student表
(2)思路:
a. 01同学学习了什么课程
select cid from sc
where sid='01';
发现01同学三门课程都学了,所以其余学生只要学习的课程数大于0即可。
b. 学号01之外的学生id有哪些
select distinct sid from sc
where sid!='01';
c. 如何查询其余学生的课程信息,比如cid?
利用其余学生的id进行分组再计数,筛选
出次数大于0的学生id,再根据该id进行学生信息的查询。
select sid from sc
where sid in (select distinct sid from sc
where sid!='01')
group by sid having count(1)>0;
select * from student a
inner join
(select sid from sc
where sid in (select distinct sid from sc
where sid!='01')
group by sid having count(1)>0) b
on a.sid=b.sid;
9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
(1)确定相关表:
’01号同学学习的课程‘-sc表;
’其他同学的信息‘-student表;
(2)思路:
a. 01号同学学习了哪些课程
select cid,count(1) from sc
where sid='01';
发现01同学学了三门课,
b.再查询总共有几门课?
select count(1) from course;
所以,总共有3门课,01同学全部学习了,
c. 查询其余学生中学习课程数目为3的学生id
#在course表中查询01同学之外的学生id
select distinct sid from sc where sid!='01';
#在sc表中统计01同学之外的学生学习课程数并筛选出课程数为3的sid
select sid,count(1) from sc
where sid in
(select distinct sid from sc where sid!='01')
group by sid having count(1)=
(select count(1) from sc
where sid='01');
d. 再跟进相应id在student表中查询学生信息
select * from student
where sid in
(select sid from sc
where sid in
(select distinct sid from sc where sid!='01')
group by sid having count(1)=
(select count(1) from sc
where sid='01'));
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
(1)确定相关表:
‘张三老师tid’--teacher;
‘张三老师授课cid’--course;
‘被授课的学生sid’--sc;
‘学生姓名sname’--student;
先找出学习过张三老师课程的学习,再not in即可。
(2)思路:
a.从teacher表中查询张三老师的tid
select tid from teacher
where tname='张三';
b.从course表中查询张三老师的授课cid
select cid from course
where tid=(select tid from teacher
where tname='张三');
c. 从sc中查询学习cid=02的学生sid
select sid from sc
where cid in
(select cid from course
where tid=(select tid from teacher
where tname='张三'));
d.利用not in 查询没学过张三老师课程的学生信息
select * from student
where sid not in
(select sid from sc
where cid in
(select cid from course
where tid=(select tid from teacher
where tname='张三')));
11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
(1)根据关键字确定相关表:
‘不及格课程数>2’ score<60--sc表,且count(1)>3
'姓名'--student 表
(2)思路:
a. 从sc表查询score小于60的sid,并根据sid分组计数
select sid,count(1) from sc
where score<60
group by sid ;
b.内连接得到不及格课程数目大于等于2的sid,sname以及avg_score
select a.sid,a.sname,b.avg_score
from student a
inner join
(select sid,count(1) as cons,avg(score) as avg_score from sc
where score<60
group by sid having cons>=2) b
on a.sid=b.sid;
12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
(1)根据关键词确定相关表:
‘01课程分数小于60’ --sc表
‘学生信息’--student
(2)思路及实现:
a.先从sc表查询01课程分数小于60并按分数降序排列的学生信息
select * from sc
where cid='01' and score<60
order by score desc;
b.根据(1)中的sid从student中查询学生信息
select a.*,b.score
from student a
inner join
(select * from sc
where cid='01' and score<60
order by score desc) b
on a.sid=b.sid;
13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
(1)根据关键词确定相关表
‘平均成绩’--sc表
(2)思路及实现过程:
a. 先按照平均成绩从高到低显示学生sc
select sid,avg(score) as avg_score
from sc group by sid
order by avg_score desc;
还要显示每个学生的每一科目的成绩吗?
回答:是的,给sc表增加一列avg_score。
select
b.*,a.avg_score
from (select sid,avg(score) as avg_score
from sc group by sid
) a
left join
sc b
on a.sid=b.sid
order by avg_score desc;
14.查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
(1)根据关键词确定相关表:
‘成绩’ --sc;
‘课程name’--course;
(2)思路及实现:
a. 按照cid对sc表进行分组,并进行初步聚合函数的查询
select cid as '课程ID' ,max(score) as '最高分',
min(score) as '最低分' ,avg(score) as '平均分' from sc
group by cid;
b.继续完善
?如何计算各种率
回答:考察条件计数的技巧 case when
select cid as '课程ID' ,max(score) as '最高分',
min(score) as '最低分' ,avg(score) as '平均分' ,
count(1) as 选修人数 ,
sum(case when score>=60 then 1 else 0 end)/count(1) as '及格率',
sum(case when score>=70 and score<80 then 1 else 0 end)/count(1) as '中等率',
sum(case when score>=80 and score<90 then 1 else 0 end)/count(1) as '优良率',
sum(case when score>=90 then 1 else 0 end)/count(1) as '优秀率'
from sc
group by cid
order by 选修人数 desc,cid asc;
修改成题目要求格式:
select
b.课程ID,a.cname,b.最高分,b.最低分,b.平均分,
b.选修人数,b.及格率,b.中等率,b.优良率,b.优秀率
from course a
inner join
(select cid as '课程ID' ,max(score) as '最高分',
min(score) as '最低分' ,avg(score) as '平均分' ,
count(1) as 选修人数 ,
sum(case when score>=60 then 1 else 0 end)/count(1) as '及格率',
sum(case when score>=70 and score<80 then 1 else 0 end)/count(1) as '中等率',
sum(case when score>=80 and score<90 then 1 else 0 end)/count(1) as '优良率',
sum(case when score>=90 then 1 else 0 end)/count(1) as '优秀率'
from sc
group by cid
order by 选修人数 desc,cid asc) b
on a.cid=b.课程ID;
阶段总结:
- 每道题目的解法并不唯一,能得到最终结果的就是好方法。
*多练多练多练。
15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
(1)根据关键词确定相关表:
sc表
(2)思路:
借助赋值变量@i实现
a.原始数据增加一列@rank
select * from
sc,(select @rank:=0) as t;
b.
select
sid,
cid,
score,
@rank:=@rank+1 as rn
from sc,(select @rank:=0) as t ;
没有指定关联条件的inner join (笛卡尔积),每一行都会与之进行关联
注意:
- ':=' 表示赋值
- '=' 表示判断
15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
select
sid,
cid,
score,
case when @sco=score then @rank
when @sco:=score then @rank:=@rank+1
end as rn
from sc,(select @rank:=0,@sco=null) as t
order by score desc;
16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
(1)‘学术总成绩’,‘重复保留空缺’
赋值变量结合if使用的变种,sc表
(2)先查询总成绩并降序排名
select sum(score) as sum_score from sc
group by sid
order by sum_score desc;
(3)构建两个赋值变量
select *
from
sc,(select @rank:=0,@sco:=null) as t;
(3)把总成绩和赋值变量关联起来
select
*
from
(select sum(score) as sum_score from sc
group by sid
order by sum_score desc) a,
(select @rank:=0,@sco:=null) as t;
(4)完善题目细节查询
select
a.*,
@rank:=if(@sco=sum_score,'',@rank+1) as rn,
@sco:=sum_score
from
(select sum(score) as sum_score from sc
group by sid
order by sum_score desc) a,
(select @rank:=0,@sco:=null) as t;
16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select
a.*,
@rank:=if(@sco=sum_score,@rank,@rank+1) as rn,
@sco:=sum_score
from
(select
sum(score) as sum_score
from sc
group by sid
order by sum_score desc) a ,
(select @rank:=0,@sco:=null) as b;
17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
提示:分组统计数据,判断各分数段的人数情况
1、直接进行分组统计
思路:
select
cid,
该分数段人数/该课程总人数=该分数段人数百分比
from sc
group by cid;
select
cid,
sum(case when 0<=score and score<=60 then 1 else 0 end)/count(1) as '(0-60]',
sum(case when 60<score and score<=70 then 1 else 0 end)/count(1) as '(60-70]',
sum(case when 70<score and score<=85 then 1 else 0 end)/count(1) as '(70-85]',
sum(case when 85<score and score<=100 then 1 else 0 end)/count(1) as '(85-100]'
from sc
group by cid;
2、加一个百分号
先扩大100倍,再利用concat函数
举例
select concat('abc','124');
select
cid,
concat(sum(case when 0<=score and score<=60 then 1 else 0 end)/count(1)*100,'%') as '(0-60]',
concat(sum(case when 60<score and score<=70 then 1 else 0 end)/count(1)*100,'%') as '(60-70]',
concat(sum(case when 70<score and score<=85 then 1 else 0 end)/count(1)*100,'%') as '(70-85]',
concat(sum(case when 85<score and score<=100 then 1 else 0 end)/count(1)*100,'%') as '(85-100]'
from sc
group by cid;
18.查询各科成绩前三名的记录
?是查学生记录吗?不是,只是成绩和sid,只用sc表。
select cid from sc
group by cid
order by score desc;
是通过cid进行关联吗?不是,换个思路
select
a.*
from sc a
where 大于此条成绩的记录少于3条
select
a.*
from sc a
where (select count(1) from sc b where b.cid=a.cid and b.score>a.score)<3
order by cid desc,score desc;
19.查询每门课程被选修的学生数
select count(1) from sc
group by cid;select count(1) from sc
group by cid;
20.查询出只选修两门课程的学生学号和姓名
(1)从sc表中筛选出选修两门课程的学生sid
select sid,count(1) as cons from sc
group by sid having cons=2;
(2)
select a.sid,a.sname from student a
inner join
(select sid from sc
group by sid having count(1)=2) b
on a.sid=b.sid;
21.查询男生、女生人数
(1)student 表
(2)
select ssex,count(1) from student
group by ssex;
22.查询名字中含有「风」字的学生信息
(1)student表
(2)
select * from student
where sname like '%风';
注意:
这里的模糊用'%'更合适。
知识点回顾:
- “_”匹配任何单个字符
- “%”匹配任意数目字符(包括零个字符)。
23.查询同名同性学生名单,并统计同名人数
(1)‘同名同性别’ 用student表
(2)思路:
‘同名同性别的学生’ 量化翻译:sname相同,ssex相同,sid不同
‘统计人数’ 考虑到用group by
select a.sname,a.ssex,count(1) from student a inner join
student b on a.sid!=b.sid and a.ssex=b.ssex
and a.sname=b.sname
group by a.sname,a.ssex;
24.查询 1990 年出生的学生名单
select *
from student
where year(sage)='1990';
25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select cid,avg(score) as avg_score
from sc group by cid
order by avg_score desc,cid asc;
总结:
- 注意:赋值变量的用法;if;case when;sum(case when...then...end)
- 理清逻辑
- 要一直做题保持熟练度
26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
(1)‘姓名’--student表;‘平均成绩’--score
(2)a.查询平均成绩大于85的学生sid
select sid,avg(score) from sc
group by sid having avg(score)>=85;
b.和student 内关联
select
a.sid,
a.sname,
b.avg_score
from student a
inner join
(select sid,avg(score) as avg_score from sc
group by sid having avg_score>85) b
on a.sid=b.sid;
27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
(1)'课程名称'--course表; ‘分数’--sc表;二者通过cid进行关联
‘学生姓名’--student表
(2)
a.从course表筛选数学课程
select * from course
where cname='数学';
b.从sc表筛选分数低于60的信息
select * from sc
where score<60;
c.上述两表进行关联
select *
from (select * from course
where cname='数学') a
inner join
(select * from sc
where score<60) b on a.cid=b.cid;
d.再和student表进行关联
select
c.sname,d.score
from student c inner join
(select b.sid,b.score
from (select * from course
where cname='数学') a
inner join
(select * from sc
where score<60) b on a.cid=b.cid) d
on c.sid=d.sid;
28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
正确思路:
‘所有学生’--student表;‘分数’--sc表,二者关联即可,即给学生信息加上课程情况。
select
*
from
student a left join
sc b on a.sid=b.sid;
错误思路:
下面这个思路,查询的并不是“所有”学生。
(1)‘课程’ --course表;‘分数’--sc表
(2)
?需要查明没选课的学生吗?用left join?
select c.*
from
sc c inner join
(select sid from sc a
left join course b on a.cid=b.cid
group by sid ) d
on c.sid=d.sid;
29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
(1)‘成绩’--sc表;‘姓名’--student表;‘课程名称’--course表
(2)“任何一门”??
?如何把名字去重? 参考答案没有去重
select distinct d.sname,c.cname,d.score from
course c inner join
(select a.sname,b.* from
student a
inner join
(select * from sc
where score >70) b
on a.sid=b.sid) d
on c.cid=d.cid;
参考答案:
学习 ‘left join ’连用的写法
select b.sname,c.cname,a.score
from sc a
left join student b
on a.sid=b.sid
left join course c
on a.cid=c.cid
where a.score>70;
30.查询不及格的课程
(1)‘不及格’ --sc表
(2)
select distinct cid
from sc
where score<60;
31.查询课程编号为 01 且课程成绩在 为80分的学生的学号和姓名
(1)'课程编号、成绩'--sc表;‘姓名’--student表
(2)
select a.sid,a.sname
from student a
inner join
(select * from sc
where cid='01' and score=80) b
on a.sid=b.sid;
32.求每门课程的学生人数
select cid,count(1)
from sc group by cid;
33.假设成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
(1) '张三老师'--teacher表(tid);course表;sc表
(2)"成绩不重复“只要查出第一个排序后的第一个学生的成绩即可
select d.* ,c.score from teacher a
inner join course b
inner join sc c
inner join student d
on a.tid=b.tid
and b.cid=c.cid
and c.sid=d.sid
and a.tname='张三'
order by c.score desc limit 1;
(可以内连接一次写一次限制条件,但还是在用连续连接,有进步的)
34.假设成绩有重复的情况下,查询选修「张三」老师所授课程的学生
中,成绩最高的学生信息及其成绩
?假设成绩有重复?意思就是如果第一名有两个人并列第一,则需要把两个人都查出来。
需要借助变量进行排名
a. 先给成绩排个名
select
m.* ,
case when @score=score then @rank
when @score:=score then @rank:=@rank +1 end as rn from
(select d.* ,c.score from teacher a
inner join course b
inner join sc c
inner join student d
on a.tid=b.tid
and b.cid=c.cid
and c.sid=d.sid
where a.tname='张三') m ,
(select @score:=null,@rank:=0) n;
b.取出排名为1的所有学生的成绩
框架:
select *
from
() t
where t.rn=1;
完整代码:
select *
from
(select
m.* ,
case when @score=score then @rank
when @score:=score then @rank:=@rank +1 end as rn from
(select d.* ,c.score from teacher a
inner join course b
inner join sc c
inner join student d
on a.tid=b.tid
and b.cid=c.cid
and c.sid=d.sid
where a.tname='张三') m ,
(select @score:=null,@rank:=0) n) t
where t.rn=1;
35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
(1)‘课程’,‘成绩’--sc;
(2)
条件关联筛选
同一名学生的不同课程
select
a.*
from sc a
inner join
sc b on a.sid=b.sid
where a.score=b.score
and a.cid!=b.cid
group by a.sid,a.cid ;
36.查询每门功成绩最好的前两名
(1)“成绩”--sc表
(2)“前两名”
a.“前一名”很好查
select * from sc
group by cid
order by score desc;
b.如果是查前两名,则需要借助用户变量,然后取出前两名
select *,
@rank:=if(@c_cid=cid,if(@sco=score,@rank,@rank+1),1) as rn,
@sco:=score,
@c_cid:=cid
from sc,(select @score:=null,@rank:=0,@c_cid=null) t
order by cid,score desc;
select cid,sid,score,rn
from
(select *,
@rank:=if(@c_cid=cid,if(@sco=score,@rank,@rank+1),1) as rn,
@sco:=score,
@c_cid:=cid
from sc,(select @score:=null,@rank:=0,@c_cid=null) t
order by cid,score desc)m
where rn<3;
37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。
(1)“选修人数”--sc表
(2)
select cid,count(1) from sc
group by cid
having count(1)>5 ;
38.检索至少选修两门课程的学生学号
(1)"至少学生两门"、“学生学号”--sc表
(2)
select
sid,count(1)
from sc
group by sid
having count(1)>=2;
39.查询选修了全部课程的学生信息
(1)‘选修全部课程’--sc表;“学生信息”--student表
(2)
全部课程量化表现就是课程总数的比较
select a.* from student a
inner join
(select sid,count(1) from sc
group by sid
having count(1)=(select count(1) from course)) b
on a.sid=b.sid;
40.查询各学生的年龄,只按年份来算
(1)“年龄”--student表
(2)
select sid,sname,2020-year(sage) as '年龄'
from student;
或者
select sid,sname,year(now()-year(sage)) as '年龄'
from student;
41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select *, timestampdiff(year,sage,now()) as age
from student;
回顾知识点:
timestampdiff 日期相减函数(特点:如果当前月日小于出生年月的月日时,年龄减一)
select timestampdiff(year,'2002-05-01','2003-06-01');
#-1
select timestampdiff(day,'2002-05-01','2001-01-01');
#-485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00');
#-12
获得当前时间的两种方式
select now();
select curdate();
42.查询本周过生日的学生
考察日期函数week()的用法
举例:select week('2004-06-02')
select
*,
week(sage),
week(now())
from student
where week(sage)=week(now());
43.查询下周过生日的学生
select
*,
week(sage),
week(now())
from student
where week(sage)=week(now())+1;
44.查询本月过生日的学生
考察日期函数:month()
select
*,
month(sage),
month(now())
from student
where month(sage)=month(now());
45.查询下月过生日的学生
select
*,
month(sage),
month(now())
from student
where month(sage)=month(now())+1;
总结一下这45道题的考察点:
- group by 的用法
(group by +having) - 自关联,左关联,内关联
- 时间函数 year();month();week();now();
- 赋值变量(@rank:=0)
- 嵌套查询
- 题目量化表述,比如“选全部课程”转化为课程数的比较
- 条件查询
最后,要一直保持做题的感觉,熟能生巧。
【7月18日】学习心得记录,sql提取某一类别的第一条记录,直接group by 即可,但如果是查询某一类别中的前几条记录,有两个角度,一是利用赋值变量对齐进行排序然后选取要求的行数;另外一个思路是利用窗口函数。
网友评论