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)

相关推荐
崎岖Qiu4 分钟前
【Spring篇08】:理解自动装配,从spring.factories到.imports剖析
java·spring boot·后端·spring·面试·java-ee
天真小巫7 小时前
2025.6.27总结
职场和发展
心平愈三千疾7 小时前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
漂流瓶jz7 小时前
清除浮动/避开margin折叠:前端CSS中BFC的特点与限制
前端·css·面试
我不吃饼干9 天前
鸽了六年的某大厂面试题:你会手写一个模板引擎吗?
前端·javascript·面试
我不吃饼干9 天前
鸽了六年的某大厂面试题:手写 Vue 模板编译(解析篇)
前端·javascript·面试
LyaJpunov9 天前
深入理解 C++ volatile 与 atomic:五大用法解析 + 六大高频考点
c++·面试·volatile·atomic
前端fighter9 天前
为什么需要dependencies 与 devDependencies
前端·javascript·面试
前端fighter9 天前
Vuex 与 Pinia:全面解析现代 Vue 状态管理的进化之路
前端·vue.js·面试
亲爱的非洲野猪9 天前
一次性理解Java垃圾回收--简单直接方便面试时使用
java·jvm·面试