SQL面试必会50题(含答案和学习链接)

最近在刷 sql 题,刷完了网上的 SQL 面试必会 50 题,现把我的答案和思路整理如下,供大家参考。

这是目录

      • 一、创建四张表
      • 二、SQL面试必会50题(不含答案)
      • 三、SQL面试必会50题(含答案)
      • 四、学习资料

一、创建四张表

SQL面试必会50题(含答案和学习链接)_第1张图片
如图,以上学生表、成绩表、课程表、教师表是我们所有题目的基础表,创建的语句如下:

--学生表
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);

以及相应的表结果
(1)学生表
SQL面试必会50题(含答案和学习链接)_第2张图片
(2)成绩表
SQL面试必会50题(含答案和学习链接)_第3张图片
(3)课程表
在这里插入图片描述
(4)教师表
在这里插入图片描述

二、SQL面试必会50题(不含答案)

这里专门把50题的题目总合在一起了,方便直接刷题的朋友,想要对答案的话,可以参考本文的第三节。

1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)
2、查询平均成绩大于60分的学生的学号和平均成绩
3、查询所有学生的学号、姓名、选课数、总成绩
4、查询姓“猴”的老师的个数
5、查询没学过“张三”老师课的学生的学号、姓名
6、查询学过“张三”老师所教的所有课的同学的学号、姓名
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
8、查询课程编号为“02”的总成绩
9、查询所有课程成绩小于60分的学生的学号、姓名
10、查询没有学全所有课的学生的学号、姓名
11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)
12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)
13、查询没学过"张三"老师讲授的任一门课程的学生姓名 和47题一样
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索"01"课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩()
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
19、按各科成绩进行排序,并显示排名()
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(重要 25类似)
23、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录(不考虑成绩并列情况)
26、查询每门课程被选修的学生数
27、查询出只有两门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
31、查询1990年出生的学生名单
32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
35、查询所有学生的课程及分数情况
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
37、查询不及格的课程并按课程号从大到小排列
38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
39、求每门课程的学生人数
40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)
42、查询每门功成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询没学过“张三”老师讲授的任一门课程的学生姓名
48、查询两门以上不及格课程的同学的学号及其平均成绩
49、查询本月过生日的学生
50、查询下一个月过生日的学生

三、SQL面试必会50题(含答案)

下面对这 50 题分别列出我的 SQL 语句与查询到的结果。以下的结果都和网上的答案比较了的,能保证一定的正确性。

1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)

-- 考察点:将子查询的表内连接起来
-- 思路:将学号,01课程成绩,02课程成绩按行显示出来,然后再用排除条件
select distinct s.s_id,st.s_name,s1.s_score as "01成绩",s2.s_score as "02成绩" 
from score as s
inner join (select * from score where c_id = '01') as s1 on s.s_id = s1.s_id
inner join (select * from score where c_id = '02') as s2 on s.s_id = s2.s_id
inner join student as st on s.s_id = st.s_id
where s1.s_score > s2.s_score

SQL面试必会50题(含答案和学习链接)_第4张图片
2、查询平均成绩大于60分的学生的学号和平均成绩

-- 考察点:聚集函数avg(),数据分组groupby,过滤分组
select s_id,avg(s_score) as "平均成绩" 
from score
group by s_id
having 平均成绩 > 60

SQL面试必会50题(含答案和学习链接)_第5张图片
3、查询所有学生的学号、姓名、选课数、总成绩

-- 考察点:使用子查询作为计算字段,case when用法,聚集函数sum()
select a.s_id,a.s_name,
count(b.c_id) as "选课数",
sum(case when b.s_score is null then 0 else b.s_score END) as "总成绩"
from student as a 
left join score as b on a.s_id = b.s_id
group by a.s_id

SQL面试必会50题(含答案和学习链接)_第6张图片

4、查询姓“猴”的老师的个数

-- 考察点:操作符 like,通配符 % 的使用
select count(*) from teacher
where t_name like '猴%'

在这里插入图片描述
5、查询没学过“张三”老师课的学生的学号、姓名

-- 考察点:not in
-- 思路:先找出学过“张三”老师课的学生学号,然后利用反向排除法
select s_id,s_name 
from student where s_id not in (
select s.s_id
from student as s
inner join score as sc on s.s_id = sc.s_id
inner join course as c on c.c_id = sc.c_id
inner join teacher as t on t.t_id = c.t_id
where t_name = '张三')

