********************使用order by 進行排序*************************
需求1:
--查詢所有部門信息 按部門編號降序排列
select * from dept order by deptno desc
寫了order by deptno 以后 如果沒有指定升序還是降序 默認是升序
相當於寫上 asc 降序是 desc
需求2:
--查詢所有的員工的信息 根據薪水升序排列
select * from emp order by sal
需求3:
---查詢員工的姓名、薪水、部門編號 先按部門編號升序、部門編號相同的按薪水降序
select ename,sal,deptno from emp order by deptno asc,sal desc
需求4:
--查詢姓名第二個字母不是A的,並且月薪大於1000的員工的
--姓名和年薪(不包括獎金),按照工資降序排序.
select ename,sal*12 + nvl(comm,0) as 年薪 from emp where
ename not like '_A%' and sal>1000
order by sal desc
oracle中的函數類似於java中的方法,可以提供某些功能..
特點: oracle中函數必須有返回值!
分類:
1. 單行函數
一行輸入 一行輸出
2. 組函數
多行輸入 一行輸出
*************************單行函數*****************************
一行輸入 一行輸出
*****************組函數(也叫做聚合函數)*************************
多行輸入 一行輸出
1. 最大值 max
2. 最小值 min
3. 平均值 avg
4. 求和 sum
5. 求總共有多少條記錄
select count(*) from emp where deptno=10
--需求1:求每個部門的平均薪水
select deptno,avg(sal) from emp group by deptno
小技巧:
1.一說每就需要分組,而且每后面的條件就是分組的條件
2. 只要使用 5個組函數其中之一,都需要分組
group by 分組的規則
一旦使用了group by 進行分組, select 后面的字段 要么是5個組函數
之一、要么是分組的條件,不能寫其他字段
使用having 對分組后的數據進行過濾
對記錄進行過濾 使用where,分組后對分組后的數據進行過濾 使用having
select 語句總結:
select * from emp
where 條件
group by deptno
having //對分組進行過濾
order by avg(sal) desc