二刷经典SQL面试50题,当当,决定将解法重新梳理一遍。从一刷的磕磕绊绊,自己就是常见错误解法,到二刷思维很顺畅,90%可以独立解出正确答案,可见那句老话“熟能生巧”!其实SQL语言的逻辑是比较简单的,基本的增删改查可以应对工作中80%的内容,反而构建解决问题的思路,基于业务场景理解各表之间的关系才是重中之重~~~
一、建表并插入数据
经典50题的场景比较简单,有四张表:学生表、学科表、老师表和成绩表。
-- 建立学生表
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' , '赵雷' , '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('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
select * from Student;
select * from Score;
select * from Teacher;
select * from Course;
二、题目实战
TIPS:重要的是思路,可以先在脑海中或者Excel中模拟自己想要的表格是什么效果,再一步一步sql实现!!!
-- 1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点)
select a.s_id as s_no,s_name, a.s_score as '01',b.s_score as '02' from
(select * from Score where c_id ='01') a
join
(select * from Score where c_id ='02') b on a.s_id=b.s_id
join Student c on a.s_id=c.s_id
where a.s_score>b.s_score;
-- 2.查询平均成绩大于60分的学生的学号和平均成绩(简单,重点)
TIPS:用group by 分组聚合后,查询的字段不要包含分组与聚合运算之外的字段,是没意义的。
select s_id,round(avg(s_score),2) as '平均成绩'
from Score
group by s_id
having 平均成绩 >60
ORDER BY 平均成绩 desc;
-- 3.查询所有学生的学号、姓名、选课数、总成绩
TIPS:还是group by 中select之后的字段问题,严格来说:既不是统计值,也不是group by之后的分组条件,是不能出现在select之后的。
select a.s_id as '学号',s_name,count(c_id) as '选课数',
case
when sum(s_score) is null then 0
else sum(s_score)
end as '总成绩'
from Student a
left join Score b on a.s_id=b.s_id
group by a.s_id,a.s_name;
-- 4.查询姓“张”的老师的个数
select t_name,count(*) from Teacher group by t_name having t_name like '张%';
-- 5.查询没学过“张三”老师课的学生的学号、姓名(重点) !!!
-- 自己开始做了好几次都不对,还要再巩固!
TIPS:先找出学过张三老师课的学生,再用 not in 排除!
①、select s_id,s_name from Student where s_id not in
(select s_id from Score c where c.c_id=
(select c_id from Course a join Teacher b on a.t_id=b.t_id
where t_name ='张三'));
②、select s_name from Student where s_name not in (select s_name
from Student a left join Score b on a.s_id = b.s_id left join Course c on b.c_id = c.c_id
left join Teacher d on c.t_id = d.t_id where t_name = '张三'
group by a.s_id,s_name,t_name);
-- 6.查询学过“张三”老师所教的所有课的同学的学号、姓名(重点)
select a.s_id,s_name from Student a left join Score b on a.s_id = b.s_id
left join Course c on b.c_id = c.c_id
left join Teacher d on c.t_id = d.t_id
where t_name = '张三' group by a.s_id ,s_name,t_name;
-- 7.查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名(重点)
-- 学过01课程的同学 与 学过02课程的同学,取交集
-- 子查询,和第一题的思路相似
select a.s_id,s_name from
(select * from Score where c_id =01) a inner join
(select * from Score where c_id =02) b on a.s_id = b.s_id
left join Student c on a.s_id = c.s_id;
-- 8.查询课程编号为“02”的总成绩(不重点)
select c_id,sum(s_score) as '总成绩' from Score where c_id=02 group by c_id;
-- 9.查询 (所有的课程成绩) 小于60分的学生的学号、姓名
select a.s_id, s_name, min(s_score)as '最高分' from Score a
join Student b on a.s_id = b.s_id
group by a.s_id,s_name having max(s_score)<60;
-- 10.查询没有学全所有课的学生的学号、姓名(重点)
-- 子查询 (select count(distinct c_id) from Course)
select a.s_id,s_name,count(c_id) from Student a left join Score b on a.s_id = b.s_id
group by s_id having count( distinct c_id) !=(select count(distinct c_id) from Course);
-- 11.查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名(重点)
select a.s_id,s_name from Score a join Student b on a.s_id = b.s_id
where c_id in
(select c_id from Score where s_id =01)
GROUP BY a.s_id,s_name;
-- 12.查询和“01”号同学所学课程完全相同的其他同学的学号(重点) !!!
TIPS:01号同学学了01,02,03三门课
-- ① 选出的课不在01,02,03三门课--排除
-- ② 剩下的同学肯定选了01,02,03三门课,再判断所学课程数=3
select * from Student
weher s_id in ( select s_id from Score where s_id !=01
group by s_id
having count(distinct c_id) = (select count(distinct c_id ) from Score where s_id =01))
and s_id not in
(select s_id from Score where c_id
not in (select c_id from Score where s_id=01));
-- 15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(重点)
select c.s_id,s_name,平均成绩,不及格科目数
from (select a.s_id,s_name,avg(s_score) as '平均成绩'
from Student a join Score b on a.s_id = b.s_id
group by s_id) c
join
(select s_id,count(*) as '不及格科目数' from Score
where s_score<60 group by s_id having count(*)>=2) d
on c.s_id =d.s_id
-- 16.检索"01"课程分数小于60,按分数降序排列的学生信息(和34题重复,不重点)
select a.s_id,s_name,c_id,s_score
from Score a join Student b on a.s_id = b.s_id
where c_id=01 and s_score <60 order by s_score desc;
-- 17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(重重点与35一样)
-- 把语文、数学、英语各门课程成绩横着排出来,有点厉害哦~
select s_id as '学号',avg(s_score)as '平均成绩',
max(case when c_id =01 then s_score else null end) as '语文',
max(case when c_id =02 then s_score else null end) as '数学',
max(case when c_id =03 then s_score else null end) as '英语'
from Score
group by s_id
order by avg(s_score) desc;
-- 18.查询各科成绩最高分、最低分和平均分 (超级重点) !!!:
以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select a.c_id as '课程id',c_name as '课程名称',
avg(s_score) as '平均分',max(s_score) as '最高分',min(s_score ) as '最低分' ,
sum(case when s_score >=60 then 1 else 0 end)/count(*) as '及格率',
sum(case when s_score >=70 and s_score <80 then 1 else 0 end)/count(*) as '中等率',
sum(case when s_score >=80 and s_score <90 then 1 else 0 end)/count(*) as '优良率',
sum(case when s_score >=90 then 1 else 0 end)/count(*) as '优秀率'
from Score a
join Course b
on a.c_id = b.c_id
group by a.c_id;
-- 19. 按各科成绩进行排序,并显示排名 (重点!!!) 涉及到窗口函数,但是我的navicat运行不出来。
select s_id,c_id,s_score,rank() over(order by s_score desc) as 'rank' from window_test
from Score ;
-- 20. 查询学生的总成绩并进行排名(不重点)
select s_id,sum(s_score) from Score group by s_id order by sum(s_score) desc;
-- 21. 查询不同老师所教不同课程平均分从高到低显示(不重点)
select t_id,c_name,avg(s_score) from Score a
join Course b on a.c_id = b.c_id group by t_id,c_name
order by avg(s_score) desc;
-- 22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(重要 25类似)窗口函数
select * from
(select s_name,c_id,s_score,row number() over(partition by c_id order by s_score desc) as 'm'
from Score a join Student b on a.s_id = b.s_id) c where m in (2,3);
-- 23.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称(重点和18题类似)
-- 也是按条件计数
select a.c_id as '课程id',c_name as '课程名称',
sum(case when s_score <60 then 1 else 0 end) as '不及格',
sum(case when s_score >=60 and s_score<70 then 1 else 0 end) as '中等',
sum(case when s_score >=80 and s_score<90 then 1 else 0 end) as '优良',
sum(case when s_score >=90 then 1 else 0 end) as '优秀'
from Score a join Course b on a.c_id = b.c_id group by a.c_id,c_name;
-- 24、查询学生平均成绩及其名次(同19题,重点) 涉及窗口函数
select s_name,avg(s_score),rank() over(order by avg(s_score) desc) as 'rank'
from Student a join Score b on a.s_id = b.s_id group by s_name;
-- 26、查询每门课程被选修的学生数(不重点)
select c_id,count(distinct s_id) as '选课人数' from Score group by c_id;
-- 27、 查询出只有两门课程的全部学生的学号和姓名(不重点)
select s_id,count(distinct c_id) as '课程数' from Score group by s_id having 课程数 = 2;
-- 28、查询男生、女生人数(不重点)
select s_sex,count(*) from Student group by s_sex;
-- 29 查询名字中含有"风"字的学生信息(不重点)
select * from Student where s_name like '%风%' ;
-- 31、查询1990年出生的学生名单(重点year)
select * from Student where year(s_birth) = 1990;
-- 32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩(不重要)
select a.s_id,s_name,avg(s_score) from Student a join Score b on a.s_id =b.s_id
group by a.s_id,s_name having avg(s_score)>=85;
-- 33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列(不重要)
select a.c_id,c_name,avg(s_score) from Score a join Course b on a.c_id = b.c_id
group by a.c_id order by avg(s_score)asc,a.c_id desc;
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数(不重点)
select s_name,s_score from Score a join Course b on a.c_id = b.c_id
join Student c on a.s_id = c.s_id where c_name='数学' and s_score <60;
-- 35、查询所有学生的课程及分数情况(重点)
select a.s_id,s_name,
max(case when c_name='语文' then s_score else null end) as '语文',
max(case when c_name='数学' then s_score else null end) as '数学',
max(case when c_name='英语' then s_score else null end) as '英语'
from Student a left join Score b on a.s_id = b.s_id
join Course c on b.c_id = c.c_id
group by a.s_id,s_name;
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数(重点)
select s_name,
max(case when c_name='语文' then s_score else null end) as '语文',
max(case when c_name='数学' then s_score else null end) as '数学',
max(case when c_name='英语' then s_score else null end) as '英语'
from Score a join Student b on a.s_id=b.s_id
join Course c on c.c_id = a.c_id
where s_score>=70
group by s_name;
-- 37、查询不及格的课程并按课程号从大到小排列(不重点)
select a.c_id,s_score from Score a join Course b on a.c_id = b.c_id
where s_score<60 order by a.c_id desc;
-- 38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名(不重要)
select a.s_id,s_name,s_score from Student a join Score b on a.s_id = b.s_id
where b.c_id=03 and s_score >80;
-- 39、求每门课程的学生人数(不重要)
select c_id,count(*) from Score group by c_id;
-- 40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩(重要top)
select s_name,s_score from Score a join Student b on a.s_id =b.s_id
where c_id = (select c_id from Course
where t_id = (select t_id from Teacher where t_name = '张三'))
order by s_score desc limit 0,1;
-- 41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (重点)
select a.s_id from (select s_id,s_score
from Score a group by s_id,s_score) a group by s_id having count(*) =1;
-- 42、查询每门功成绩最好的前两名(同22和25题)
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列(不重要)
select c_id,count(distinct s_id) from Score group by c_id
having count(distinct s_id)>5 order by count(s_id) desc,c_id;
-- 44、检索至少选修两门课程的学生学号(不重要)
select s_id,count(c_id) from Score group by s_id having count(c_id) >=2;
-- 45、 查询选修了全部课程的学生信息
select s_id,count(distinct c_id) from Score group by s_id
having count(distinct c_id)=(select count(c_id) from Course);
-- 46、查询各学生的年龄(精确到月份)
select s_id,s_name,FLOOR(DATEDIFF(now(),s_birth)/365) as '年龄' from Student;
-- 47、查询没学过“张三”老师讲授的任一门课程的学生姓名
select s_id,s_name from Student where s_id not in
(select s_id from Score where c_id=
(select c_id from Course where t_id=
(select t_id from Teacher where t_name = '张三')));
-- 48、查询下周过生日的学生
select * from Student
where week(concat('2020-',substring(s_birth,6,5)),1)= week('2020-5-15',1)+1;
-- 49、查询本月过生日的学生
select * from Student where month(s_birth)=month(now());
-- 50、查询下个月过生日的学生
select * from Student where
case when month(now())=12 then month(s_birth) =1
else month(s_birth) = month(now()) +1 end;
三、说明
题目来自于:
常见的SQL面试题:经典50题 - 知乎( https://zhuanlan.zhihu.com/p/38354000)