Sql 关联子查询以查找其部门中收入高于平均工资的员工
我正在编写一个Oracle查询,以查找其部门内收入高于平均工资的员工。我需要显示该部门的姓氏、员工ID、工资、部门ID和平均工资。我写了这个问题,但没有得到想要的答案。有人能告诉我,我在哪里犯了错误吗Sql 关联子查询以查找其部门中收入高于平均工资的员工,sql,oracle,subquery,correlated-subquery,Sql,Oracle,Subquery,Correlated Subquery,我正在编写一个Oracle查询,以查找其部门内收入高于平均工资的员工。我需要显示该部门的姓氏、员工ID、工资、部门ID和平均工资。我写了这个问题,但没有得到想要的答案。有人能告诉我,我在哪里犯了错误吗 select last_name,employee_id,salary,department_id,avg(salary) from employees e1 where salary > (select avg(salary) from em
select last_name,employee_id,salary,department_id,avg(salary)
from employees e1
where salary > (select avg(salary)
from employees e2
where e1.department_id=e2.department_id)
group by last_name,employee_id,salary,department_id
如果您需要在结果中显示部门平均工资,则不需要使用相关子查询 您可以在单独的查询中计算平均工资,然后加入到该查询中:
with emp as (select 1 emp_id, 'bob' name, 10000 salary, 1 dept_id from dual union all
select 2 emp_id, 'joe' name, 20000 salary, 1 dept_id from dual union all
select 3 emp_id, 'fred' name, 10000 salary, 2 dept_id from dual union all
select 4 emp_id, 'ivy' name, 20000 salary, 2 dept_id from dual union all
select 5 emp_id, 'ann' name, 30000 salary, 2 dept_id from dual union all
select 6 emp_id, 'jo' name, 20000 salary, 3 dept_id from dual union all
select 7 emp_id, 'fion' name, 30000 salary, 3 dept_id from dual union all
select 8 emp_id, 'alan' name, 40000 salary, 3 dept_id from dual union all
select 9 emp_id, 'eve' name, 50000 salary, 3 dept_id from dual union all
select 10 emp_id, 'rob' name, 10000 salary, 4 dept_id from dual)
select emp1.name,
emp1.emp_id,
emp1.salary,
emp1.dept_id,
emp2.average_dept_salary
from emp emp1
inner join (select dept_id,
avg(salary) average_dept_salary
from emp
group by dept_id) emp2
on (emp1.dept_id = emp2.dept_id)
where emp1.salary > emp2.average_dept_salary;
NAME EMP_ID SALARY DEPT_ID AVERAGE_DEPT_SALARY
---- ---------- ---------- ---------- -------------------
joe 2 20000 1 15000
ann 5 30000 2 20000
alan 8 40000 3 35000
eve 9 50000 3 35000
但就我个人而言,我会用一个分析函数得到平均工资,然后用它进行过滤。这样,您就不必扫描表两次:
with emp as (select 1 emp_id, 'bob' name, 10000 salary, 1 dept_id from dual union all
select 2 emp_id, 'joe' name, 20000 salary, 1 dept_id from dual union all
select 3 emp_id, 'fred' name, 10000 salary, 2 dept_id from dual union all
select 4 emp_id, 'ivy' name, 20000 salary, 2 dept_id from dual union all
select 5 emp_id, 'ann' name, 30000 salary, 2 dept_id from dual union all
select 6 emp_id, 'jo' name, 20000 salary, 3 dept_id from dual union all
select 7 emp_id, 'fion' name, 30000 salary, 3 dept_id from dual union all
select 8 emp_id, 'alan' name, 40000 salary, 3 dept_id from dual union all
select 9 emp_id, 'eve' name, 50000 salary, 3 dept_id from dual union all
select 10 emp_id, 'rob' name, 10000 salary, 4 dept_id from dual),
res as (select name,
emp_id,
salary,
dept_id,
avg(salary) over (partition by dept_id) average_dept_salary
from emp)
select name,
emp_id,
salary,
dept_id,
average_dept_salary
from res
where salary > average_dept_salary;
NAME EMP_ID SALARY DEPT_ID AVERAGE_DEPT_SALARY
---- ---------- ---------- ---------- -------------------
joe 2 20000 1 15000
ann 5 30000 2 20000
alan 8 40000 3 35000
eve 9 50000 3 35000
这里有一种方法,你可以通过一个相关的查询(实际上是两个)来做你想做的事情,但是我真的,真的建议不要使用这个。首先,现在同一个表有3个传递
with emp as (select 1 emp_id, 'bob' name, 10000 salary, 1 dept_id from dual union all
select 2 emp_id, 'joe' name, 20000 salary, 1 dept_id from dual union all
select 3 emp_id, 'fred' name, 10000 salary, 2 dept_id from dual union all
select 4 emp_id, 'ivy' name, 20000 salary, 2 dept_id from dual union all
select 5 emp_id, 'ann' name, 30000 salary, 2 dept_id from dual union all
select 6 emp_id, 'jo' name, 20000 salary, 3 dept_id from dual union all
select 7 emp_id, 'fion' name, 30000 salary, 3 dept_id from dual union all
select 8 emp_id, 'alan' name, 40000 salary, 3 dept_id from dual union all
select 9 emp_id, 'eve' name, 50000 salary, 3 dept_id from dual union all
select 10 emp_id, 'rob' name, 10000 salary, 4 dept_id from dual)
select emp1.name,
emp1.emp_id,
emp1.salary,
emp1.dept_id,
(select avg(salary)
from emp emp3
where emp1.dept_id = emp3.dept_id) average_dept_salary
from emp emp1
where emp1.salary > (select avg(salary)
from emp emp2
where emp1.dept_id = emp2.dept_id);
NAME EMP_ID SALARY DEPT_ID AVERAGE_DEPT_SALARY
---- ---------- ---------- ---------- -------------------
joe 2 20000 1 15000
ann 5 30000 2 20000
alan 8 40000 3 35000
eve 9 50000 3 35000
你能用相关子查询回答这个问题吗?你为什么需要相关子查询?我的意思是,您可以使用相关查询进行过滤,但是为了在结果中显示平均薪资,您需要进行选择以获得薪资。既然有更好、更高效的方法,为什么还要麻烦呢?
with emp as (select 1 emp_id, 'bob' name, 10000 salary, 1 dept_id from dual union all
select 2 emp_id, 'joe' name, 20000 salary, 1 dept_id from dual union all
select 3 emp_id, 'fred' name, 10000 salary, 2 dept_id from dual union all
select 4 emp_id, 'ivy' name, 20000 salary, 2 dept_id from dual union all
select 5 emp_id, 'ann' name, 30000 salary, 2 dept_id from dual union all
select 6 emp_id, 'jo' name, 20000 salary, 3 dept_id from dual union all
select 7 emp_id, 'fion' name, 30000 salary, 3 dept_id from dual union all
select 8 emp_id, 'alan' name, 40000 salary, 3 dept_id from dual union all
select 9 emp_id, 'eve' name, 50000 salary, 3 dept_id from dual union all
select 10 emp_id, 'rob' name, 10000 salary, 4 dept_id from dual)
select emp1.name,
emp1.emp_id,
emp1.salary,
emp1.dept_id,
(select avg(salary)
from emp emp3
where emp1.dept_id = emp3.dept_id) average_dept_salary
from emp emp1
where emp1.salary > (select avg(salary)
from emp emp2
where emp1.dept_id = emp2.dept_id);
NAME EMP_ID SALARY DEPT_ID AVERAGE_DEPT_SALARY
---- ---------- ---------- ---------- -------------------
joe 2 20000 1 15000
ann 5 30000 2 20000
alan 8 40000 3 35000
eve 9 50000 3 35000