在这里插入图片描述

6、查询学过“张三”老师所教的所有课的同学的学号、姓名

-- 考察点:in , 子查询
select s_id,s_name 
from student where s_id in (
select s.s_id
from student as s
inner join score as sc on s.s_id = sc.s_id
inner join course as c on c.c_id = sc.c_id
inner join teacher as t on t.t_id = c.t_id
where t_name = '张三')

SQL面试必会50题(含答案和学习链接)_第7张图片

7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

-- 思路:和第1题类似,将子表连接起来
select distinct s.s_id,st.s_name 
from score as s
inner join (select * from score where c_id = '01') as s1 on s.s_id = s1.s_id
inner join (select * from score where c_id = '02') as s2 on s.s_id = s2.s_id
inner join student as st on s.s_id = st.s_id

SQL面试必会50题(含答案和学习链接)_第8张图片
8、查询课程编号为“02”的总成绩

-- 考察点:聚集函数sum()
select c_id, sum(s_score)
from score
group by c_id
having c_id = '02'

在这里插入图片描述
9、查询所有课程成绩小于60分的学生的学号、姓名

-- 思路:最大的一门课都小于60,也就是所有的都小于60了,另外,有的同学一门课都没有考过试,所以我后面多加了一个姓名不在成绩表里的选项
select a.s_id,a.s_name 
from student as a 
where (select max(s_score) from score group by s_id having s_id = a.s_id ) < 60 
or a.s_id not in (select distinct s_id from score)

在这里插入图片描述

10、查询没有学全所有课的学生的学号、姓名

-- 考察点:过滤分组,王菊一门课都没有选,但是也应该是答案,所以要用左连接
select a.s_id,a.s_name
from student as a left join score as b
on a.s_id = b.s_id
group by a.s_id
having count(b.c_id) < (select count(distinct c_id) from score)

SQL面试必会50题(含答案和学习链接)_第9张图片
11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)

-- 考察点: distinct,子查询
select s_id,s_name 
from student
where s_id in (select distinct s_id from score where c_id in (select c_id from score where s_id = '01') and s_id != '01')

SQL面试必会50题(含答案和学习链接)_第10张图片
12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)

-- 思路:从选课表找出所学的课不在01号同学学的课的学号,从选课表中找出所学的课程数和01号同学学的课程数一样的同学的学号,前者不要,后者保留
select s_id,s_name from student 
where s_id in (
select s_id from score
where s_id != '01'
group by s_id
having count(*) = (select count(*) from score where s_id = '01'))
and s_id not in(select distinct s_id from score where c_id not in (select c_id from score where s_id = '01'))

SQL面试必会50题(含答案和学习链接)_第11张图片
13、查询没学过"张三"老师讲授的任一门课程的学生姓名

-- 思路:使用排除法,去掉学过张三老师课程的学生,和13题一样
select s_id,s_name 
from student 
where s_id not in(
select s_id from score as s
inner join course as c on s.c_id = c.c_id
inner join teacher as t on t.t_id = c.t_id
where t.t_name = '张三')

在这里插入图片描述

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select a.s_id,a.s_name,avg(b.s_score)
from student as a,score as b
where a.s_id = b.s_id and a.s_id in (
select a.s_id 
from student as a,score as b
where a.s_id = b.s_id and b.s_score < 60
group by a.s_id
having count(*) >= 2)
group by a.s_id

在这里插入图片描述16、检索"01"课程分数小于60,按分数降序排列的学生信息

select  a.*
from student as a,score as b
where a.s_id = b.s_id and b.c_id = '01' and b.s_score < 60
order by b.s_score desc

在这里插入图片描述

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)

-- 思路: 这题需要横排显示每个学生的所有成绩以及平均,不是竖排显示的
-- 方法一:利用自查询 作为计算字段
select s_id,s_name,
(select s_score from score where s_id = a.s_id and c_id = '01') as '01_score',
(select s_score from score where s_id = a.s_id and c_id = '02') as '02_score',
(select s_score from score where s_id = a.s_id and c_id = '03') as '03_score',
(select avg(s_score) from score where s_id = a.s_id) as 'avg_score'
from student as a
order by avg_score desc
-- 方法二,case when
select a.s_id "id",
b.s_name "姓名",
MAX(case when a.c_id='01' then s_score else null end) "01_score",
MAX(case when a.c_id='02' then s_score else null end) "02_score",
MAX(case when a.c_id='03' then s_score else null end) "03_score",
avg(a.s_score) "平均成绩"
from score as a,student as b
where a.s_id = b.s_id
group by a.s_id
order by "平均成绩" 

