MySQL经典45道练习题&答案

数据表

--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)S

SId 学生编号,CId 课程编号,score 分数

1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

select *

from SC a

left join  Student d

      on a.Sid=d.Sid

inner join SC b

      on a.SID=b.Sid and a.Cid='01' and b.Cid='02'

where a.score>b.score;

1.1 查询同时存在" 01 "课程和" 02 "课程的情况

select *

from SC a

inner join SC b

      on a.Sid=b.Sid and a.Cid='01' and b.Cid='02'

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) 

select *

from (select * from SC where Cid='01') a

left join SC b

on a.Sid=b.Sid and b.Cid='02';

写法2:

select *

from SC a

left join SC b

on a.Sid=b.Sid and b.Cid='02'

where a.Cid='01';

1.3 查询不存在"01 "课程但存在" 02 "课程的情况

select *

from SC a

inner join SC b

on a.Sid=b.Sid and b.Cid='02'

where a.Sid not in(select Sid from SC where Cid='01');

写法2:

select *

from SC

where Sid not in(select Sid from SC where Cid='01')

and Cid='02';

2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select a.Sid,a.Sname,b.avg_score

from Student a

inner join

(select Sid,avg(score) as avg_score

  from SC

  group by Sid having avg(score)>=60) b

on a.Sid=b.Sid;

3.查询在 SC 表存在成绩的学生信息

select a.*

from Student a

inner join

(select distinct Sid from SC) b

on a.Sid=b.Sid;

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select a.Sid,a.Sname,b.count_c,b.sum_score

from Student a

left join

(select Sid,count(Cid) as count_c,sum(score) as sum_score

  from SC

  group by Sid ) b

on a.Sid=b.Sid;

5.查询「李」姓老师的数量

select count(*)

from Teacher

where Tname like'李%';

6.查询学过「张三」老师授课的同学的信息

select *

from Student

where Sid in

  (select Sid from SC where Cid in

    (select Cid from Course where Tid in

      (select Tid from Teacher where Tname='张三')));

写法2:

select Student.*

from Student,SC,Course,Teacher

where Teacher.Tname='张三'

and Teacher.Tid=Course.Tid

and Course.Cid=SC.Cid

and SC.Sid=Student.Sid;

7.查询没有学全所有课程的同学的信息

select a.*,cons

from Student a

inner join

(select Sid,count(Cid) as cons

  from SC

  group by Sid having cons<(select count(Cid) from Course))b

on a.Sid=b.Sid;

写法2:

select a.*,cons

from Student a,

(select Sid,count(Cid) as cons

  from SC

  group by Sid having cons<(select count(Cid) from Course))b

where a.Sid=b.Sid;

算上未学过课程的同学的情况:

select *

from Student

where Sid not in

(select Sid

  from SC

  group by Sid having count(Cid)=(select count(Cid) from Course));

8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

select b.*

from (select distinct Sid

  from SC

  where Cid in(select Cid from SC where Sid='01')) a,

Student b

where a.Sid=b.Sid;

9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

select b.*

from SC a

inner join Student b

on a.Sid=b.Sid

where a.Sid not in(select Sid from SC where Cid not in(select Cid from SC where Sid='01'))

and a.Sid!='01'

group by a.Sid

having count(*)=(select count(*) from SC where Sid='01')

10.查询没学过"张三"老师讲授的任一门课程的学生姓名

select Sname

from Student

where Sid not in

  (select Sid from SC

  where Cid in

    (select Cid from Course

    where Tid in

      (select Tid from Teacher

      where Tname='张三')));

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

select a.*,avg(Score)

from Student a

inner join SC b

on a.Sid=b.Sid

where b.score<60

group by a.Sid

having count(*)>=2;

12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select a.*,Score

from Student a

right join

(select Sid, Score from SC where Cid='01' and Score<60) b

on a.Sid=b.Sid

order by Score desc;

13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select a.*,avg_score

from SC a

left join (select Sid,avg(Score) as avg_score from SC group by Sid) b

on a.Sid=b.Sid

order by avg_score desc;

14.查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select Cid,max(Score) as 最高分,min(Score) as 最低分,avg(Score) as 平均分,

sum(case when sc.score>=60 then 1 else 0 end)/count(*) as 及格率,

sum(case when sc.score>=70 and score<80 then 1 else 0 end)/count(*) as 中等率,

sum(case when sc.score>=80 and score<90 then 1 else 0 end)/count(*) as 优良率,

sum(case when sc.score>=90 then 1 else 0 end)/count(*) as 优秀率,

count(*) as 选修人数

from SC group by Cid

order by 选修人数 desc,Cid asc;

15.按各科成绩进行排序,并显示排名, Score重复时继续排名

select Sid,Cid,Score,@rank:=@rank+1 as rn

from SC,(select @rank:=0)t

order by Score desc;

15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

select Sid,Cid,Score,

case when @sco=Score then@rank

    else @rank:=@rank+1 end as rn,

@sco:=Score

from SC,(select @rank:=0,@sco:=null)t

order by Score desc;

写法2:(可以去掉上面写法中多出来的@sco:=Score这一列)

select Sid,Cid,Score,

case when @sco=Score then@rank

    when @sco:=Score then @rank:=@rank+1 end as rn

from SC,(select @rank:=0,@sco:=null)t

order by Score desc;

16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 

select a.*,@rank:=if(@sco=scos,'',@rank+1) as rn,

@sco:=scos

from(select sid,sum(Score) as scos

    from SC

    group by Sid

    order by scos desc)a,

    (select @sco:=null,@rank:=0)b;

