MySQL 简单查询(实验回顾)

2020/11/2 数据库实验回顾

year() 和 now() 练习

查询教师的姓名,生日,出生年和年龄

select 姓名, year(生日) as 出生年, year(now()) - year(生日) as 年龄
from teacher;

Image

distinct练习

以10年为一个年代,查询teacher表中老师出生于哪个年代。(div整除

select distinct (year(生日) div 10) * 10
from teacher;

Image

between and 练习

查询工资在5000到6000之间的教师

select * from teacher
where 工资 between 5000 and 6000;

Image

in 练习

查询工资为2300,2400,3600或4300之一的教师

select * from teacher
where 工资 in (2300, 2400, 3600, 4300);

Image

like 练习

查询含有“原理”的课程

select 课号, 课名 from course
where 课名 like '%原理%';

Image

order by 练习

按工资的降序显示所有教师的工号、姓名和工资

select 工号, 姓名, 工资 from teacher
order by 工资 desc;

Image

limit 练习

查询长得最高的学生

select * from student
order by 身高
limit 1;

Image

统计查询练习

查询老师的人数、总工资、平均工资、最高工资和最低工资

select count(工号) as 人数,
sum(工资) as 总工资,
avg(工资) as 平均工资,
max(工资) 最高工资,
min(工资) 最低工资
from teacher;

Image

分组查询练习

  1. 查询教师人数在3人以上的职称及其人数
select 职称, count(工号) as 人数
from teacher
group by 职称
having 人数 >= 3 ;

Image

  1. 查询score表中有哪些学号不及格的课程数超过了1门,他们不及格的课程数是多少?
select 学号, count(课号) as 不及格课程数
from score
where 成绩 < 60
group by 学号
having 不及格课程数 > 1;

Image

  1. 查询平均年龄最大的系的名字及其平均年龄值
select 系别, avg(年龄) as 平均年龄
from student
group by 系别
order by 平均年龄 desc
limit 1;

Image

54
原文地址:https://www.cnblogs.com/Liwker/p/13917324.html