SQL面试必会50题(含答案和学习链接)_第12张图片
– 错误,竖排显示

select a.s_id,a.s_name,b.c_id,b.s_score,c.avg
from student as a,score as b,(
select a.s_id,a.s_name,avg(b.s_score) as avg
from student as a,score as b
where a.s_id = b.s_id
group by a.s_id) as c
where a.s_id = b.s_id and a.s_id = c.s_id
order by c.avg desc

SQL面试必会50题(含答案和学习链接)_第13张图片
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

select 
a.c_id "课程ID",
b.C_name "课程name",
avg(s_score) "平均分",
MAX(s_score) "最高分",
Min(s_score) "最低分",
(select count(*) from score where c_id = a.c_id and s_score >= 60)/count(*) "及格率",
(select count(*) from score where c_id = a.c_id and s_score between 70 and 80)/count(*) "中等率",
(select count(*) from score where c_id = a.c_id and s_score between 80 and 90)/count(*) "优良率", 
(select count(*) from score where c_id = a.c_id and s_score >= 90)/count(*) "优秀率" 
from score as a, course as b
where a.c_id = b.c_id
group by a.c_id

SQL面试必会50题(含答案和学习链接)_第14张图片
19、按各科成绩进行排序,并显示排名(难)

-- 思路:窗口函数里面的排序函数rank()、row_number()、dense_rank()
-- (1)按照每个科目进行一次排名
select s_id,c_id,s_score,row_number() over (partition by c_id order by s_score desc) as 'm_rank'
from score
order by c_id

SQL面试必会50题(含答案和学习链接)_第15张图片

-- (2)对所有分数进行一个总的排名
select s_id,c_id,s_score,row_number() over (order by s_score desc) as 'm_rank'
from score
order by m_rank

SQL面试必会50题(含答案和学习链接)_第16张图片
20、查询学生的总成绩并进行排名

-- 考察点:窗口函数 rank()
select a.s_id,a.s_name,
(case when sum(s_score) is Null then 0 else sum(s_score) end) as "总分",
rank() over (order by sum(s_score) desc) as "rank"
from student as a left join score as b
on a.s_id = b.s_id
group by a.s_id

SQL面试必会50题(含答案和学习链接)_第17张图片
21、查询不同老师所教不同课程平均分从高到低显示

-- 考察点:聚集函数 avg(),降序 desc
select a.t_id,a.t_name,b.c_id,b.c_name,avg(s_score) as "s_avg"
from teacher as a,course as b, score as c
where a.t_id = b.t_id and b.c_id = c.c_id
group by c_id
order by s_avg desc

在这里插入图片描述
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(重要 25类似)

-- 方法一,比较粗鲁,直接连接三个课程的第2名和第3名的信息,用union all 是为了不删除重复记录
(select c_id,a.s_id,s_name,s_score,row_number() over (order by s_score desc) as 'rank'
from score as a right join student as b
on b.s_id = a.s_id
where c_id = '01'
order by c_id,s_score desc
limit 1,2)
union all
(select c_id,a.s_id,s_name,s_score,row_number() over (order by s_score desc) as 'rank'
from score as a right join student as b
on b.s_id = a.s_id
where c_id = '02'
order by c_id,s_score desc
limit 1,2)
union all
(select c_id,a.s_id,s_name,s_score,row_number() over (order by s_score desc) as 'rank'
from score as a right join student as b
on b.s_id = a.s_id
where c_id = '03'
order by c_id,s_score desc
limit 1,2)
-- 方法二,和19题一样,用row_number,先得到所有的排序,然后再取出2,3的排序
select c.* from
(select c_id,b.s_name,s_score,row_number() over (partition by c_id order by s_score desc) as "m_rank"
from score as a,student as b
where a.s_id = b.s_id) as c
where m_rank in (2,3)

SQL面试必会50题(含答案和学习链接)_第18张图片
23、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称

-- 思路:和18题类似
select 
distinct a.c_id,
b.c_name "课程名称",
(select count(*) from score where c_id = a.c_id) "总数",
(select count(*) from score where c_id = a.c_id and s_score between 85 and 100) "100-85",
(select count(*) from score where c_id = a.c_id and s_score between 70 and 85) "85-70",
(select count(*) from score where c_id = a.c_id and s_score between 60 and 70) "70-60",
(select count(*) from score where c_id = a.c_id and s_score < 60) "<60"
from score as a, course as b
where a.c_id = b.c_id

