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)

相关推荐
零售ERP菜鸟2 小时前
范式革命:从“信息化”到“数字化”的本质跃迁
大数据·人工智能·职场和发展·创业创新·学习方法·业界资讯
发现一只大呆瓜2 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
千寻girling3 小时前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试
程序员清风4 小时前
北京回长沙了,简单谈谈感受!
java·后端·面试
网络安全-杰克4 小时前
2026面试自动化测试面试题【含答案】
自动化测试·软件测试·面试·职场和发展
努力学算法的蒟蒻6 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
CodeSheep程序羊6 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
学历真的很重要6 小时前
【系统架构师】第二章 操作系统知识 - 第二部分:进程与线程(补充版)
学习·职场和发展·系统架构·系统架构师
hqyjzsb7 小时前
盲目用AI提效?当心陷入“工具奴”陷阱,效率不增反降
人工智能·学习·职场和发展·创业创新·学习方法·业界资讯·远程工作
千寻girling8 小时前
主管:”人家 Node 框架都用 Nest.js 了 , 你怎么还在用 Express ?“
前端·后端·面试