本博客SQL脚本地址:gitee
已知有如下4张表:
学生表:student(学号,学生姓名,出生年月,性别)
成绩表:score(学号,课程号,成绩)
课程表:course(课程号,课程名称,教师号)
教师表:teacher(教师号,教师姓名)
为了演示题目的运行过程,我们先按下面语句在客户端HeidiSQL中创建数据库和表。
创建"成绩表“。“课程表的“学号”和“课程号”一起设置为主键约束(联合主键),“成绩”这一列设置为数值类型(float,浮点数值)
课程表的“课程号”设置为主键约束
教师表的“教师号”列设置为主键约束,
教师姓名这一列设置约束为“null”(红框的地方不勾选),表示这一列允许包含空值(null)
insert into student(学号,姓名,出生日期,性别)
values('0001' , '猴子' , '1989-01-01' , '男');
insert into student(学号,姓名,出生日期,性别)
values('0002' , '猴子' , '1990-12-21' , '女');
insert into student(学号,姓名,出生日期,性别)
values('0003' , '马云' , '1991-12-21' , '男');
insert into student(学号,姓名,出生日期,性别)
values('0004' , '王思聪' , '1990-05-20' , '男');
insert into score(学号,课程号,成绩)
values('0001' , '0001' , 80);
insert into score(学号,课程号,成绩)
values('0001' , '0002' , 90);
insert into score(学号,课程号,成绩)
values('0001' , '0003' , 99);
insert into score(学号,课程号,成绩)
values('0002' , '0002' , 60);
insert into score(学号,课程号,成绩)
values('0002' , '0003' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0001' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0002' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0003' , 80);
insert into course(课程号,课程名称,教师号)
values('0001' , '语文' , '0002');
insert into course(课程号,课程名称,教师号)
values('0002' , '数学' , '0001');
insert into course(课程号,课程名称,教师号)
values('0003' , '英语' , '0003');
insert into teacher(教师号,教师姓名)
values('0001' , '孟扎扎');
insert into teacher(教师号,教师姓名)
values('0002' , '马化腾');
-- 这里的教师姓名是空值(null)
insert into teacher(教师号,教师姓名)
values('0003' , null);
-- 这里的教师姓名是空字符串('')
insert into teacher(教师号,教师姓名)
values('0004' , '');
为了方便学习,将50道面试题进行了分类
查询姓“猴”的学生名单
select count(教师号)
from teacher
where 教师姓名 like '孟%';
select sum(成绩)
from score
where 课程号 = '0002';
select count(distinct 学号) as 学生人数
from score;
select 课程号,max(成绩) as 最高分,min(成绩) as 最低分
from score
group by 课程号;
select 课程号, count(学号) as 人数
from score
group by 课程号;
select 性别,count(*) as 人数
from student
group by 性别;
select 学号, avg(成绩) as 平均成绩
from score
group by 学号
having avg(成绩)>60;
select 学号, count(课程号) as 选修课程数目
from score
group by 学号
having count(课程号)>=2;
select 姓名,count(*) as 人数
from student
group by 姓名
having count(*)>=2;
select 课程号
from score
where 成绩<60
order by 课程号 desc;
select 课程号, avg(成绩) as 平均成绩
from score
group by 课程号
order by 平均成绩 asc,课程号 desc;
select 学号
from score
where 课程号='0004' and 成绩 <60
order by 成绩 desc;
select 课程号, count(学号) as '选修人数'
from score
group by 课程号
having count(学号)>2
order by count(学号) desc,课程号 asc;
select 学号, avg(成绩) as 平均成绩
from score
where 成绩 <60
group by 学号
having count(课程号)>2;
select 学号 ,sum(成绩) as 总成绩
from score
group by 学号
order by sum(成绩);
select 学号 ,avg(成绩)
from score
group by 学号
having avg(成绩 )>60;
select student.学号,姓名
from student,score
where 成绩<60 and student.`学号`=score.`学号`;
select score.学号,姓名
from student,score
group by score.学号
having count(课程号)<(select count(课程号) from course);
select score.学号,姓名
from student,score
where student.`学号`=score.`学号`
group by score.`学号`
having count(课程号)=2;
select 学号,姓名
from student
where year(出生日期)=1990;
select 学号,timestampdiff(month ,出生日期 ,now())/12 as 年龄
from student;
select *
from student
where month (出生日期 )=month(now());
selecta.学号,a.姓名,count(b.课程号) as 选课数,sum(b.成绩) as 总成绩
from student as a left join score as b
on a.学号 = b.学号
group by a.学号;
select a.学号,a.姓名, avg(b.成绩) as 平均成绩
from student as a left join score as b
on a.学号 = b.学号
group by a.学号
having avg(b.成绩)>85;
select a.学号, a.姓名, c.课程号,c.课程名称
from student a
inner join score b on a.学号=b.学号
inner join course c on b.课程号=c.课程号;
select 课程号,
sum(case when 成绩>=60 then 1
else 0
end) as 及格人数,
sum(case when 成绩 < 60 then 1
else 0
end) as 不及格人数
from score
group by 课程号;
select a.课程号,b.课程名称,
sum(case when 成绩 between 85 and 100
then 1 else 0 end) as '[100-85]',
sum(case when 成绩 >=70 and 成绩<85
then 1 else 0 end) as '[85-70]',
sum(case when 成绩>=60 and 成绩<70
then 1 else 0 end) as '[70-60]',
sum(case when 成绩<60 then 1 else 0 end) as '[<60]'
from score as a right join course as b
on a.课程号=b.课程号
group by a.课程号,b.课程名称;
select a.学号,a.姓名
from student as a inner join score as b on a.学号=b.学号
where b.课程号='0003' and b.成绩>80;
思路如图:
select a.*,b.成绩
from student as a
inner join score as b
on a.学号 =b.学号
where b.成绩 <60 and b.课程号 =01
order by b.成绩 desc;
【知识点】分组+条件+排序+多表连接,思路如图
select a.教师号,a.教师姓名,avg(c.成绩)
from teacher as a
inner join course as b
on a.教师号= b.教师号
inner join score c on b.课程号= c.课程号
group by a.教师姓名
order by avg(c.成绩) desc;
【知识点】多表连接,思路如图
select a.姓名,b.成绩
from student as a
inner join score as b
on a.学号 =b.学号
inner join course c on b.课程号 =c.课程号
where b.成绩 <60 and c.课程名称 ='数学';
select a.姓名,c.课程名称,b.成绩
from student as a
Inner join score as b
on a.学号=b.学号
Inner join course c on b.课程号=c.课程号
where b.成绩>70;
【知识点】分组+条件+多表连接
翻译成大白话:计算每个学号不及格分数个数,筛选出大于2个的学号并找出姓名,平均成绩,思路如图:
select b.姓名,avg(a.成绩),a.学号
from score as a
Inner join student as b
on a.学号=b.学号
where a.成绩<60
group by a.学号
having count(a.学号)>=2;
select distinct a.学号,a.成绩,a.课程号
from score as a
inner join score as b
on a.学号 = b.学号
where a.成绩 = b.成绩 and a.课程号 != b.课程号;
select a.学号
from
(select 学号 ,成绩 from score where 课程号=01) as a
inner join
(select 学号 ,成绩 from score where 课程号=02) as b
on a.学号 =b.学号
inner join student c on c.学号 =a.学号
where a.成绩 >b.成绩 ;
select a.学号
from
(select 学号 ,成绩 from score where 课程号=01) as a
inner join
(select 学号 ,成绩 from score where 课程号=02) as b
on a.学号 =b.学号
inner join student c on c.学号 =a.学号
where a.成绩 >b.成绩 ;
select s.学号,s.姓名,a.学号,b.课程号,c.教师号,c.教师姓名
from student as s
inner join score as a
on s.`学号`=a.学号
inner join course b on a.课程号=b.课程号
inner join teacher c on b.教师号= c.教师号
where c.教师姓名 ='孟扎扎';
select 姓名,学号
from student
WHERE 学号 NOT IN(
select a.学号
from student as a
inner join score AS b
on a.学号 =b.学号
INNER JOIN course AS c ON b.课程号=c.课程号
INNER JOIN teacher AS d ON c.教师号=d.教师号
where d.教师姓名 ='孟扎扎');
select 学号, 姓名
from student
where 学号 not in
(select 学号 from score where 课程号=
(select 课程号 from course where 教师号 =
(select 教师号 from teacher where 教师姓名 ='孟扎扎')
)
);
select a.姓名,b.成绩
From student as a
Inner join score as b on a.学号=b.学号
Inner join course as c on b.课程号=c.课程号
Inner join teacher as d on c.教师号=d.教师号
where d.教师姓名='孟扎扎'
order by b.成绩 desc limit 1;
select 学号,姓名
from student
where 学号 in
(select distinct(学号) from score where 课程号 in
(select 课程号 from score where 学号=0001))
and 学号!=0001;
【知识点】多表连接 新建字段 ,思路如图
select a.学号,avg(a.成绩),
max(case when b.课程名称='数学' then a.成绩 else null end) AS 数学,
max(case when b.课程名称='语文' then a.成绩 else null end) AS 语文,
max(case when b.课程名称='英语' then a.成绩 else null end) AS 英语
from score as a
Inner join course as b
on a.课程号=b.课程号
group by a.学号;
【知识点】窗口函数排名,思路如图
select 学号,avg(成绩) as 平均成绩,row_number() over( order by avg(成绩) DESC) AS 名次
from score
group by 学号;
select 课程号,row_number() over(partition by 课程号 order BY 成绩) as 排名
from score;
【知识点】窗口函数排名+多表连接+条件
select a.课程号,b.姓名,a.成绩,a.ranking from (
select 课程号,学号,成绩,row_number() over(partition by 课程号 order by 成绩 desc) as ranking
from score) as a
inner join student b on a.学号=b.学号
where a.ranking<3;
select b.姓名,a.课程号,a.成绩
from (
select 课程号,学号,成绩,row_number() over( partition by 课程号 order by 成绩 desc) as ranking
from score ) as a
inner join student as b
on a.学号 =b.学号
where a.ranking in(2,3);
select b.姓名,a.课程号,a.成绩
from(
select 课程号,学号,成绩,
row_number() over( partition by 课程号 order by 成绩 desc) as 'ranking'
from score) as a
inner join student as b
on a.学号=b.学号
where a.ranking<4;