SQL面试必会50题(含答案和学习链接)_第19张图片
24、查询学生平均成绩及其名次

-- 考察点:row_number 的用法,记得里面可以指定是升序还是降序排列
select a.s_id,avg(s_score),row_number() over (order by avg(s_score) desc) as "rank"
from student as a,score as b
where a.s_id = b.s_id
group by a.s_id
order by avg(s_score) desc

SQL面试必会50题(含答案和学习链接)_第20张图片

25、查询各科成绩前三名的记录(不考虑成绩并列情况)

-- 思路: 与22题类似,用row_number()
select c.* from
(select c_id,a.s_id,s_score,row_number() over (partition by c_id order by s_score desc) as "m_rank"
from score as a,student as b
where a.s_id = b.s_id) as c
where m_rank in (1,2,3)

SQL面试必会50题(含答案和学习链接)_第21张图片

26、查询每门课程被选修的学生数

select c_id,count(*) as "学生数"
from score
group by c_id

SQL面试必会50题(含答案和学习链接)_第22张图片
27、查询出只有两门课程的全部学生的学号和姓名

select s_id,s_name 
from student where s_id in(
select s_id
from score
group by s_id
having count(*) = 2)

在这里插入图片描述

28、查询男生、女生人数

select (select count(*) from student where s_sex = '男') as "男生数",
(select count(*) from student where s_sex = '女') as "女生数"

在这里插入图片描述
29、查询名字中含有"风"字的学生信息

-- 考察点:like语句,%代表搜索模式中给定位置的 0个、1个或多个字符。_表示能匹配单个字符
select * 
from student
where s_name like '%风%'

在这里插入图片描述
31、查询1990年出生的学生名单

-- 考察点:Year()、Month()、Day()
-- 变形题:查询2月出生的学生名单,查询2号出生的学生名单
select * from student
where year(s_birth) = 1990

SQL面试必会50题(含答案和学习链接)_第23张图片
32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select a.s_id,b.s_name,avg(s_score) as avg_score
from score as a,student as b
where a.s_id = b.s_id
group by a.s_id,b.s_name
having avg_score >= 85

在这里插入图片描述
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

select c_id,avg(s_score) as avg_score
from score
group by c_id
order by avg_score asc,c_id desc

在这里插入图片描述

34、查询课程名称为"数学",且分数低于60的学生姓名和分数

-- 思路:和16题一样
select a.s_name,b.s_score
from student as a,score as b,course as c
where a.s_id = b.s_id and b.c_id = c.c_id and c.c_name = '数学' and s_score < 60

在这里插入图片描述

35、查询所有学生的课程及分数情况

-- 思路:和17题一样
-- 方法一:子查询
select 
distinct c.s_id,
c.s_name,
(select (case when s_score is NULL then 0 else s_score end) from score as a,course as b where a.c_id = b.c_id and a.s_id = c.s_id and c_name = '语文') "语文",
(select (case when s_score is NULL then 0 else s_score end) from score as a,course as b where a.c_id = b.c_id and a.s_id = c.s_id and c_name = '数学') "数学",
(select (case when s_score is NULL then 0 else s_score end) from score as a,course as b where a.c_id = b.c_id and a.s_id = c.s_id and c_name = '英语') "英语"
from student as c,score as d
where c.s_id = d.s_id
-- 方法二:case when
select a.s_id "id",
b.s_name "姓名",
MAX(case when a.c_id='01' then s_score else null end) "01_score",
MAX(case when a.c_id='02' then s_score else null end) "02_score",
MAX(case when a.c_id='03' then s_score else null end) "03_score",
avg(a.s_score) "平均成绩"
from score as a,student as b
where a.s_id = b.s_id
group by a.s_id
order by "平均成绩" 

SQL面试必会50题(含答案和学习链接)_第24张图片

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

select c.s_id, c.s_name, b.c_name, a.s_score 
from score as a, course as b,student as c
where a.c_id = b.c_id and a.s_id = c.s_id and s_score > 70

SQL面试必会50题(含答案和学习链接)_第25张图片

37、查询不及格的课程并按课程号从大到小排列

