MySQL 简单查询(实验回顾)
2020/11/2 数据库实验回顾
year() 和 now() 练习
查询教师的姓名,生日,出生年和年龄
select 姓名, year(生日) as 出生年, year(now()) - year(生日) as 年龄
from teacher;
distinct练习
以10年为一个年代,查询teacher表中老师出生于哪个年代。(div整除)
select distinct (year(生日) div 10) * 10
from teacher;
between and 练习
查询工资在5000到6000之间的教师
select * from teacher
where 工资 between 5000 and 6000;
in 练习
查询工资为2300,2400,3600或4300之一的教师
select * from teacher
where 工资 in (2300, 2400, 3600, 4300);
like 练习
查询含有“原理”的课程
select 课号, 课名 from course
where 课名 like '%原理%';
order by 练习
按工资的降序显示所有教师的工号、姓名和工资
select 工号, 姓名, 工资 from teacher
order by 工资 desc;
limit 练习
查询长得最高的学生
select * from student
order by 身高
limit 1;
统计查询练习
查询老师的人数、总工资、平均工资、最高工资和最低工资
select count(工号) as 人数,
sum(工资) as 总工资,
avg(工资) as 平均工资,
max(工资) 最高工资,
min(工资) 最低工资
from teacher;
分组查询练习
- 查询教师人数在3人以上的职称及其人数
select 职称, count(工号) as 人数
from teacher
group by 职称
having 人数 >= 3 ;
- 查询score表中有哪些学号不及格的课程数超过了1门,他们不及格的课程数是多少?
select 学号, count(课号) as 不及格课程数
from score
where 成绩 < 60
group by 学号
having 不及格课程数 > 1;
- 查询平均年龄最大的系的名字及其平均年龄值
select 系别, avg(年龄) as 平均年龄
from student
group by 系别
order by 平均年龄 desc
limit 1;

本文作者:Liwker
本文链接:https://www.cnblogs.com/Liwker/p/13917324.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步