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)

相关推荐
007php0073 小时前
某游戏大厂 Java 面试题深度解析(四)
java·开发语言·python·面试·职场和发展·golang·php
倔强青铜三5 小时前
苦练Python第73天:玩转对象持久化,pickle模块极速入门
人工智能·python·面试
程序员三藏6 小时前
Postman持久化保存/设置断言详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
夏鹏今天学习了吗7 小时前
【LeetCode热题100(56/100)】组合总和
算法·leetcode·职场和发展
绝无仅有8 小时前
某游戏大厂的常用面试问题解析:Netty 与 NIO
后端·面试·架构
测试19988 小时前
如何写出一个完整的测试用例?
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
绝无仅有8 小时前
某游戏大厂的 Redis 面试必问题解析
后端·算法·面试
拉不动的猪10 小时前
深入理解 JavaScript 中的静态属性、原型属性与实例属性
前端·javascript·面试
光军oi11 小时前
面试Redis篇—————缓存穿透问题及解决策略
redis·缓存·面试