问两道简单的oracle问题

1,查询10号部门与20号部门平均工资的差距。

2,查询dept表中没有员工的部门名称。

刚刚学oracle,遇到很多困难,请大家给与帮助。

1.
一个部门的平均工资:
select avg(工资) from table_name where 部门=10;
select avg(工资) from table_name where 部门=20;
平均工资差距:
select (select avg(工资) from table_name where 部门=10) -
(select avg(工资) from table_name where 部门=20) as xx
from dual;

2.
如果你的dept表中有对应员工信息的话:
select 部门名称,部门id
from dept
group by 部门名称,部门id
having count(1)>0;

如果是两个表,一个部门表dept,一个部门和员工对应关系表table2
这是有员工的部门:
select distinct a.部门名称,a.部门id
from dept a, table2 b
where a.部门id = b.部门id;
没员工的部门:
select 部门名称,部门id
from dept a
where not exists (
select 'x' from table2 b
where a.部门id = b.部门id);
或者:
select 部门名称,部门id
from dept a
where 部门id not in
(select 部门id from table2);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-06
没说明数据库结果,无法给你写出sql。
1、使用avg函数分别计算部分平均工资,然后相减
2、where子句中,使用count和group分组函数找出员工数量为0的部门
第2个回答  2010-04-06
1,select avg(selevt salary from 表 where dept_id=10)-avg(selevt salary from 表 where dept_id=20) from dual;

2,select dept_name from 表 where count(name)<1;
第3个回答  2010-04-06
效率最好的写法

问题1.
SELECT AVG(a.sal)-AVG(b.sal) sal
FROM emp a,emp b
WHERE a.deptno = 10
AND b.deptno = 20;
问题2.
SELECT *
FROM DEPT A
WHERE NOT EXISTS (SELECT 1 FROM EMP B WHERE A.DEPTNO = B.DEPTNO);