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)

相关推荐
杰哥在此2 小时前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
GISer_Jing8 小时前
【React】增量传输与渲染
前端·javascript·面试
Neituijunsir13 小时前
2024.09.22 校招 实习 内推 面经
大数据·人工智能·算法·面试·自动驾驶·汽车·求职招聘
希望有朝一日能如愿以偿14 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
小飞猪Jay14 小时前
面试速通宝典——10
linux·服务器·c++·面试
猿java15 小时前
Cookie和Session的区别
java·后端·面试
数据分析螺丝钉15 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
无理 Java15 小时前
【技术详解】SpringMVC框架全面解析:从入门到精通(SpringMVC)
java·后端·spring·面试·mvc·框架·springmvc
鱼跃鹰飞16 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
TANGLONG22216 小时前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