SQL Aggregate 函数计算从列中取得的值,返回一个单一的值
有用的 Aggregate 函数:
AVG() - 返回平均值
COUNT() - 返回行数
FIRST() - 返回第一个记录的值
LAST() - 返回最后一个记录的值
MAX() - 返回最大值
MIN() - 返回最小值
SUM() - 返回总和
AVG() 函数返回数值列的平均值
SELECT AVG(column_name) FROM table_name
从 "student" 表的 "grade" 列获取分数平均值:
select avg(grade) from student;
查询分数高于平均分的学生:
select * from student where grade > (select avg(grade) from student);
COUNT() 函数返回匹配指定条件的行数
SQL COUNT(column_name) 语法
COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(column_name) FROM table_name;
SQL COUNT(*) 语法
COUNT(*) 函数返回表中的记录数:
SELECT COUNT(*) FROM table_name;
SQL COUNT(DISTINCT column_name) 语法
COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:
SELECT COUNT(DISTINCT column_name) FROM table_name;
注:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access。
FIRST() 函数返回指定的列中第一个记录的值
SQL FIRST() 语法
SELECT FIRST(column_name) FROM table_name;
注:只有 MS Access 支持 FIRST() 函数。
SQL Server 语法
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name ASC;
MySQL 语法
SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
实例
SELECT name FROM student
ORDER BY id ASC
LIMIT 1;
Oracle 语法
SELECT column_name FROM table_name
ORDER BY column_name ASC
WHERE ROWNUM <=1;
实例
SELECT name FROM student
ORDER BY id ASC
WHERE ROWNUM <=1;
LAST() 函数返回指定的列中最后一个记录的值
SQL LAST() 语法
SELECT LAST(column_name) FROM table_name;
注:只有 MS Access 支持 LAST() 函数
SQL Server 语法
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name DESC;
MySQL 语法
SELECT column_name FROM table_name
ORDER BY column_name DESC
LIMIT 1;
实例
SELECT name FROM student
ORDER BY id DESC
LIMIT 1;
Oracle 语法
SELECT column_name FROM table_name
ORDER BY column_name DESC
WHERE ROWNUM <=1;
实例
SELECT name FROM student
ORDER BY id DESC
WHERE ROWNUM <=1;
MAX() 函数返回指定列的最大值
SELECT MAX(column_name) FROM table_name;
MIN() 函数返回指定列的最小值
SELECT MIN(column_name) FROM table_name;
SUM() 函数返回数值列的总数
SELECT SUM(column_name) FROM table_name;
GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
GROUP BY 简单应用
统计 class班级中 各个 stu_id 的成绩,实例
SELECT stu_id, SUM(class.grade) AS nums
FROM class GROUP BY stu_id;
SQL GROUP BY 多表连接
SELECT student.name,COUNT(classes.stu_id) AS nums FROM classes
LEFT JOIN student
ON classes.stu_id=student.id
GROUP BY student.name;
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。
HAVING 子句可以让我们筛选分组后的各组数据
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
现在我们想要查找学生总数大于 45 的班级,并且 分数 小于 60
SELECT student.name, SUM(classes.count) AS nums FROM student
INNER JOIN classes
ON student.id=classes.stu_id
WHERE student.grade < 60
GROUP BY student.name
HAVING SUM(classes.count) > 45;
EXISTS 运算符用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
查找班级学生数(count 字段)大于 45 的班级是否存在。
SELECT student.name FROM student
WHERE EXISTS
(SELECT count FROM classes WHERE student.id = classes.stu_id AND count > 45);
EXISTS 可以与 NOT 一同使用,查找出不符合查询语句的记录
SELECT student.name
FROM student
WHERE NOT EXISTS (SELECT count FROM classes WHERE student.id = classes.stu_id AND count > 45);