16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

select a.*,@rank:=if(@sco=scos,@rank,@rank+1) as rn,

@sco:=scos

from(select sid,sum(Score) as scos

    from SC

    group by Sid

    order by scos desc)a,

    (select @sco:=null,@rank:=0)b;

17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

select SC.Cid,Course.Cname,

concat(sum(case when score>=85 and score<=100 then 1 else 0 end)/count(*)*100,'%') as '[85-100]',

concat(sum(case when score>=70 and score<85 then 1 else 0 end)/count(*)*100,'%') as '[70-85)',         

concat(sum(case when score>=60 and score<70 then 1 else 0 end)/count(*)*100,'%') as '[60-70)',

concat(sum(case when score>=0 and score<60 then 1 else 0 end)/count(*)*100,'%') as '[0-60)'

from SC

inner join Course

on SC.Cid=Course.Cid

group by SC.Cid;

18.查询各科成绩前三名的记录

不考虑Score重复的情况:

select a.*

from SC a

where (select count(*) from SC b

      where b.Cid=a.Cid and b.Score>a.Score)<3

order by Cid asc,score desc;

Score 重复时合并名次的情况:

select Sid,Cid,Score,rn from

(select SC.*,@rank:=if(@ci=Cid,if(@sco=Score,@rank,@rank+1),1) as rn,

@sco:=Score,@ci:=Cid

from SC,(select @rank:=0,@sco:=null,@ci:=null)b

order by Cid asc,Score desc)a

where rn<=3;

19.查询每门课程被选修的学生数

select Cid,count(*) as cons from SC

group by Cid

order by Cid asc

20.查询出只选修两门课程的学生学号和姓名

select a.Sid,Sname

from Student a,SC b

where a.Sid=b.Sid

group by Sid

having count(*)=2;

21.查询男生、女生人数

select Ssex,count(*) as cons

from Student

group by Ssex;

22.查询名字中含有「风」字的学生信息

select Student.* from Student where Sname like"%风%";

23.查询同名同性学生名单,并统计同名人数

select a.*,cons

from Student a

left join (select Sname,Ssex,count(*) as cons from Student group by Sname,Ssex)b

on a.Sname=b.Sname and a.Ssex=b.Ssex

where cons>1;

24.查询 1990 年出生的学生名单

select Student.* from Student where year(Sage)=1990;

25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select Cid,avg(Score)

from SC

group by Cid

order by Score desc,Cid asc;

26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

select a.Sid,Sname,avg_score

from Student a

right join (select Sid,avg(Score) as avg_score

  from SC

          group by Sid

          having avg_score>=85) b

on a.Sid=b.Sid;

27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

select Sname,Score

from Student a,SC b

where a.Sid=b.Sid and Score<60

and b.Cid =(select Cid from Course where Cname='数学');

28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

select * from Student a left join SC b on a.Sid=b.Sid;

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

select Sname,Cname,Score

from Student a,SC b,Course c

where a.Sid=b.Sid and b.Cid=c.Cid and Score>70;

30.查询不及格的课程

select distinct Cid from SC where Score<60;

31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

select Sid,Sname

from Student

where Sid in(select Sid from SC where Cid='01' and Score>=80);

32.求每门课程的学生人数

select Cid,count(*) from SC group by Cid;

33.假设成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select a.*,max(Score)

from Student a,SC b,Course c,Teacher d

where a.Sid=b.Sid and b.Cid=c.Cid and c.Tid=d.Tid and Tname='张三';

34.假设成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select * from

(select e.*,

case when @sco=Score then @rank

when @sco:=Score then @rank:=@rank+1 end as rn

from (select a.*,Score

  from Student a,SC b,Course c,Teacher d

      where a.Sid=b.Sid and b.Cid=c.Cid and c.Tid=d.Tid and Tname="张三"

      order by Score desc)e,(select @rank:=0,@sco:=null)f)g

where rn=1;

35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select * from

SC a

where a.Sid in(select Sid from SC b

              where a.Sid=b.Sid and a.Cid!=b.Cid and a.Score=b.Score);

36.查询每门功成绩最好的前两名

select * from

(select a.*,

@rank:=if(@ci=Cid,if(@sco=Score,@rank,@rank+1),1) as rn,

@sco:=Score,@ci:=Cid

from (select SC.* from SC order by Cid asc,Score desc)a,

(select @rank:=0,@sco:=null)b)c

where rn<=2;

37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。

select Cid,count(*) from SC group by Cid having count(*)>5;

38.检索至少选修两门课程的学生学号

select Sid,count(*) from SC group by Sid having count(*)>=2;

39.查询选修了全部课程的学生信息

select a.* from Student a

where Sid in(select Sid from SC

            group by Sid having count(*)=(select count(Cid) from Course));

40.查询各学生的年龄,只按年份来算

select *,year(now())-year(Sage) as age from Student;

41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select *,timestampdiff(year,Sage,now()) as age from Student;

42.查询本周过生日的学生

select *,week(Sage),week(now()) from Student where week(Sage)=week(now());

43.查询下周过生日的学生

select *,week(Sage),week(now()) from Student where week(Sage)=week(now())+1;

44.查询本月过生日的学生

select *,month(Sage),month(now()) from Student where month(Sage)=month(now());

45.查询下月过生日的学生

select *,month(Sage),month(now()) from Student where month(Sage)=month(now())+1;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,561评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,218评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,162评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,470评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,550评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,806评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,951评论 3 407
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,712评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,166评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,510评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,643评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,306评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,930评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,745评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,983评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,351评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,509评论 2 348