设为首页 加入收藏

TOP

常用的sql函数(三)
2014-11-24 07:23:04 来源: 作者: 【 】 浏览:6
Tags:常用 sql 函数
-
5000
求最小值
SQL> select min(sal) from emp;
MIN(SAL)
----------
800
求平均值
SQL> select avg(sal) from emp;
AVG(SAL)
----------
2073.21429
先得到平均值,再转换成字符串形式:
SQL> select to_char(avg(sal),'9999999.99') from emp;
TO_CHAR(AVG
-----------
2073.21
对平均值进行四舍五入保留两位处理:
SQL> select round(to_char(avg(sal),'999999.99'),2)from emp;
ROUND(TO_CHAR(AVG(SAL),'999999.99'),2)
--------------------------------------
2073.21
求和:
SQL> select sum(sal)from emp;
SUM(SAL)
----------
29025
SQL> select count(*) from emp;
COUNT(*)
----------
14
求有多少行:
SQL> select count(*) from emp where deptno = 10;
COUNT(*)
----------
3
查询有多少个姓名:
SQL> select count(ename) from emp;
COUNT(ENAME)
------------
14
SQL> select count(distinct deptno) from emp;
COUNT(DISTINCTDEPTNO)
---------------------
3
分组函数:group by
(查询员工的姓名,薪水,部门号)
SQL>select ename,sal ,deptno from emp;
通过分组查一个部门中,平均工资
SQL>select avg(sal) from emp group by deptno;
通过对部门分组查询每一个部门的平均工资
SQL>select deptno,avg(sal) from emp group by deptno;
通过对部门和工作的分组,查询最高工资
SQL>select max(sal) from emp group by deptno ,job;
把部门和工作分组中最高的工资显示出来
SQL>select deptno ,job,max(sal) from emp group by deptno ,job;
查询所有员工中的最高工资
SQL>select max(sal) from emp;
对deptno分组,查询最高工资(注意:没有在select列表中出现的名称,不能出现在
主函数和 分组列表中)
SQL>select deptno,max(sal) from emp group by deptno;
Having()对分组进行限制:
where 是对单条语句进行过滤;Having()可以对分组语句进行过滤;
部门的平均工资大于2000的显示:
SQL>select deptno,avg(sal) from emp group by deptno having avg(sal) >2000;
最基本的语句的执行顺序,先进行select查询,进行单条语句的过滤,
再进行分组,然后最分组后的进行过滤,再进行排序:
eg:对于工资大于1200的按部门进行分组,分组后的平均薪水大于1500,然后按降序进行排列:
SQL>select avg(sal) from emp where sal>1200 group by deptno having avg(sal) > 1500 order by avg(sal) desc;
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQLServer2008-2012资源及性能监.. 下一篇SQLServer2008-2012资源及性能监..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)