1.求出每个部门薪水的平均值
先讲几个函数:max(),min(),avg(),sum(),count()----组函数

select count(distinct deptno) from emp;
select count(deptno) from emp;

聚合函数,例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于一组的数据起作用。当你指定 GROUP BY region 时, 属于同一个region(地区)的一组数据将只能返回一行值.也就是说,表中所有除region(地区)外的字段,只能通过 SUM, COUNT等聚合函数运算后返回一个值. 

HAVING子句可以让我们筛选成组后的各组数据. WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前.而 HAVING子句在聚合后对组记录进行筛选。

select avg(sal),deptno from emp group by deptno having avg(sal)>2000;

转换为字符串函数:
select to_char(avg(sal),'99999999.99') from emp;注意to_char()里面的用法
select to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp;
select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;
select to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;
select to_char(hiredate,'YYYY-MM-DD') from emp;注意日期规范格式
select to_char(sal,'$99,999.9999') from emp;9-代表以为数字,L-代表本地货币,0-代表一位数字-->为空补齐为0
日期转换为字符函数:
select ename,hiredate from emp where hiredate > to_date('1982-2-20 12:34:56','YYYY-MM-DD HH:MI:SS');
to_number()特定字符串转换为数字to_number('$1,250.00','$9,999.99');
nvl(comm,0)处理空格函数

SQL> select avg(sal) from emp group by deptno;
 
SQL> select count(*) from emp
  2  group by deptno;
 
SQL> select sum(sal) from emp group by deptno;
 
SQL> select sum(sal),count(*),sum(sal)/count(*) from emp group by deptno;
 
2.聚合汇总总结:
select avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;

3.子查询(存在from或where子句里)
查询出高于所有人平均工资的员工
select ename from emp where sal>(select avg(sal) from emp);
查询出每个部门工资最高的员工
select ename,sal from emp
join (select max(sal) max_sal,deptno from emp group by deptno) t
on (emp.sal=t.max_sal and emp.deptno=t.deptno);

4.自身连接
选出员工名字及其对应的经理名字
select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno;
1999标准,where只写过滤条件,不写连接条件
old---select ename,dname from emp,dept where emp.deptno=dept.deptno
new---select ename,dname from emp join dept on (emp.deptno=dept.deptno)等同于下边语句
等值连接--select ename,dname from emp join dept using (deptno)--不推荐使用-限制条件多-字段必须有,数据类型相等
非等值连接--select ename,dname,grade from emp e join dept d on (e.deptno=d.deptno)
join salgrade s on (e.sal between s.losal and s.hisal) where ename not like '_A%';

左外连接--左边表中多余的数据也显示出来--e1.mgr=e2.empno(+)左连接
select e1.ename,e2.ename from emp e1 left join emp e2 on (e1.mgr=e2.empno);
右连接--右边表中多余的数据也显示出来--e1.mgr(+)=e2.empno右连接
select ename,dname from emp e right outer join dept d on (e.deptno=d.deptno)

5.求部门薪水最高

select ename,sal from emp join (select max(sal) max_sal,deptno from emp group by deptno) t
on (emp.sal=t.max_sal and emp.deptno=t.deptno

select e.ename,e.sal from emp e,(select max(sal) as maxsal,deptno from emp group by deptno) t where e.deptno=t.deptno and e.sal=t.maxsal;

求部门平均薪水等级

select deptno,avg_sal,grade from (select deptno,avg(sal) avg_sal from emp group by deptno ) t
join salgrade s on (t.avg_sal between s.losal and s.hisal);

select s.grade,t.deptno,s.losal,s.hisal,t.avg_sal from salgrade s,(select deptno,avg(sal) avg_sal from emp group by deptno) t
where t.avg_sal between s.losal and s.hisal;

求部门平均的薪水等级

select deptno,avg(grade) from
(select deptno,ename,grade from emp join salgrade s on (emp.sal between s.losal and s.hisal)) t
group by deptno;

select avg(grade),t.deptno from (select s.grade,e.deptno from emp e,salgrade s where e.sal between s.losal and s.hisal) t group by deptno;

雇员中哪些人是经理人

select ename from emp where empno in (select distinct mgr from emp);

不准用组函数,求薪水的最高值

select distinct sal from emp
where sal not in
(select distinct e1.sal from emp e1 join emp e2 on (e1.sal<e2.sal));

平均薪水最高的部门编号

select deptno,avg_sal from
(select avg(sal) avg_sal,deptno from emp group by deptno)
where avg_sal=(select max(avg_sal) from
(select avg(sal) avg_sal,deptno from emp
group by deptno
));

备注:
sys用户是超级用户,权限最高,具有sysdba角色,由create database的权限,默认密码是:manager
system是管理操作员,权限也很大,具有sysoper角色,没有create database权限,密码是:change_on_install
一般来讲对数据库维护用system的用户足以