1. 使用 AVG() 函数求数值列的平均值
SELECT AVG(`column_name`)
FROM `table_name`;
计算课程表 courses
中学生上课人数 student_count
的平均值
select avg(student_count) as`average_student_count` from courses;
其中 AS
关键字的作用是赋予 AVG(student_count)
计算结果列显示在列表中的别名。
查询教师表 teachers
中教师邮箱为 'qq.com' 结尾的年龄的平均值,最后返回结果列名显示为 'average_teacher_age'
select avg (age) as `average_teacher_age` from teachers where email like'%qq.com';
2.使用 MAX() 函数返回指定列中的最大值
SELECT MAX(`column_name`)
FROM `table_name`;
查询课程表 courses
所有课程中最多的学生上课人数。
SELECT MAX(`student_count`)as max_student_count FROM `courses`;
从教师表 teachers 中,查询最年长的中国教师信息,并返回该教师的年龄.
select max(age) as max_age from teachers where country='CN';
3. 使用 MIN() 函数返回指定列中的最小值
SELECT MIN(`column_name`)
FROM `table_name`;