MySQL经典50题
mysq经典50题拿来练手·····
文章目录
- MySQL经典50题
- 创建表及信息录入
- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
- 6、查询"李"姓老师的数量
- 7、查询学过"张三"老师授课的同学的信息
- 8、查询没学过"张三"老师授课的同学的信息
- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
- 11、查询没有学全所有课程的同学的信息
- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
- 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
- 19、按各科成绩进行排序,并显示排名
- 20、查询学生的总成绩并进行排名
- 21、查询不同老师所教不同课程平均分从高到低显示
- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
- 23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
- 24、查询学生平均成绩及其名次
- 25、查询各科成绩前三名的记录
- 26、查询每门课程被选修的学生数
- 27、查询出只有两门课程的全部学生的学号和姓名
- 28、查询男生、女生人数
- 29、查询名字中含有"风"字的学生信息
- 30、查询同名同姓学生名单,并统计同名人数
- 31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
- 35、查询所有学生的课程及分数情况
- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
- 37、查询不及格的课程
- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
- 39、求每门课程的学生人数
- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
- 42、查询每门功课成绩最好的前两名
- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
- 44、检索至少选修两门课程的学生学号
- 45、查询选修了全部课程的学生信息
- 46、查询各学生的年龄 只按年份来算
- 47、查询本周过生日的学生
- 48、查询下周过生日的学生
- 49、查询本月过生日的学生
- 50、查询下月过生日的学生
创建表及信息录入
# 学生表
create table student
(
sid varchar(10),
sname varchar(32),
saged datetime,
ssex varchar(10)
) comment '学生表';
# 学生表插入数据
insert into student(sid,sname,saged,ssex)values('01' , '赵雷' , '1990-01-01' , '男');
insert into student(sid,sname,saged,ssex)values('02' , '钱电' , '1990-12-21' , '男');
insert into student(sid,sname,saged,ssex)values('03' , '孙风' , '1990-05-20' , '男');
insert into student(sid,sname,saged,ssex)values('04' , '李云' , '1990-08-06' , '男');
insert into student(sid,sname,saged,ssex)values('05' , '周梅' , '1991-12-01' , '女');
insert into student(sid,sname,saged,ssex)values('06' , '吴兰' , '1992-03-01' , '女');
insert into student(sid,sname,saged,ssex)values('07' , '郑竹' , '1989-07-01' , '女');
insert into student(sid,sname,saged,ssex)values('08' , '王菊' , '1990-01-20' , '女');
-- -------------------------------------------------------------
# 课程表
create table course
(
cid varchar(10),
cname varchar(32),
tid varchar(10)
) comment '课程表';
# 插入课程数据
insert into course(cid,cname,tid)values('01' , '语文' , '02');
insert into course(cid,cname,tid)values('02' , '数学' , '01');
insert into course(cid,cname,tid)values('03' , '英语' , '03');
-- --------------------------------------------------------
# 教师表
create table teacher
(
tid varchar(10),
tname varchar(32)
)comment '教师表';
# 插入教师表信息
insert into teacher(tid,tname)values('01' , '张三');
insert into teacher(tid,tname)values('02' , '李四');
insert into teacher(tid,tname)values('03' , '王五');
-- --------------------------------------------------
# 成绩表
create table sc
(
sid varchar(10),
cid varchar(10),
score int(30)
)comment '成绩表';
# 插入成绩信息
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);
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
# 涉及表 student ,sc
select stu.*, s1.score, s2.score
from student stu
left join sc s1 on stu.sid = s1.sid and s1.cid = 01
left join sc s2 on stu.sid = s2.sid and s2.cid = 02
where s1.score > s2.score;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select stu.*,s1.score,s2.score
from student stu
left join sc s1 on stu.sid = s1.sid and s1.cid = 01
left join sc s2 on stu.sid = s2.sid and s2.cid = 02
where s1.score < s2.score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
# 涉及表:student ,sc
# 聚合函数 avg---分组
# having---分组之后过滤平均分大于等于60
select stu.sid, stu.sname, avg(s.score) avgscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having avgscore >= 60;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
情况一:参加考试的学生
# 涉及表:student ,sc
# 聚合函数 avg---分组
# having---分组之后过滤平均分小于60
select stu.sid, stu.sname, ifnull(avg(s.score),0 )avgscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having avgscore < 60;
情况二:
注意:这里有个同学没有参加选课和考试,所以显示成绩为null,如果判定没参加考试的为0分,那么算平均分之前加上ifnull函数,将null的值判定为0
# 涉及表:student ,sc
# 聚合函数 avg---分组
# having---分组之后过滤平均分小于60
select stu.sid, stu.sname, ifnull(avg(s.score),0) avgscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having avgscore < 60;
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
这里将没有选课和考试的学生原为null值,用ifnull判定为null值
# 涉及表:student,sc
# 聚合函数count,sum---分组
select stu.sid, stu.sname, count(s.cid), ifnull(sum(s.score), 0)
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname;
6、查询"李"姓老师的数量
#涉及表:teacher
#聚合函数:count
#模糊查询 like %
select count(*)
from teacher
where tname like '李%'
7、查询学过"张三"老师授课的同学的信息
#涉及表:student,sc,course,teacher
select stu.*
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = '张三';
8、查询没学过"张三"老师授课的同学的信息
#涉及表:student,sc,course,teacher
# 联系第7题,可以根据学号 not in,将上题范围排除
select stu.*
from student stu
where stu.sid not in (
select stu.sid
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = '张三'
);
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
# 涉及表 student ,sc
select stu.*
from student stu
left join sc s1 on stu.sid = s1.sid and s1.cid = 01
left join sc s2 on stu.sid = s2.sid and s2.cid = 02
where s1.sid = s2.sid;
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
# 涉及表 student ,sc
select *
from student stu
where stu.sid in (
select s1.sid
from sc s1
where s1.cid = 01
)
and stu.sid not in (
select s2.sid
from sc s2
where s2.cid = 02
);
11、查询没有学全所有课程的同学的信息
# 涉及表:student,sc,course
# 用聚合函数统计course的课程数
# 根据课程数来判断
select stu.sid, stu.sname, stu.saged, stu.ssex
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname, stu.saged, stu.ssex
having count(s.cid) < (select count(cid) from course)
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
# 涉及表 student,sc
# 先找出学号01同学的所学课程
# 用in在其中筛选,并且排除自己(排除01)
select distinct stu.*
from student stu
left join sc s on stu.sid = s.sid
where stu.sid != 01
and s.cid in (
select s2.cid
from student stu2
left join sc s2 on stu2.sid = s2.sid
where stu2.sid = 01
);
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
# 涉及表:student,sc
select s1.sid, stu.*, count(s1.cid) num
from sc s1
left join sc s2 on s1.cid = s2.cid
and s1.sid != 01 and s2.sid = 01
left join student stu on s1.sid = stu.sid
where stu.sid != 01
and s2.sid is not null
group by s1.sid, stu.sid, stu.sname, stu.saged, stu.ssex
having num = (select count(cid) from sc where sid = 01);
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
# 涉及表:student,sc,course,teacher
# 先找到张三老师的课程号--02
# 再找到学生选修02的学生信息
select stu.*
from student stu
where stu.sid in (
select stu.sid
from student stu
left join sc s on stu.sid = s.sid
where s.cid in (
select c.cid
from course c
left join teacher t on c.tid = t.tid
where t.tname = '张三')
);
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
# 涉及表:student,sc
# 条件:where <60
# 聚合函数 avg---分组
select stu.sid, stu.sname
from student stu
left join sc s on stu.sid = s.sid
where s.sid in (
select s1.sid
from sc s1
where s1.score < 60
group by s1.sid
having count(*) >= 2
)
group by stu.sid, stu.sname;
16、检索"01"课程分数小于60,按分数降序排列的学生信息
# 涉及表 student,sc
# 条件 where cid=01 and score<60
# 排序 order by desc
select stu.*
from sc s
left join student stu on s.sid = stu.sid
where s.cid = 01
and s.score < 60
order by s.score desc;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
# 涉及表 student,sc
# 聚合函数 sum,avg
# case when then else end
select stu.sid,
stu.sname,
ifnull(t.avgscore, 0) avgscore,
sum(case when s2.cid = 01 then s2.score else 0 end) '语文',
sum(case when s2.cid = 02 then s2.score else 0 end) '数学',
sum(case when s2.cid = 03 then s2.score else 0 end) '英语'
from (select s1.sid, avg(s1.score) avgscore
from sc s1
group by s1.sid
order by avgscore desc) t
left join sc s2 on t.sid = s2.sid
right join student stu on t.sid = stu.sid
group by stu.sid, stu.sname, t.avgscore
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
# 涉及表:sc
# 聚合函数 max,min,avg,sum,count
select c.cid,
c.cname,
max(s.score)'最高分',
min(s.score)'最低分',
avg(s.score)'平均分',
sum(case when score >= 60 then 1 else 0 end) / count(*) '及格率',
sum(case when score between 70 and 80 then 1 else 0 end) / count(*) '中等率',
sum(case when score between 80 and 90 then 1 else 0 end) / count(*) '优良率',
sum(case when score >=90 then 1 else 0 end) / count(*) '优秀率'
from sc s
left join course c on s.cid = c.cid
group by c.cid, c.cname
19、按各科成绩进行排序,并显示排名
#涉及表sc
#窗口函数 row_number()
select s.*, row_number() over (partition by s.cid order by s.score desc ) 排名
from sc s;
20、查询学生的总成绩并进行排名
# 涉及表 student ,sc
# 聚合函数 sum
# 排序 order by desc
select stu.sid, ifnull(sum(s.score), 0) sumscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid
order by sumscore desc
21、查询不同老师所教不同课程平均分从高到低显示
# 涉及表 teacher, course,sc
# 聚合函数 avg
# 排序 order by desc
select t.tid, t.tname, avg(s.score) avgscore
from teacher t
left join course c on t.tid = c.tid
left join sc s on c.cid = s.cid
group by t.tid, t.tname
order by avgscore desc;
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
#涉及表 sc
SELECT a.* , (SELECT COUNT(DISTINCT score) FROM SC WHERE CID = a.CID AND score > a.score) + 1 AS 排名
FROM SC AS a HAVING 排名 BETWEEN 2 AND 3 ORDER BY a.CID,排名;
23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
# 涉及表 sc,course
# 聚合函数 sum count round
# case when then else end
select c.cid '课程编号',
c.cname '课程名称',
round(sum(case when s.score between 0 and 60 then 1 else 0 end) / count(*) * 100) '0-60占比%',
round(sum(case when s.score between 60 and 70 then 1 else 0 end) / count(*) * 100) '70-60占比%',
round(sum(case when s.score between 70 and 85 then 1 else 0 end) / count(*) * 100) '85-70占比%',
round(sum(case when s.score between 85 and 100 then 1 else 0 end) / count(*) * 100) '100-85占比%'
from sc s
left join course c on s.cid = c.cid
group by c.cid, c.cname
24、查询学生平均成绩及其名次
# 涉及表 sc student
# 聚合函数 avg
select stu.sname,avg(s.score)
from sc s
left join student stu on s.sid = stu.sid
group by stu.sname,stu.sid
25、查询各科成绩前三名的记录
#涉及表 sc
select s1.sid, s1.score
from sc s1
where (
select count(*)
from sc s2
where s1.cid = s2.cid
and s1.score < s2.score
) < 3
order by s1.cid, s1.score desc;
26、查询每门课程被选修的学生数
# 涉及表 course sc
select c.cid, c.cname, count(*)
from course c
left join sc s on c.cid = s.cid
group by c.cid, c.cname;
27、查询出只有两门课程的全部学生的学号和姓名
# 涉及表 student sc
select stu.sid, stu.sname
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having count(s.cid) = 2
28、查询男生、女生人数
# 涉及表 student
# 聚合函数 count--分组
select stu.ssex, count(*)
from student stu
group by stu.ssex
29、查询名字中含有"风"字的学生信息
# 涉及表 student
# 模糊查询 like '%风%'
select stu.*
from student stu
where stu.sname like '%风%';
30、查询同名同姓学生名单,并统计同名人数
# 涉及表 student
# 内连接,不排除自身情况下,若出现同名同姓的至少大于1
select stu1.sid, stu2.sname
from student stu1
join student stu2 on stu1.sname = stu2.sname
group by stu1.sid, stu2.sname
having count(*) > 1
31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
# 涉及表 student
# 模糊查询 like '1990%'
select stu.*
from student stu
where stu.saged like '1990%'
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
# 涉及表 sc
# 聚合函数 avg---分组
select s.cid,avg(s.score) avgscore
from sc s
group by s.cid
order by avgscore desc ,s.cid asc ;
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
# 涉及表 student sc
# 聚合函数 avg---分组
# having 过滤
select stu.sid, stu.sname, avg(s.score) avgscore
from student stu
left join sc s on stu.sid = s.sid
group by stu.sid, stu.sname
having avgscore >= 85
35、查询所有学生的课程及分数情况
# 涉及表:student sc,course
select stu.sid, stu.sname, s.cid, c.cname, s.score
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
# 涉及表 student ,sc,course
select stu.sname, c.cname, s.score
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
where s.score >= 70;
37、查询不及格的课程
# 涉及表 sc course
select distinct s.sid, s.cid, c.cname
from sc s
left join course c on s.cid = c.cid
where s.score < 60;
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
# 涉及表 student sc
select stu.sid, stu.sname
from student stu
left join sc s on stu.sid = s.sid
where s.cid = 01
and s.score >= 80
39、求每门课程的学生人数
# 涉及表 sc course
# 聚合函数 count
select c.cname, count(s.cid)
from sc s
left join course c on s.cid = c.cid
group by c.cid, c.cname
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
# 涉及表 student sc course teacher
# 分页查询 1 ---显示第一个
select stu.*, s.score
from student stu
left join sc s on stu.sid = s.sid
left join course c on s.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = '张三'
order by s.score desc
limit 1;
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select s.*
from sc s,
(
select cid, score
from sc
group by cid, score
having count(*) > 1
) s2
where s.cid = s2.cid
and s.score = s2.score
order by s.cid, s.sid, s.score;
42、查询每门功课成绩最好的前两名
# 涉及表 sc
select s.cid,s.score
from sc s
where (
select count(*)
from sc s2
where s.cid=s2.cid and s.score<s2.score
)<2
order by s.cid,s.score desc;
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
# 涉及表 sc
# 聚合函数 count
select count(s.cid) countpeople
from sc s
group by s.cid
having countpeople>5
order by countpeople desc ,s.cid asc;
44、检索至少选修两门课程的学生学号
# 涉及表 student
select stu.sid
from student stu
join sc s on stu.sid = s.sid
group by stu.sid
having count(s.cid) >= 2;
45、查询选修了全部课程的学生信息
# 涉及表 student sc
select stu.*
from student stu
join (
select s.sid
from sc s
group by s.sid
having count(s.sid) = 3
) t on stu.sid = t.sid;
46、查询各学生的年龄 只按年份来算
# 涉及表 student
# 函数 year(),now()
select stu.sname, (year(now()) - year(stu.saged)) age
from student stu;
47、查询本周过生日的学生
# 涉及表 student
# 函数 week(),now()
select *
from student stu
where week(saged) = week(now());
48、查询下周过生日的学生
# 涉及表 student
# 函数 week(),now()
select *
from student stu
where week(saged)=week(now())+1;
49、查询本月过生日的学生
# 涉及表 student
# 函数 month(),now()
select *
from student stu
where month(now())=month(stu.saged);
50、查询下月过生日的学生
# 涉及表 student
# 函数 month(),date_sub(),curdate()
select
stu.*
from student stu
where month(date_sub(saged,interval 1 month)) = month(curdate());