SQL> select * from salgrade;
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
SQL> select avg(sal),sum(sal) from emp;
AVG(SAL) SUM(SAL)
---------- ----------
2073.21429 29025
SQL> select min(sal),max(sal) from emp;
MIN(SAL) MAX(SAL)
---------- ----------
800 5000
SQL> select count(*) from emp;
COUNT(*)
----------
14
SQL> select count(empno) from emp;
COUNT(EMPNO)
------------
14
SQL> select count(deptno) from emp;
COUNT(DEPTNO)
-------------
14
SQL> select depno from emp;
select depno from emp
*
第 1 行出现错误:
ORA-00904: "DEPNO": 标识符无效
SQL> select deptno from emp;
DEPTNO
----------
20
30
30
20
30
30
10
20
10
30
20
DEPTNO
----------
30
20
10
已选择14行。
SQL> select count(distinct deptno) from emp;
COUNT(DISTINCTDEPTNO)
---------------------
3
4.
3.
2.
1.
\
select count(distinct deptno)from emp; --distinct 去除重复项
去重 select count(distinct deptno) from emp;
使用分组函数1
1、AVG(平均值)和SUM(合计)函数
·求出员工的平均工资和工资的总额
select avg(sal),sum(sal) from emp;
2、MIN(最小值)和MAX(最大值)函数
·求出员工工资的最大值和最小值
select max(sal),min(sal) from emp;
3、COUNT(计数)函数
·求出员工的总人数
select count(*) from emp;
select count(empno) from emp;
4、DISTINCT 关键字(用于去掉重复的记录)
·求出部门数
select count(distinct deptno) from emp;
select avg (表中列字段) ,sum (表中列字段) from 表; --列出 表中字段的平均值,和
select max (表中的字段), min (表中的字段) from 表 ; --列出表中字段的 最大值 最小值
select count(*) from 表; 输出这个表中一共有的数据条数
select count(distinct 表中字段) from 表; 输出表中不重复字段的个数
distinct 用于去重
avg函数与sum函数
min函数与max函数
count函数,可加distinct关键字
select count(distinct deptno) from emp等价于select count(deptno) from emp group by deptno;
avg (表中列字段) ,
sum (表中列字段) from 表; --列出 表中字段的平均值,和
max (表中的字段),
min (表中的字段) from 表 ; --列出表中字段的 最大值 最小值
count(*) 输出这个表中一共有的数据条数
count(distinct 表中字段) f; 输出表中不重复字段的个数
distinct 用于去重
select avg(sal),sum(sal) from emp;
select min(sal),min(sal) from emp;
select count(*) from emp;
select count (empno) from emp;
select count(deptno) from emp;
select count(distinct deptno) from emp;
select avg (表中列字段) ,sum (表中列字段) from 表; --列出 表中字段的平均值,和
select max (表中的字段), min (表中的字段) from 表 ; --列出表中字段的 最大值 最小值
select count(*) from 表; 输出这个表中一共有的数据条数
select count(distinct 表中字段) from 表; 输出表中不重复字段的个数
distinct 用于去重