mysql-面试50题-3

一、查询数据

ymysql-面试50题-2-CSDN博客

二、问题

21.查询男生、女生人数

mysql> select ssex, count(*) from student

-> group by ssex;

+------+----------+

| ssex | count(*) |

+------+----------+

| 女 | 8 |

| 男 | 4 |

+------+----------+

2 rows in set (0.02 sec)

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

mysql> select *

-> from student

-> where student.Sname like '%风%'

-> ;

+------+--------+---------------------+------+

| SId | Sname | Sage | Ssex |

+------+--------+---------------------+------+

| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |

+------+--------+---------------------+------+

1 row in set (0.00 sec)

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

mysql> select sname, count(*) from student

-> group by sname

-> having count(*)>1;

+--------+----------+

| sname | count(*) |

+--------+----------+

| 李四 | 2 |

+--------+----------+

1 row in set (0.00 sec)

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

mysql> select *

-> from student

-> where YEAR(student.Sage)=1990;

+------+--------+---------------------+------+

| SId | Sname | Sage | Ssex |

+------+--------+---------------------+------+

| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |

| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |

| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |

| 04 | 李云 | 1990-12-06 00:00:00 | 男 |

+------+--------+---------------------+------+

4 rows in set (0.00 sec)

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

mysql> select sc.CId,avg(sc.score) from sc,course

-> group by sc.cid

-> order by avg(sc.score) desc,cid asc;

+------+---------------+

| CId | avg(sc.score) |

+------+---------------+

| 02 | 72.66667 |

| 03 | 68.50000 |

| 01 | 64.50000 |

+------+---------------+

3 rows in set (0.00 sec)

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

mysql> select student.sid, AVG(sc.score) as aver from student, sc

-> where student.sid = sc.sid

-> group by sc.sid

-> having aver > 85;

+------+----------+

| sid | aver |

+------+----------+

| 01 | 89.66667 |

| 07 | 93.50000 |

+------+----------+

2 rows in set (0.00 sec)

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

mysql> select student.sname, sc.score from student, sc, course

-> where student.sid = sc.sid

-> and course.cid = sc.cid

-> and course.cname = "数学"

-> and sc.score < 60;

+--------+-------+

| sname | score |

+--------+-------+

| 李云 | 30.0 |

+--------+-------+

1 row in set (0.00 sec)

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

mysql> select student.sname, cid, score from student

-> left join sc

-> on student.sid = sc.sid;

+--------+------+-------+

| sname | cid | score |

+--------+------+-------+

| 赵雷 | 01 | 80.0 |

| 赵雷 | 02 | 90.0 |

| 赵雷 | 03 | 99.0 |

| 钱电 | 01 | 70.0 |

| 钱电 | 02 | 60.0 |

| 钱电 | 03 | 80.0 |

| 孙风 | 01 | 80.0 |

| 孙风 | 02 | 80.0 |

| 孙风 | 03 | 80.0 |

| 李云 | 01 | 50.0 |

| 李云 | 02 | 30.0 |

| 李云 | 03 | 20.0 |

| 周梅 | 01 | 76.0 |

| 周梅 | 02 | 87.0 |

| 吴兰 | 01 | 31.0 |

| 吴兰 | 03 | 34.0 |

| 郑竹 | 02 | 89.0 |

| 郑竹 | 03 | 98.0 |

| 张三 | NULL | NULL |

| 李四 | NULL | NULL |

| 李四 | NULL | NULL |

| 赵六 | NULL | NULL |

| 孙七 | NULL | NULL |

+--------+------+-------+

23 rows in set (0.00 sec)

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

mysql> select student.sname, course.cname,sc.score from student,course,sc

-> where sc.score>70

-> and student.sid = sc.sid

-> and sc.cid = course.cid;

+--------+--------+-------+

| sname | cname | score |

+--------+--------+-------+

| 赵雷 | 语文 | 80.0 |

| 赵雷 | 数学 | 90.0 |

| 赵雷 | 英语 | 99.0 |

| 钱电 | 英语 | 80.0 |

| 孙风 | 语文 | 80.0 |

| 孙风 | 数学 | 80.0 |

| 孙风 | 英语 | 80.0 |

| 周梅 | 语文 | 76.0 |

| 周梅 | 数学 | 87.0 |

| 郑竹 | 数学 | 89.0 |

| 郑竹 | 英语 | 98.0 |

+--------+--------+-------+

11 rows in set (0.00 sec)

30.查询不及格的课程

mysql> select DISTINCT sc.CId

-> from sc

-> where sc.score <60;

+------+

| CId |

+------+

| 01 |

| 02 |

| 03 |

+------+

3 rows in set (0.00 sec)

相关推荐
swipe1 小时前
RabbitMQ 实战:AI Agent 里异步处理的标配方案(手把手 + 4 种交换机全解析)
后端·面试·langchain
时针滴滴答啊3 小时前
最大子数组和
算法·leetcode·职场和发展
重生之后端学习4 小时前
15. 三数之和[中等]✅
java·数据结构·算法·leetcode·职场和发展
早点睡9756 小时前
Python 上下文管理器深度剖析:从 with 语法糖到 CPython 底层协议
后端·面试
嵌入式阿蔡6 小时前
面试高频考点 01:volatile / 中断 / 堆栈 八股精讲
java·面试·职场和发展·嵌入式实时数据库
小的~~7 小时前
面试被问分布式锁,我差点语塞…直到搞懂了Redis、ZooKeeper和etcd的“三国杀”
redis·分布式·面试
程序员爱钓鱼7 小时前
Go 编程实战:匿名函数 Anonymous Function——没有名字的函数与灵活回调
后端·面试·go
To_OC16 小时前
面试被问了三回三栏布局,这次我终于把 BFC 那层窗户纸捅破了
前端·css·面试
城管不管17 小时前
重生——第十一次面试之挖财一面2026.8.19已OC
java·服务器·jvm·数据库·spring·面试·职场和发展
码匠许师傅17 小时前
【C++ 面试真题】26. 聊聊 C++ 的智能指针
java·c++·面试