MySQL练习
50道经典SQL练习题
数据表介绍
-
1.学⽣表 Student(SId,Sname,Sage,Ssex)
-
SId 学⽣编号
-
Sname 学⽣姓名
-
Sage 出⽣年⽉
-
Ssex 学⽣性别
-
-
2.课程表 Course(CId,Cname,TId)
-
CId 课程编号
-
Cname 课程名称
-
TId 教师编号
-
-
3.教师表 Teacher(TId,Tname)
-
TId 教师编号
-
Tname 教师姓名
-
-
4.成绩表 SC(SId,CId,score)
-
SId 学⽣编号
-
CId 课程编号
-
score 分数
-
建表语句
-
学⽣表 Student
create table Student( SId varchar(10), Sname varchar(10), Sage datetime, Ssex varchar(10) );
-
课程表 Course
create table Course( CId varchar(10), Cname nvarchar(10), TId varchar(10) );
-
教师表 Teacher
create table Teacher( TId varchar(10), Tname varchar(10) );
-
成绩表 SC
create table SC( SId varchar(10), CId varchar(10), score decimal(18,1) );
插入数据
-
学⽣表 Student
-- 学生表 Student insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); insert into Student values('02' , '钱电' , '1990-12-21' , '男'); insert into Student values('03' , '孙⻛' , '1990-12-20' , '男'); insert into Student values('04' , '李云' , '1990-12-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '⼥'); insert into Student values('06' , '吴兰' , '1992-01-01' , '⼥'); insert into Student values('07' , '郑⽵' , '1989-01-01' , '⼥'); insert into Student values('09' , '张三' , '2017-12-20' , '⼥'); insert into Student values('10' , '李四' , '2017-12-25' , '⼥'); insert into Student values('11' , '李四' , '2012-06-06' , '⼥'); insert into Student values('12' , '赵六' , '2013-06-13' , '⼥'); insert into Student values('13' , '孙七' , '2014-06-01' , '⼥');
-
课程表 Course
-- 科⽬表 Course insert into Course values('01' , '语⽂' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03');
-
教师表 Teacher
-- 教师表 Teacher insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五');
-
成绩表 SC
-- 成绩表 SC 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 "课程成绩⾼的学⽣的信息及课程分数
SELECT
tt1.SId
,t3.Sname
,tt1.语文
,tt1.数学
FROM(
SELECT
t1.SId
,t1.score '语文'
,t2.score '数学'
FROM (
( SELECT SId, score FROM SC WHERE CId = '01' ) t1
LEFT JOIN
( SELECT SId, score FROM SC WHERE CId = '02' ) t2
ON t1.SId = t2.SId
) WHERE t1.score>t2.score )tt1
LEFT JOIN Student t3
on t3.SId=tt1.SId
2.查询同时存在" 01 “课程和” 02 "课程的情况
select t1.SId
,t1.CId t1_CId
,t2.CId t2_CId
from(
select SId
,CId
from SC
where CId='01'
) t1
join
(
select SId
,CId
from SC
where CId='02'
)t2
on t2.SId=t1.SId
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
select t1.SId
,t1.CId t1_CId
,t2.CId t2_CId
from(
select SId
,CId
from SC
where CId='01'
) t1
left join
(
select SId
,CId
from SC
where CId='02'
)t2
on t2.SId=t1.SId
4.查询可能不存在" 01 “课程但存在” 02 "课程的情况
select t2.SId
,t1.CId t1_CId
,t2.CId t2_CId
from(
select SId
,CId
from SC
where CId='01'
) t1
right join
(
select SId
,CId
from SC
where CId='02'
)t2
on t2.SId=t1.SId
5.查询平均成绩⼤于等于 60 分的同学的学⽣编号和学⽣姓名和平均成绩
select t1.SId
,t2.Sname
,round(t1.avg_score,2)
from(
select SId
,avg(score) avg_score
from SC
group by SId
having avg_score>=60
) t1 join Student t2
on t1.SId=t2.SId;
6.查询在 SC 表存在成绩的学⽣信息
select t1.SId
,t2.Sname
,t2.Sage
,t2.Ssex
from (
select distinct SId
from SC
where score is not null
) t1 join Student t2
on t1.SId=t2.SId
7.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select t2.SId
,t2.Sname
,t1.count_CId
,t1.sum_score
from(
select SId
,count(CId) count_CId
,sum(score) sum_score
from SC
group by SId
) t1 right join Student t2
on t1.SId=t2.SId=
8.查询「李」姓⽼师的数量
select count(Tname)
from Teacher
where Tname like '李%';
9.查询学过「张三」⽼师授课的同学的信息
#-------------------方式一-------------------------
select ttt2.SId
,ttt2.Sname
from(
select tt2.SId SId
from(
select t2.CId t2_CId
,t2.Cname t2_Cname
from(
select TId
from Teacher
where Tname='张三'
) t1 left join Course t2
on t1.TId=t2.TId
) tt1 left join SC tt2
on tt1.t2_CId=tt2.CId
)ttt1 left join Student ttt2
on ttt1.SId=ttt2.SId
#-------------------方式二-------------------------
select t1.SId
,t2.Sname
from(
select SId
from SC
where CId=(
select CId
from Course
where TId=(
select TId
from Teacher
where Tname='张三'
)
)
)t1 left join Student t2
on t1.SId=t2.SId
10.查询没有学全所有课程的同学的信息
select t2.SId
,t2.Sname
,if(t1.num2 is null,0,t1.num2) num3
from(
select SId
,count(CId) num2
from SC
group by SId
)t1 right join Student t2
on t1.SId=t2.SId
having num3<(select count(CId) from Course)
11.查询⾄少有⼀⻔课与学号为" 01 "的同学所学相同的同学的信息
select t2.SId
,t2.Sname
,t1.CId
from SC
t1 join Student t2
on t1.SId=t2.SId
having t2.SId!='01' and t1.CId in (
select CId from SC where SId='01')
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
select tt1.SId
,tt1.Sname
,count(tt1.CId) num
from(
select t2.SId SId
,t2.Sname Sname
,t1.CId CId
from SC t1
join Student t2
on t1.SId=t2.SId
having t2.SId!='01' and
t1.CId in (select CId
from SC
where SId='01')
)tt1 group by tt1.SId,tt1.Sname
having num=(select count(SId) from SC where SId='01')
13.查询没学过"张三"⽼师讲授的任⼀⻔课程的学⽣姓名
select SId
,Sname
from Student
where SId not in (
select SId
from SC
where CId in (
select CId
from Course where TId=(
select TId
from Teacher
where Tname='张三'
)
)
);
14.查询两⻔及其以上不及格课程的同学的学号,姓名及其平均成绩
select t1.SId
,t2.Sname
,t1.avg_score
from(
select SId
,count(score) count_score
,round(avg(score),2) avg_score
from SC
where score < 60
group by SId
having count_score>=2
) t1 left join Student t2
on t1.SId=t2.SId
15.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
select SId
,Sname
from Student
where SId in(
select SId
from SC
where CId='01' and score < 60
order by score desc
)
16.按平均成绩从⾼到低显示所有学⽣的所有课程的成绩以及平均成绩
select t1.SId
,t2.chinese
,t3.math
,t4.english
,t1.avg_score
from(
select SId
,round(avg(score),2) avg_score
from SC
group by SId
) t1 left join
(select SId,score chinese from SC where CId='01') t2
on t1.SId=t2.SId
left join
(select SId,score math from SC where CId='02') t3
on t1.SId=t3.SId
left join
(select SId,score english from SC where CId='03') t4
on t1.SId=t4.SId
order by t1.avg_score desc
17.查询各科成绩最⾼分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
select t1.CId
,t2.Cname
,t1.count_CId
,t1.max_score
,t1.min_score
,t1.avg_score
,t1.及格率 及格率
,t1.中等率 中等率
,t1.优良率 优良率
,t1.优秀率 优秀率
from(
select CId
,count(CId) count_CId
,max(score) max_score
,min(score) min_score
,avg(score) avg_score
,round(sum(if(score>=60,1,0))*100 /count(score),2) '及格率'
,round(sum(if(score>=70 and score<=80,1,0))*100 /count(score),2) '中等率'
,round(sum(if(score>=80 and score<=90,1,0))*100 /count(score),2) '优良率'
,round(sum(if(score>=90,1,0))*100 /count(score),2) '优秀率'
from SC
group by CId
) t1 join Course t2
on t1.CId=t2.CId
order by t1.count_CId desc,t1.CId
18.按各科平均成绩进⾏排序,并显示排名, Score 重复时保留名次空缺
set @i :=0;
set @j :=0;
set @k :=0;
select t1.CId
,t1.score
,@j :=t1.score
,if(@k=@j,@i,@i :=@i+1) rank
,@k :=@j
from(
select CId
,score
from SC
order by score desc
) t1
19.按各科平均成绩进⾏排序,并显示排名, Score 重复时不保留名次空缺
set @i :=0;
select t1.CId
,t1.avg_score
,@i :=@i+1
from(
select CId
,avg(score) avg_score
from SC
group by CId
order by avg_score desc
) t1
20.查询学⽣的总成绩,并进⾏排名,总分重复时保留名次空缺
1 2 2 4 5 6
set @i :=0;
set @j :=0;
set @k :=0;
set @n :=0;
select t1.SId
,t1.sum_score
,@n :=@n+1
,@j :=t1.sum_score
,if(@k=@j,@i,@i :=@n) rank
,@k :=@j
from(
select SId
,sum(score) sum_score
from SC
group by SId
order by sum_score desc
) t1
21.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺
1 2 2 2 3 4 5
set @i :=0;
set @j :=0;
set @k :=0;
select t1.SId
,t1.sum_score
,@j :=t1.sum_score
,if(@k=@j,@i,@i :=@i+1) rank
,@k :=@j
from(
select SId
,sum(score) sum_score
from SC
group by SId
order by sum_score desc
) t1
22.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分⽐
select t2.CId
,t2.Cname
,t1.sum1 '[100-85)'
,t1.sum2 '[85-70)'
,t1.sum3 '[70-60)'
,t1.sum4 '[60-0)'
from(
select CId
,concat(round(sum(if(score>85 and score<=100,1,0))*100/count(score),2),'%') sum1
,concat(round(sum(if(score>70 and score<=85,1,0))*100/count(score),2) ,'%')sum2
,concat(round(sum(if(score>60 and score<=70,1,0))*100/count(score),2) ,'%')sum3
,concat(round(sum(if(score>0 and score<=60,1,0))*100/count(score),2) ,'%')sum4
from SC
group by CId
) t1 join Course t2
on t1.CId=t2.CId
23.查询各科成绩前三名的记录
SET @i := 0;
SET @p := 0;
SET @q := 0;
select t1.SId
,t2.Sname
,t1.CId
,t1.score
,t1.rn
from (
select SId
,CId
,@p :=CId
,if(@p=@q,@i :=@i+1,@i :=1) rn
,@q :=@p
,score
from SC
order by CId, score desc
) t1 join Student t2
on t1.rn<=3 and t1.SId=t2.SId
order by t1.CId,t1.rn
24.查询每⻔课程被选修的学⽣数
select t1.CId
,t2.Cname
,t1.amount
from(
select CId
,count(SId) amount
from SC
group by CId
) t1 join Course t2
on t1.CId=t2.CId
25.查询出只选修两⻔课程的学⽣学号和姓名
select t1.SId
,t2.Sname
from(
select SId
,count(CId) amount
from SC
group by SId
having amount=2
) t1 join Student t2
on t1.SId=t2.SId
26.查询男⽣、⼥⽣⼈数
select Ssex
,count(Ssex)
from Student
group by Ssex
27.查询名字中含有「⻛」字的学⽣信息
select *
from Student
where Sname like '%风%'
28.查询同名同性学⽣名单,并统计同名⼈数
select Sname
,Ssex
,count(Sname) amount
from Student
group by Sname,Ssex
having amount>1
29.查询 1990 年出⽣的学⽣名单
select Sname
,Sage
from Student
where Sage>='1990-01-01' and Sage<='1990-12-31';
30.查询每⻔课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select t1.CId
,t2.Cname
,t1.avg_score
from(
select CId
,avg(score) avg_score
from SC
group by CId
) t1 join Course t2
on t1.CId=t2.CId
order by t1.avg_score desc,t1.CId asc
31.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
select t1.SId
,t2.Sname
,t1.avg_score
from(
select SId
,avg(score) avg_score
from SC
group by SId
having avg_score>=85
) t1 join Student t2
on t1.SId=t2.SId
32.查询课程名称为「数学」,且分数低于 60 的学⽣姓名和分数
select t1.SId
,t2.Sname
,t1.score
from(
select SId
,score
from SC
where CId=(
select CId
from Course
where Cname='数学'
) and score<60
) t1 join Student t2
on t1.SId=t2.SId
33.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
#----------------------方式一——————————————————————————————————
select t3.SId
,t3.Sname
,t2.Cname
,t1.score
from(
select SId
,CId
,score
from SC
) t1 join Course t2
on t1.CId=t2.CId
right join Student t3
on t1.SId=t3.SId
#----------------------方式二——————————————————————————————————
select t2.SId
,t1.chinese
,t1.math
,t1.english
from(
select SId
,sum(case CId when '01' then score else 0 end) as chinese
,sum(case CId when '02' then score else 0 end) as math
,sum(case CId when '03' then score else 0 end) as english
from SC
group by SId
) t1 right join Student t2
on t1.SId=t2.SId
34.查询任何⼀⻔课程成绩在 70 分以上的姓名、课程名称和分数
select t1.SId
,t2.Sname
,t3.Cname
,t1.score
from(
select SId
,CId
,score
from SC
where score>70
) t1 join Student t2
on t1.SId=t2.SId
join Course t3
on t1.CId=t3.CId
35.查询不及格的课程
select t2.Cname
from(
select distinct CId
from SC
where score<60
) t1 join Course t2
on t1.CId=t2.CId
#-------------------------------------------
select t3.Sname
,t2.Cname
from(
select CId
,SId
from SC
where score<60
) t1 join Course t2
on t1.CId=t2.CId
join Student t3
on t1.SId=t3.SId
36.查询课程编号为 01 且课程成绩在 80 分以上的学⽣的学号和姓名
select SId
,Sname
from Student
where SId in (
select SId
from SC
where CId='01' and score >=80
)
37.求每⻔课程的学⽣⼈数
select t1.CId
,t2.Cname
,t1.amount
from (
select CId
,count(SId) amount
from SC
group by CId
) t1 join Course t2
on t1.CId=t2.CId
38.成绩不重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select t1.SId
,t2.Sname
,t1.CId
,t1.score
from (
select SId
,CId
,score
from SC
where score = (
select max(score) max_score
from SC
where CId in(
select CId
from Course
where TId=(
select TId
from Teacher
where Tname='张三'
)
)
)
) t1 join Student t2
on t1.SId=t2.SId
#-------------------------------------------------------
select t1.SId
,t2.Sname
,t1.CId
,t1.score
from (
select SId
,CId
,score
from SC
where CId=(
select CId
from Course
where TId=(
select TId
from Teacher
where Tname='张三'
)
)
)t1 join Student t2
on t1.SId=t2.SId
order by t1.score desc
limit 1;
#---------------------假设张三老师带两门课-----------------------
select tt1.SId
,tt2.Sname
,tt1.CId
,tt1.max_score
from(
select t2.SId SId
,t1.CId CId
,t1.max_score max_score
from(
select CId
,max(score) max_score
from SC
where CId in(
select CId
from Course
where TId=(
select TId
from Teacher
where Tname='张三'
)
)
group by CId
) t1 join SC t2
on t1.CId=t2.CId
and t1.max_score=t2.score
)tt1 join Student tt2
on tt1.SId=tt2.SId
39.成绩有重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select tt1.SId
,tt2.Sname
,tt1.CId
,tt1.max_score
from(
select t2.SId SId
,t1.CId CId
,t1.max_score max_score
from(
select CId
,max(score) max_score
from SC
where CId in(
select CId
from Course
where TId=(
select TId
from Teacher
where Tname='张三'
)
)
group by CId
) t1 join SC t2
on t1.CId=t2.CId
and t1.max_score=t2.score
)tt1 join Student tt2
on tt1.SId=tt2.SId
40.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩
select distinct
t1.SId
,t3.Sname
,t1.CId
,t1.score
from SC t1
join SC t2
on t1.score=t2.score and t1.CId!=t2.CId
join Student t3
on t1.SId=t3.SId
41.查询每⻔课程成绩最好的前两名
(select SId
,CId
,score
from SC
where CId='01'
order by score desc
limit 2)
union all
(select SId
,CId
,score
from SC
where CId='02'
order by score desc
limit 2)
union all
(select SId
,CId
,score
from SC
where CId='03'
order by score desc
limit 2)
#------------------------加上学生姓名------------------------
(select t1.SId
,t2.Sname
,t1.CId
,t1.score
from(
select SId
,CId
,score
from SC
where CId='01'
order by score desc
limit 2
) t1 join Student t2
on t1.SId=t2.SId)
union
(select t3.SId
,t4.Sname
,t3.CId
,t3.score
from(
select SId
,CId
,score
from SC
where CId='02'
order by score desc
limit 2
) t3 join Student t4
on t3.SId=t4.SId)
union
(select t5.SId
,t6.Sname
,t5.CId
,t5.score
from(
select SId
,CId
,score
from SC
where CId='03'
order by score desc
limit 2
) t5 left join Student t6
on t5.SId=t6.SId)
42.统计每⻔课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)。
select t1.CId
,t2.Cname
,t1.amount
from(
select CId
,count(SId) amount
from SC
group by CId
having amount>5
) t1 join Course t2
on t1.CId=t2.CId
43.检索⾄少选修两⻔课程的学⽣学号
select SId
,count(CId) amount
from SC
group by SId
having amount>=2
#-------------------加上学生姓名------------------
select t1.SId
,t2.Sname
,t1.amount
from(
select SId
,count(CId) amount
from SC
group by SId
having amount>=2
) t1 join Student t2
on t1.SId=t2.SId
44.查询选修了全部课程的学⽣信息
select t1.SId
,t2.Sname
,t1.amount
from(
select SId
,count(CId) amount
from SC
group by SId
having amount =(
select count(CId)
from Course
)
) t1 join Student t2
on t1.SId=t2.SId
45.查询各学⽣的年龄,只按年份来算
select SId
,Sname
,year(now()) - date_format(Sage,'%Y') age
from Student
46.按照出⽣⽇期来算,当前⽉⽇ < 出⽣年⽉的⽉⽇则,年龄减一
select SId
,Sname
,if(date_format(now(),'%m-%d') < date_format(Sage,'%m-%d'),
year(now()) - date_format(Sage,'%Y')-1,
year(now()) - date_format(Sage,'%Y')) age
from Student
47.查询本周过⽣⽇的学⽣
select *
from Student
where week(now())=week(Sage)
48.查询下周过⽣⽇的学⽣
select *
from Student
where week(now())+1=week(Sage)
#---------------------------------------------------------
SELECT SId
,SName
,Sage
,week(Sage)
from Student
where week(Sage) = week(date_add(now(),INTERVAL 1 Week))
49.查询本⽉过⽣⽇的学⽣
select *
from Student
where month(now())=month(Sage)
50.查询下⽉过⽣⽇的学⽣
select *
from Student
where month(now())+1=month(Sage)
#----------------------------------------------------------
SELECT SId
,SName
,Sage
,month(Sage)
from Student
where month(Sage) = month(date_add(now(),INTERVAL 1 Month))