mysql四十五道题之三十八道

纪念一下,是真的不看答案自己做出来的,另外还有七道目前不能不看答案做出来,各个击破,加油加油!!!!!

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

select a.snum,b.sname ,a.score,c.score from (select * from  sc where cnum='01') a left join student b on a.snum=b.snum inner join (select * from  sc where cnum='02') c on a.snum=c.snum where a.score>c.score;

select * from sc a left join sc b on a.snum=b.snum and a.cnum='01' and b.cnum='02' where a.score>b.score group by a.snum;

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

select * from (select * from  sc where cnum='01') a left join student b on a.snum=b.snum inner join (select * from  sc where cnum='02') c on a.snum=c.snum;

select * from sc a inner join sc b on a.snum=b.snum and a.cnum='01' and b.cnum='02';

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

select * from (select * from  sc where cnum='01') a left join student b on a.snum=b.snum left join (select * from  sc where cnum='02') c on a.snum=c.snum;

select * from sc a left join sc b on a.snum=b.snum and b.cnum='02' where a.cnum='01' ;

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

select * from (select * from  sc where cnum='02') a left join student b on a.snum=b.snum left join (select * from  sc where cnum='01') c on a.snum=c.snum where c.cnum is null;

select * from sc a left join sc b on a.snum=b.snum and b.cnum='01' where a.cnum='02' and b.cnum is null;

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

select a.snum,b.sname,avg(score) as avg from sc a left join student b on a.snum=b.snum group by a.snum;

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

select b.snum,a.sname from sc b left join student a on a.snum=b.snum group by b.snum;

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

select a.snum,b.sname,count(*) as cons,sum(score) as con from sc a left join student b on a.snum=b.snum group by a.snum;

select a.snum,a.sname,b.* from student a left join (select snum,count(*) as cons,sum(score) as con from sc group by snum)b on a.snum=b.snum;

4.1 查有成绩的学生信息

select * from student where snum in (select distinct snum from sc where score is not null);

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

select count(tname) as cons from teacher where tname like '李%';

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