select a.s_id,a.s_name,s.c_id,c.c_name,s.s_score 
from score as s
inner join course as c on s.c_id = c.c_id
inner join student as a on a.s_id = s.s_id
where s.s_score < 60
order by s.c_id desc

SQL面试必会50题(含答案和学习链接)_第26张图片
38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名

select s.s_id,st.s_name,s.s_id,s.s_score 
from score as s
inner join student as st on s.s_id = st.s_id
where s.c_id = '03' and s.s_score > 80

在这里插入图片描述
39、求每门课程的学生人数

select c_id,count(c_id) as total_student
from score
group by c_id

在这里插入图片描述
40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩

-- 考察点:limit 0,1,mysql中起始位置从0开始
select st.s_id,st.s_name,s.c_id,c.c_name,s.s_score,t.t_id,t.t_name
from score as s
inner join course as c on s.c_id = c.c_id
inner join student as st on st.s_id = s.s_id
inner join teacher as t on t.t_id = c.t_id
where t.t_name = '张三'
order by s.s_score desc
limit 0,1

在这里插入图片描述

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)

-- 思路:我理解的题意是这个学生至少修了两门课以上,且这些课程成绩都相同
select a.s_id,st.s_name,a.s_score
from
(select s_id,s_score from score
where s_id in (select s_id from score group by s_id having count(*) >= 2)
group by s_id,s_score) as a
inner join student as st on a.s_id = st.s_id
group by s_id
having count(*) = 1

在这里插入图片描述

42、查询每门功成绩最好的前两名

-- 思路:同22和25题,row_number(),要先把row_number的结果查询出来形成一个表,才能使用m_rank in (1,2),不然直接使用是不行的!!!
select c.* from 
(select s.c_id, st.s_name, s.s_score,row_number() over (partition by s.c_id order by s.s_score desc) as "m_rank"
from score as s 
inner join student as st on s.s_id = st.s_id) as c
where m_rank in (1,2)

SQL面试必会50题(含答案和学习链接)_第27张图片
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select c_id,count(*) from score
group by c_id
having count(*) > 5
order by count(*) desc,c_id asc

在这里插入图片描述

44、检索至少选修两门课程的学生学号

select s_id from score
group by s_id
having count(*) >= 2

在这里插入图片描述
45、查询选修了全部课程的学生信息

select * from student as a 
where (select count(distinct c_id) from course) = (select count(*) from score as s where s.s_id = a.s_id)

SQL面试必会50题(含答案和学习链接)_第28张图片
46、查询各学生的年龄

-- 方法一:year() 直接取得年份
select st.*, 2020 - year(st.s_birth) as "年龄"
from student as st

SQL面试必会50题(含答案和学习链接)_第29张图片

-- 方法二:round()四舍五入,now()现在的时间,datadiff(日期1,日期2),返回日期2-日期1的天数
select st.*, round(datediff(now(),st.s_birth)/365) as "年龄"
from student as st

SQL面试必会50题(含答案和学习链接)_第30张图片

-- 方法三:floor()向下取整
select st.*, floor(datediff(now(),st.s_birth)/365) as "年龄"
from student as st

SQL面试必会50题(含答案和学习链接)_第31张图片
47、查询没学过“张三”老师讲授的任一门课程的学生姓名

-- 思路:使用排除法,去掉学过张三老师课程的学生,和13题一样
select s_id,s_name 
from student 
where s_id not in(
select s_id from score as s
inner join course as c on s.c_id = c.c_id
inner join teacher as t on t.t_id = c.t_id
where t.t_name = '张三')

在这里插入图片描述

48、查询两门以上不及格课程的同学的学号及其平均成绩

select s_id as "学号",
count(*) as "不及格课程数",
avg(s_score) as "平均成绩" 
from score
where s_score < 60
group by s_id
having count(*) >= 2

在这里插入图片描述

49、查询本月过生日的学生

-- 我这里的now()是2020年9月7日
select * from student
where (select month(now()) = month(s_birth))

在这里插入图片描述

50、查询下一个月过生日的学生

-- 我这里的now()是2020年9月7日
select * from student
where (select (month(now()) + 1 ) % 12 = month(s_birth) )

在这里插入图片描述

四、学习资料

  • 这是 SQL面试必会50题 的初始来源
  • 我不会写的题目,还看了 SQL面试50题-视频讲解版本
  • 初始 SQL 的语法复习我看的是《SQL必知必会》
  • 你还可以继续做牛客网SQL实战 70 题和力扣数据库题

你可能感兴趣的:(sql)