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);
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
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
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
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
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;
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
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=
select count(Tname) from Teacher where Tname like '李%';
#-------------------方式一------------------------- 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
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)
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')
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')
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='张三' ) ) );
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
select SId ,Sname from Student where SId in( select SId from SC where CId='01' and score < 60 order by score desc )
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
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
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
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
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
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
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
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
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
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
select Ssex ,count(Ssex) from Student group by Ssex
select * from Student where Sname like '%风%'
select Sname ,Ssex ,count(Sname) amount from Student group by Sname,Ssex having amount>1
select Sname ,Sage from Student where Sage>='1990-01-01' and Sage<='1990-12-31';
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
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
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
#----------------------方式一—————————————————————————————————— 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
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
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
select SId ,Sname from Student where SId in ( select SId from SC where CId='01' and score >=80 )
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
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
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
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
(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)
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
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
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
select SId ,Sname ,year(now()) - date_format(Sage,'%Y') age from Student
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
select * from Student where week(now())=week(Sage)
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))
select * from Student where month(now())=month(Sage)
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))