select * from student where snum in (select snum from sc where cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')));

select a.* from sc a  inner join course b on a.cnum=b.cnum inner join teacher c on b.tnum=c.tnum

and c.tname='张三' group by a.snum;

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

思路分解

课程总数小于cnum总数

select a.snum,b.* from student a inner join (select snum,count(cnum) as cons from sc group by snum having cons <(select count(1) from course))b on a.snum=b.snum;

select * from student where snum not in (select a.snum from sc a left join course b on a.cnum=b.cnum group by a.snum having count(a.cnum)=(select count(1) from course));

select distinct student.* from (select student.snum,course.cnum from student,course)t1 left join (select sc.snum,sc.cnum from sc) t2 on t1.snum=t2.snum and t1.cnum=t2.cnum,student where t2.snum is null and t1.snum=student.snum;

select * from (select student.snum,course.cnum from student,course)t1 left join (select sc.snum,sc.cnum from sc) t2 on t1.snum=t2.snum and t1.cnum=t2.cnum

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

select distinct a.snum,b.sname from sc a left join student b on a.snum=b.snum where a.cnum in (select cnum from sc where snum='01') and a.snum<>'01';

select distinct b.*  from sc a left join student b on a.snum=b.snum where cnum in (select cnum from sc where snum='01') a.snum<>'01';

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

select b.* from (select * from sc where snum  in (select snum from sc where cnum  in (select cnum from sc where snum='01')) and snum!='01') a left join student b on a.snum=b.snum group by a.snum having count(cnum)=(select count(cnum) from sc where snum='01');

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

select * from student where snum not in (select snum from sc where cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')));

select *from student where student.Snum not in (select student left join sc on student.snum=sc.snum where exists (select * from course a inner join teacher b on a.tnum=b.tnum where tname='张三' and course.cnum=sc.cnum));

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

select a.snum,b.sname,avg(score) from sc a left join student b on a.snum=b.snum where a.score<60 group by a.snum having count(a.cnum)>=2;

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

select a.*,b.* from sc a left join student b on a.snum=b.snum where a.cnum='01' and a.score<60

order by a.score;

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

select snum,max(case cnum when '01' then score else null end)'01',

max(case cnum when '02' then score else null end)'02',

max(case cnum when '01' then score else null end)'03',avg(score) as avg

from sc group by snum order by avg desc;

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

select a.cnum,b.cname,count(*) as 选课人数,sum(case when score<=60 then 1 else 0 end)'[60-0]',concat(sum(case when score<=60 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=70 and score> 60 then 1 else 0 end)'[70-60]',concat(sum(case when score<=70 and score> 60 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=85 and score> 70 then 1 else 0 end)'[85-70]',concat(sum(case when score<=85 and score> 70 then 1 else 0 end)/count(*)*100,'%')占比,sum(case when score<=100 and score> 85 then 1 else 0 end)'[100-85]' ,concat(sum(case when score<=100 and score> 85 then 1 else 0 end)/count(*)*100,'%')占比 from sc a inner join course b on a.cnum=b.cnum group by a.cnum;

select a.cnum,b.cname,count(*) as 选课人数,sum(If (score<=60,1,0))'[60-0]',concat(sum(If (score<=60,1,0))/count(*)*100,'%')占比,sum(if(score<=70 and score> 60,1,0))'[70-60]',concat(sum(if(score<=70 and score> 60,1,0))/count(*)*100,'%')占比,sum(if(score<=85 and score>70,1,0))'[85-70]',concat(sum(if(score<=85 and score>70,1,0))/count(*)*100,'%')占比,sum(if(score<=100 and score>85,1,0))'[100-85]' ,concat(sum(if(score<=100 and score>85,1,0))/count(*)*100,'%')占比 from sc a inner join course b on a.cnum=b.cnum group by a.cnum;

15、按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

select *,rank()over(partition by cnum order by score desc) as rank from sc;

select snum,cnum,score,@rank:=@rank+1 as rn from sc ,(select @rank:=0) as t order by score desc;

select a.score,(select count(b.score) from sc b where b.score>a.score)+1 as rank from sc a order by rank ;

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

select *,case when (@sco=score) then @rank else @rank:=@rank+1 end as rn,

@sco:=score from sc ,(select @rank:=0,@sco:=null) as t order by score desc;

select a.cnum, a.snum, a.score, count(b.score)+1 as rank from sc as a left join sc as b on a.score<b.score and a.cnum = b.cnum group by a.cnum, a.snum,a.score order by a.cnum, rank ASC;

select a.score,(select count(distinct b.score) from sc b where b.score>a.score)+1 as rank from sc a order by rank ;

select a.cnum, a.snum, a.score, count(b.score)+1 as rank from sc as a left join sc as b on a.score<b.score and a.cnum = b.cnum group by a.cnum, a.snum,a.score order by a.cnum, rank ASC;

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

select cnum ,count(cnum) from sc group by cnum;

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

select a.* from student a inner join (select snum,count(cnum) from sc group by snum having count(cnum)=2)b on a.snum=b.snum;

21.查询男生、女生人数

select ssex,count(1) from student group by ssex;

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

select * from student where sname like '%风%';

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

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

select * from student where year(sage)='1990';

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

select cnum,avg(cnum) as avg from sc group by cnum order by avg desc,cnum;

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

select a.snum,b.sname,avg(score) as avg from sc a left join student b on a.snum=b.snum

group by snum having avg>85;

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

select * from sc where cnum in (select cnum from course where cname='数学') and score<60;

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

select * from student a left join sc b on a.snum=b.snum group by a.snum;

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

select a.snum,b.sname,c.cname,a.score from sc a left join student b on a.snum=b.snum inner join course c on a.cnum=c.cnum where score>70;

30.查询存在不及格的课程

select distinct cnum from sc where score<60;

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

select * from student where snum in (select snum from sc where cnum='01' and score>80);

select student.Snum,student.Sname from student ,sc where sc.Cnum='01' and student.Snum=sc.Snum and sc.score>80

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

select cnum,count(cnum) from sc group by cnum;

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

select a.*,b.score from sc b inner join student a on a.snum=b.snum where b.cnum in (select cnum from course where tnum in (select tnum from teacher where tname='张三')) order by score desc limit 1

select a.*,b.score from student a inner join (select a.* from sc a left join course b on a.cnum=b.cnum inner join teacher c on b.tnum=c.tnum where tname='张三')b on a.snum=b.snum

order by b.score desc limit 1;

select * from sc left join student on student.snum=sc.snum left join course on course.cnum=sc.cnum left join teacher on teacher.tnum=course.tnum where teacher.tname='张三' order by score desc limit 1;

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

select * from sc a left join sc b on a.snum=b.snum and a.score=b.score where a.cnum<>b.cnum

group by a.snum,a.cnum;

select * from sc a where exists(select * from sc b where a.snum=b.snum and a.cnum<>b.cnum and a.score=b.score);

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

select cnum,count(cnum) from sc group by cnum having count(cnum)>5;

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

select snum,count(cnum) from sc group by snum having count(cnum)>=2;

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

select * from student where snum in (select snum from sc group by snum having count(cnum) <(select count(1) from course));

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

select *,Year(now())-Year(sage) as age from student;

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

select *,case when substr(sage,6,5)<substr(now(),6,5) then Year(now())-Year(sage)-1 when substr(sage,6,5)>=substr(now(),6,5) then Year(now())-Year(sage) else sage end '年龄' from student;

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

select *,month(sage) as birth_month,month(now()) as now_month from student where month(sage)=month(now());

select * from student where EXTRACT(MONTH FROM student.Sage)=EXTRACT(MONTH FROM CURDATE());

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

select *,month(sage) as birth_month,month(now()) as now_month from student where month(sage)=month(now())+1;

select * from student where EXTRACT(MONTH FROM student.Sage)=EXTRACT(MONTH FROM DATE_ADD(CURDATE(),INTERVAL 1 MONTH))

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

推荐阅读更多精彩内容

  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,864评论 0 8
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,232评论 0 7
  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    望l阅读 315评论 0 0
  • 希望对大家面试有帮助,很经典的几道题,大家有兴趣的话,欢迎大家加群讨论,QQ群号:295383988 问题及描述:...
    hc爱编程阅读 1,892评论 0 12
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,306评论 0 1