JAVA开发常用数据库技术——组函数和分组查询

-- 组函数

-- 1)COUNT() 统计行数
-- 2)AVG() 平均值
-- 3)SUM() 求和
-- 4)MAX() 最大值
-- 5)MIN() 最小值

-- count() 函数
select count(*) from scott.emp;

-- sum() 函数
select sum(sal) from scott.emp;

-- avg() 函数
-- 平均值 = 总数 / 人数
select sum(sal)/count(ename) from scott.emp;
select avg(sal) from scott.emp;

-- max() 函数
select max(sal) from scott.emp;

-- min() 函数
select min(sal) from scott.emp;

select avg(sal), max(sal), min(sal) from scott.emp;

-- 查询的时候,注意结果的行数,如果不对称的话,则会报错
-- select ename, sum(sal) from scott.emp;

-- 分组查询

-- 使用 group by 语句。
-- 如果不想报错的话,需要将 select 后面列表中的字段添加到 group by 子句中即可。
-- 其实很简单,就是以 deptno 作为一个参照物,根据参照物定义出条件对象。
select deptno, sum(sal) 
from scott.emp
group by deptno;

-- 如果想在分组后,还需要进行条件过滤
-- 可以使用 having 关键字,追加条件
select deptno, sum(sal) 
from scott.emp
group by deptno
having sum(sal) > 10000;

-- 常见的关键字使用顺序:
-- select > from > where > group by > having > order by
 

猜你喜欢

转载自blog.csdn.net/qq_41991836/article/details/82226353