SQL笔试 I 经典44题及答案解析~

Aidan075
• 阅读 1284

↑ 关注 + 星标 ~ 有趣的不像个技术号

每晚九点,我们准时相约 SQL笔试 I 经典44题及答案解析~

SQL笔试 I 经典44题及答案解析~

今天这篇文章,是关于44道经典SQL测试题:

01 建表语句

`create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));``insert into Student values('01' , '赵雷' , '1990-01-01' , '男');``insert into Student values('02' , '钱电' , '1990-12-21' , '男');``insert into Student values('03' , '孙风' , '1990-05-20' , '男');``insert into Student values('04' , '李云' , '1990-08-06' , '男');``insert into Student values('05' , '周梅' , '1991-12-01' , '女');``insert into Student values('06' , '吴兰' , '1992-03-01' , '女');``insert into Student values('07' , '郑竹' , '1989-07-01' , '女');``insert into Student values('08' , '王菊' , '1990-01-20' , '女');``create table Course(cid varchar(10),cname varchar(10),tid varchar(10));``insert into Course values('01' , '语文' , '02');``insert into Course values('02' , '数学' , '01');``insert into Course values('03' , '英语' , '03');``create table Teacher(tid varchar(10),tname varchar(10));``insert into Teacher values('01' , '张三');``insert into Teacher values('02' , '李四');``insert into Teacher values('03' , '王五');``create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));``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);`

02 表结构预览

--学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数

1. 查询“01”课程比“02”课程成绩高的所有学生的学号;

`select distinct t1.sid as sidfrom `` (select * from sc where cid='01')t1``left join ` `(select * from sc where cid='02')t2``on t1.sid=t2.sid``where t1.score>t2.score`

2. 查询平均成绩大于60分的同学的学号和平均成绩;

`select `` sid` `,avg(score)``from sc``group by sid``having avg(score)>60`

3. 查询所有同学的学号、姓名、选课数、总成绩

`select` `student.sid as sid` `,sname` `,count(distinct cid) course_cnt` `,sum(score) as total_score``from student``left join sc``on student.sid=sc.sid``group by sid,sname`

4. 查询姓“李”的老师的个数;

`select` `count(distinct tid) as teacher_cnt``from teacher``where tname like '李%'`

5. 查询没学过“张三”老师课的同学的学号、姓名;

`select` `sid,sname``from student``where sid not in ` `(` `select` `sc.sid` `from teacher` `left join course` `on teacher.tid=course.tid` `left join sc` `on course.cid=sc.cid` `where teacher.tname='张三'` `)`

6. 查询学过“01”并且也学过编号“02”课程的同学的学号、姓名;

`select` `t.sid as sid` `,sname``from ` `(` `select` `sid` `,count(if(cid='01',score,null)) as count1` `,count(if(cid='02',score,null)) as count2` `from sc` `group by sid` `having count(if(cid='01',score,null))>0 and count(if(cid='02',score,null))>0` `)t``left join student` `on t.sid=student.sid`

7. 查询学过“张三”老师所教的课的同学的学号、姓名;

`select` `student.sid` `,sname``from ` `(` `select` `distinct cid`` from course` `left join teacher`` on course.tid=teacher.tid` `where teacher.tname='张三'` `)course``left join sc ` `on course.cid=sc.cid``left join student` `on sc.sid=student.sid``group by student.sid,sname`

8. 查询课程编号“01”的成绩比课程编号“02”课程低的所有同学的学号、姓名;

`select` `t1.sid,sname``from ` `(` `select distinct t1.sid as sid` `from`` (select * from sc where cid='01')t1` `left join`` (select * from sc where cid='02')t2` `on t1.sid=t2.sid` `where t1.score>t2.score` `)t1``left join student` `on t1.sid=student.sid`

9. 查询所有课程成绩小于60分的同学的学号、姓名;

`select` `t1.sid,sname``from ` `(` `select` `sid,max(score)` `from sc` `group by sid` `having max(score<60)` `)t1``left join student` `on t1.sid=student.sid`

10. 查询没有学全所有课的同学的学号、姓名;

`select` `t1.sid,sname``from ` `(` `select` `count(cid),sid` `from sc` `group by sid` `having count(cid) < (select count(distinct cid) from course)` `)t1``left join student` `on t1.sid=student.sid`

11. 查询至少有一门课与学号为“01”的同学所学相同的同学的学号和姓名;

`select` `distinct sc.sid``from ` `(` `select` `cid` `from sc` `where sid='01'` `)t1``left join sc` `on t1.cid=sc.cid`

