文章目录
1. 数据准备
1、有以下4张表:
学生表:student(学号,学生姓名,出生年月,性别)
成绩表:score(学号,课程号,成绩)
课程表:course(课程号,课程名称,教师号)
教师表:teacher(教师号,教师姓名)
2、创建表结构并插入数据:
drop table if exists Student ;
-- 创建学生表
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);
3、数据库表:
2. 50道SQL面试题
1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)
select distinct a.s_id
from ( select * from score where c_id=01) a
join ( select * from score where c_id=02) b
on a.s_score>b.s_score
where a.s_id=b.s_id;
思路:
(1) 先求出课程编号为“01”的学生课程成绩
(2) 再求出课程编号为“02”的学生课程成绩
(3) 将这两张表连接查询,条件是a.s_id = b.s_id,代表是同一个学生
2、查询平均成绩大于60分的学生的学号和平均成绩
(1) 求每个学生的平均成绩
(2) 使用having对每个学生的平均成绩过滤
3、查询所有学生的学号、姓名、选课数、总成绩
select a.s_name,b.s_id,b.count,b.sum_score
from student a
left join (select s_id,count(*) count,sum(s_score) sum_score
from score
group by s_id) b
on a.s_id=b.s_id;
思路:
(1) 先查询出所有学生的学号,选课数,总成绩
(2) 将步骤1中查询出来的这张表作为一个字表与student表进行连接查询
简化查询:可以不使用子查询,直接将两张表连接查询
select a.s_name,b.s_id,count(b.s_id) count,sum(b.s_score) sum_score
from student a
left join score b
on a.s_id=b.s_id
group by s_id;
4、查询姓“猴”的老师的个数
mysql> select count(t_name) from teacher where t_name='猴';
5、查询没学过“张三”老师课的学生的学号、姓名
select s_id,s_name from student where s_id not in (select a.s_id
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id
join teacher d on c.t_id=d.t_id
where d.t_name='张三');
思路:
(1) 首先,将4张表连接,查询出选过张三老师课的学生的学号
select a.s_id
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id
join teacher d on c.t_id=d.t_id
where d.t_name='张三';
(2) 将步骤1中的查询作为一个子查询,与student表精进行连接查询
6、查询学过“张三”老师所教的所有课的同学的学号、姓名
select b.s_id,b.s_name
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id
join teacher d on c.t_id=d.t_id
where d.t_name='张三';
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
方法1 :
select s_id,s_name
from student
where s_id in(select s_id
from score
where c_id='01')
and s_id
in(select s_id
from score
where c_id='02');
思路:
(1) 分别查询学过编号为“01” 和“02”的课程的学生的学号
select s_id from score where c_id='01'
select s_id from score where c_id='02'
(2) 学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
方法2 :
select distinct s.s_id,s.s_name from score a
join (select s_id from score where c_id='01') b on a.s_id=b.s_id
join (select s_id from score where c_id='02') c on a.s_id=c.s_id
join student s on a.s_id=s.s_id;
思路:
(1) 先将三张表连接查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号
select distinct a.s_id
from score a
join (select s_id from score where c_id='01') b on a.s_id=b.s_id
join (select s_id from score where c_id='02') c on a.s_id=c.s_id;
(2) 将student表和步骤1的查询结果连表查询出sname
8、查询课程编号为“02”的总成绩
mysql> select sum(s_score) from score group by c_id having c_id='02';
9、查询所有,课程成绩小于60分的学生的学号、姓名
select distinct a.s_id,a.s_name
from student a
left join score b on a.s_id=b.s_id
where b.s_score<60 or a.s_id not in (select s_id from score);
(1) 查询课程成绩小于60分的学生的学号
(2) 将步骤1的查询结果和student表联表查询出姓名,同时考虑没有考试成绩的学生
10、查询没有学全所有课的学生的学号、姓名
select a.s_id,a.s_name
from student a
left join score b on a.s_id=b.s_id
group by a.s_id,a.s_name
having count(b.c_id)<(select count(c_id) from course);
11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)
select distinct a.s_id,a.s_name
from student a
right join score b on a.s_id=b.s_id
where b.c_id in (select c_id from score where s_id='01') and a.s_id !='01';
(1) 查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号
(2) 根据步骤1的结果与student表联表查询出姓名,同时排除学号1的学生
12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)
select s_id
from score where s_id!='01'
group by s_id
having count(s_id) = (select count(*) from score where s_id='01');
13、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_id,s_name
from student
where s_id not in (select a.s_id
from score a
join course b on a.c_id=b.c_id
join teacher c on b.t_id=c.t_id
where c.t_name='张三');
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.s_id,a.s_name,avg(s_score) avg_score
from student a
join score b on a.s_id=b.s_id
where b.s_score<60
group by s_id
having count(b.s_id)>=2;
(1) 求出每个学生成绩不及格的课程数量及平均成绩
(2) 利用步骤1的结果与student表联表查询出学生姓名,其中课程成绩不及格数量要大于等于2门
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select a.s_id,a.s_name
from student a
join score b on a.s_id=b.s_id
where b.s_score<60 and b.c_id='01'
order by b.s_score desc;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)
select a.s_id,a.s_score,b.avg_score
from score a
left join (select s_id, avg(s_score) avg_score
from score
group by s_id) b
on a.s_id =b.s_id
order by avg_score desc;
注意:上述虽然表达出了题目的意思,但是最好以横排形式展示,可以使用select嵌套子查询
方法2 :
select s_id,s_name,
(select s_score from score where score.s_id=student.s_id and c_id='01') 01_score,
(select s_score from score where score.s_id=student.s_id and c_id='02') 02_score,
(select s_score from score where score.s_id=student.s_id and c_id='03') 03_score,
(select avg(s_score) from score where score.s_id=student.s_id) avg_score
from student
order by avg_score desc;
使用select子查询嵌套查询时,需要将有歧义的字段区分出来比如: score.s_id=student.s_id
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
(1) 查询各科成绩最高分、最低分和平均分
select a.c_id,a.c_name, avg(b.s_score) avg_score,min(b.s_score) min_score,max(b.s_score) max_score
from course a j
oin score b on a.c_id=b.c_id
group by b.c_id;
(2) 每一个科目的及格率,中等率,优良率,优秀率需要使用select后嵌套子查询获得
select
b.c_id,b.c_name, avg(a.s_score) avg_score,min(a.s_score) min_score,max(a.s_score) max_score,
-- 注意:这里相当于将 score a和course b联表查询后再与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 a
join course b
on a.c_id=b.c_id
group by a.c_id;
19、查询学生的总成绩并进行排名
select a.s_id,a.s_name,(case when sum(b.s_score) is null then 0 else sum(b.s_score) end) sum_score
from student a
left join score b on a.s_id=b.s_id
group by b.s_id
order by sum_score desc;
20、查询不同老师所教不同课程平均分,从高到低显示
select c_id,avg(s_score) avg_score
from score
group by c_id
order by avg_score desc;
21、查询学生平均成绩及其名次
select s_id,avg(s_score) avg_score,
row_number () over( order by avg(s_score) desc) row_num
from score
group by s_id;
22、按各科成绩进行排序,并显示排名(难)
序号函数:row_number() / rank() / dense_rank()
partition子句:窗口按照那些字段进行分组,窗口函数在不同的分组上分别执行。下面的例子就按照 c_id进行了分组。在每个 c_id上,按照order by的顺序分别生成从1开始的顺序编号。
order by子句:按照哪些字段进行排序,窗口函数将按照排序后的记录顺序进行编号。可以和partition子句配合使用,也可以单独使用。如果没有partition子句,则会按照所有score排序来生成序号。
-- partition by c_id 按照c_id进行分组
-- order by s_score desc 按照s_score倒叙排序
select c_id,s_id,row_number() over(partition by c_id order by s_score desc) num_row
from score;
23、查询每门功课成绩最好的前两名学生姓名
select a.s_id,a.s_name,b.num_row
from student a
-- 将select查询作为一张子表,与student表联表查询
join (select
s_id,
c_id,
row_number() over(partition by c_id order by s_score desc) num_row
from score) b on a.s_id=b.s_id
where b.num_row<=2;
24、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row
from student a
join (select
s_id,
c_id,
s_score,
row_number() over(partition by c_id order by s_score desc) num_row from score) b
on a.s_id=b.s_id
where b.num_row between 2 and 3;
25、查询各科成绩前三名的记录(不考虑成绩并列情况)
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row
from student a
join (select
s_id,
c_id,
s_score,row_number() over(partition by c_id order by s_score desc) num_row
from score) b
on a.s_id=b.s_id
where b.num_row<=3;
26、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
select a.c_id,a.c_name,
(select count(*) from score where score.c_id=a.c_id and s_score<60) '小于60',
(select count(*) from score where score.c_id=a.c_id and s_score between 60 and 70) '60-70',
(select count(*) from score where score.c_id=a.c_id and s_score between 70 and 85) '70-85',
(select count(*) from score where score.c_id=a.c_id and s_score between 85 and 100) '85-100'
from course a
join score b on a.c_id=b.c_id
group by c_id;
27、查询每门课程被选修的学生数
mysql> select c_id,count(c_id) count from score group by c_id;
28、查询出只有两门课程的全部学生的学号和姓名
select a.s_id,a.s_name,count(b.s_id) count
from student a
join score b on a.s_id=b.s_id
group by s_id
having count=2;
29、查询男生、女生人数
mysql> select count(s_sex) count_sex from student group by s_sex;
30、查询名字中含有"风"字的学生信息
mysql> select * from student where s_name like '%周%';
31、查询1990年出生的学生名单
mysql> select s_id,s_name from student where year(s_birth)=1990;
32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.s_id,a.s_name,avg(b.s_score) avg_score
from student a
join score b on a.s_id=b.s_id
group by b.s_id
having avg_score>=85;
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
select c_id, avg(s_score) avg_score from score group by c_id order by avg_score,c_id desc;
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select a.c_id,b.c_name,c.s_id,c.s_name,a.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 a.s_score<60 and b.c_name='数学';
35、查询所有学生的课程及分数情况
select b.s_id,b.s_name,c.c_id,c.c_name,a.s_score
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id ;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select c.s_name,b.c_name,a.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 a.s_score>70;
37、查询不及格的课程并按课程号从大到小排列
mysql> select distinct c_id from score where s_score<60 order by c_id desc;
38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
select a.s_id,a.s_name
from student a
join score b on a.s_id=b.s_id
where b.s_score>80 and b.c_id=03;
39、求每门课程的学生人数
mysql> select c_id, count(*) count from score group by c_id;
40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩
select a.s_score,b.s_id,b.s_name
from score a
join student b on a.s_id=b.s_id
join course c on a.c_id=c.c_id
join teacher d on c.t_id=d.t_id
where d.t_name='张三'
order by a.s_score desc
limit 1;
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)
方法1:利用group by将s_id,c_id,s_score都重复的去除掉
select a.s_id,a.c_id,a.s_score
from score a
join score b on a.s_score=b.s_score
where a.c_id!=b.c_id and a.s_id=b.s_id
group by a.s_id,a.c_id,a.s_score;
方法2: 利用distinct将s_id,c_id,s_score都重复的去除掉
select distinct a.s_id,a.c_id,a.s_score
from score a
join score b on a.s_score=b.s_score
where a.c_id!=b.c_id and a.s_id=b.s_id ;
42、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
mysql> select c_id, count(*) count from score group by c_id order by c_id asc;
43、检索至少选修两门课程的学生学号
select s_id,count(*) count from score group by s_id having count>=2;
44、查询选修了全部课程的学生信息
方法1 :
select a.s_id,a.s_name,a.s_birth,a.s_sex
from student a
join score b on a.s_id=b.s_id
group by a.s_id,a.s_name,a.s_birth,a.s_sex
having count(b.s_id) =(select count(*) from course);
方法2 :
select * from student a
-- 查询score表中s_id=student表中s_id的行数,即统计每个学生的选修课程数量
where (select count(*) from score where s_id = a.s_id)=(select count(*) from course)
45、查询各学生的年龄
-- 时间差函数:timestampdiff
select TIMESTAMPDIFF(SECOND,"2020-02-27",NOW()) -- 31680773
select TIMESTAMPDIFF(MINUTE,"2020-02-27",NOW()) -- 528010
select TIMESTAMPDIFF(HOUR,"2020-02-27",NOW()) -- 8800
select TIMESTAMPDIFF(DAY,"2020-02-27",NOW()) -- 366
select TIMESTAMPDIFF(WEEK,"2020-02-27",NOW()) -- 52
select TIMESTAMPDIFF(MONTH,"2020-02-27",NOW()) -- 12
select TIMESTAMPDIFF(QUARTER,"2020-02-27",NOW()) -- 4
select TIMESTAMPDIFF(YEAR,"2020-02-27",NOW()) -- 1
48、查询两门以上不及格课程的同学的学号及其平均成绩
select s_id,avg(s_score) avg_score
from score
where s_score<60
group by s_id
having count(*)>=2;
49、查询本月过生日的学生
mysql> select s_id from student where month(s_birth) = month(now());
50、查询下一个月过生日的学生
mysql> select s_id from student where month(s_birth) = month(now())+1;
题目参考:https://blog.csdn.net/qiqi123i/article/details/108455130
题目参考:https://zhuanlan.zhihu.com/p/38354000
版权归原作者 小小茶花女 所有, 如有侵权,请联系我们删除。