12. 查询和"01"号的同学学习的课程完全相同的其他同学的学号和姓名

`#注意是和'01'号同学课程完全相同但非学习课程数相同的,这里我用左连接解决这个问题select` `t1.sid,sname``from` `(` `select` `sc.sid` `,count(distinct sc.cid)` `from`` (` `select` `cid` `from sc` `where sid='01'` `)t1 #选出01的同学所学的课程` `left join sc` `on t1.cid=sc.cid` `group by sc.sid` `having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = '01')` `)t1``left join student` `on t1.sid=student.sid``where t1.sid!='01'`

13. 把“SC”表中“张三”老师教的课的成绩都更改为此课程的平均成绩;

#暂跳过update题目

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

`select` ` sname``from student``where sid not in` `(` `select` `distinct sid` `from sc` `left join course` `on sc.cid=course.cid` `left join teacher` `on course.tid=teacher.tid`` where tname='张三'` `)`

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

`select` `t1.sid,sname,avg_score``from ` `(` `select` `sid,count(if(score<60,cid,null)),avg(score) as avg_score` `from sc` `group by sid` `having count(if(score<60,cid,null)) >=2` `)t1``left join student` `on t1.sid=student.sid`

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

`select `` sid,if(cid='01',score,100)from sc``where if(cid='01',score,100)<60``order by if(cid='01',score,100) desc`

17. 按平均成绩从高到低显示所有学生的平均成绩

`select sid,avg(score)``from sc``group by sid``order by avg(score) desc`

18. 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率

`select` `sc.cid` `,cname` `,max(score) as max_score` `,min(score) as min_score` `,avg(score) as avg_score` `,count(if(score>=60,sid,null))/count(sid) as pass_rate` `from sc`  `left join course` `on sc.cid=course.cid` `group by sc.cid`

19. 按各科平均成绩从低到高和及格率的百分数从高到低顺序

`#这里先按照平均成绩排序,再按照及格百分数排序,``select `` cid` `,avg(score) as avg_score` `,count(if(score>=60,sid,null))/count(sid) as pass_rate``from sc``group by cid``order by avg_score,pass_rate desc`

20. 查询学生的总成绩并进行排名

`select` `sid` `,sum(score) as sum_score``from sc``group by sid``order by sum_score desc`

21. 查询不同老师所教不同课程平均分从高到低显示

`select` `tid` `,avg(score) as avg_score``from course``left join sc` `on course.cid=sc.cid``group by tid``order by avg_score desc`

22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

`select` `sid,rank_num,score,cid``from` `(` `select` `rank() over(partition by cid order by score desc) as rank_num` `,sid` `,score` `,cid` `from sc` `)t``where rank_num in (2,3)`

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

`select` `sc.cid` `,cname` `,count(if(score between 85 and 100,sid,null))/count(sid)` `,count(if(score between 70 and 85,sid,null))/count(sid)` `,count(if(score between 60 and 70,sid,null))/count(sid)` `,count(if(score between 0 and 60,sid,null))/count(sid)``from sc``left join course` `on sc.cid=course.cid``group by sc.cid,cname`

24. 查询学生平均成绩及其名次

`select` `sid` `,avg_score` `,rank() over (order by avg_score desc)``from ` `(` `select` `sid` `,avg(score) as avg_score` `from sc` `group by sid` `)t`

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

`select` `sid,cid,rank1from`` (` `select` `cid` `,sid` `,rank() over(partition by cid order by score desc) as rank1` `from sc` `)twhere rank1<=3`

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

`select` `count(sid)` `,cid``from sc``group by cid`

27. 查询出只选修了一门课程的全部学生的学号和姓名

`select` `sid``from sc``group by sid``having count(cid) =1`

28. 查询男生、女生人数

`select` `ssex` `,count(distinct sid)from studentgroup by ssex`

29. 查询名字中含有"风"字的学生信息

`select` `sid,sname``from student``where sname like '%风%'`

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

`select` `ssex` `,sname` `,count(sid)``from student``group by ssex,sname``having count(sid)>=2`

31. 查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)

`select` `sid,sname,sage``from student``where year(sage)=1990`

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

`select` `cid,avg(score) as avg_score``from sc``group by cid``order by avg_score,cid desc`

33. 查询不及格的课程,并按课程号从大到小排列

`select` `cid,sid,score``from sc``where score<60``order by cid desc,sid`

34. 查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名;

`select` `sid,cid,scorefrom scwhere cid='01' and score>60`

35. 查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩

`select` `sc.sid,sname,cname,score``from sc``left join course` `style="font-weight: 600;">=course.cid``left join teacher` `style="font-weight: 600;">=teacher.tid``left join student` `style="font-weight: 600;">=student.sid``where tname='张三'``order by score desc``limit 1;`

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

`select` `cid,sid,rank1``from ` `(` `select` `cid` `,sid` `,rank() over(partition by cid order by score desc) as rank1` `from sc`` )t``where rank1 <=2`

37. 统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

`select` `cid` `,count(sid) as cnt``from sc``group by cid``having cnt>=5``order by count(sid) desc,cid`

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

`select` `sid` `,count(cid)``from sc``group by sid``having count(cid)>=2`

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

`select` `sid` `,count(cid)``from sc``group by sid``having count(cid)=(select count(distinct cid) from sc)`

40. 查询各学生的年龄

`select` `sid,sname,year(curdate())-year(sage) as sage``from student`

41 查询本周过生日的学生

`select` `sid,sname,sage``from student``where weekofyear(sage)=weekofyear(curdate())`

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

`select `` sid,sname,sage``from student``where weekofyear(sage) = weekofyear(date_add(curdate(),interval 1 week))`

43 查询本月过生日的学生

`select` `sid,sname,sage``from student``where month(sage) = month(curdate())`

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

`select` `sid,sname,sage``from student``where month(date_sub(sage,interval 1 month)) = month(curdate())`

End.

作者:tomocat

来源:知乎





![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/03759599883da4a16a7a651a8fd6cfe1.png)







**近期文章,点击图片即可查看**








[![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/e3ae2a4dc4e7312ee5871ea840eace49.jpeg)](http://mp.weixin.qq.com/s?__biz=MzU5Nzg5ODQ3NQ==&mid=2247485538&idx=1&sn=709528774666f9b783ab578a4c0a5bc2&chksm=fe4d2de6c93aa4f01805db92db6220bb9f7026b86ceffc850fee83da3d840cd1c4fda10eebc5&scene=21#wechat_redirect)

[![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/e0748da66f8598f193664b8500a13227.jpeg)](http://mp.weixin.qq.com/s?__biz=MzU5Nzg5ODQ3NQ==&mid=2247485482&idx=2&sn=146589c2f04badad8e5182d07ccde9e0&chksm=fe4d2daec93aa4b89f0483cdbca630ace630cbdfde1f21d93426432921e8d7279267721be8ac&scene=21#wechat_redirect)

[![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/4dbe5084137931dd1f0d0d5fad089d51.jpeg)](http://mp.weixin.qq.com/s?__biz=MzU5Nzg5ODQ3NQ==&mid=2247485538&idx=4&sn=236904980fdbfcff9ba84e3770d29d75&chksm=fe4d2de6c93aa4f0784ad2fbe62b8ffbc21056c1e7dc38948cec3291f80980c92e37fff42a3a&scene=21#wechat_redirect)

[![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/1a8025cc9c9d12ba74b1572120a9d9c0.jpeg)](http://mp.weixin.qq.com/s?__biz=MzU5Nzg5ODQ3NQ==&mid=2247485538&idx=3&sn=d46e38cd25fbd5df47b20e15deaba3ff&chksm=fe4d2de6c93aa4f0115f32f76146f7d98cdb14ef5875a086fabdb9e71bc19a9857f3c980d6ba&scene=21#wechat_redirect)



**后台回复关键词「****进群****」,即刻加入读者交流群~**



![](https://img-hello-world.oss-cn-beijing.aliyuncs.com/75aeb2154ab31017e46a6b2948bf3f94.gif)









本文转转自微信公众号凹凸数据原创https://mp.weixin.qq.com/s/ftXeq21VumgP0utAt9noCA,可扫描二维码进行关注: SQL笔试 I 经典44题及答案解析~ 如有侵权,请联系删除。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Karen110 Karen110
3年前
​一篇文章总结一下Python库中关于时间的常见操作
前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在
Stella981 Stella981
3年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Wesley13 Wesley13
3年前
4cast
4castpackageloadcsv.KumarAwanish发布:2020122117:43:04.501348作者:KumarAwanish作者邮箱:awanish00@gmail.com首页:
Stella981 Stella981
3年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Stella981 Stella981
3年前
200的大额人民币即将面世?央行:Yes!
点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/2a1c2ac00bf54458a78c48a6c2e547d5.png)点击上方“印象python”,选择“星标”公众号重磅干货,第一时间送达!!(
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这