从表中检索信息

时间:2012-12-19 11:15:39

标签: sql

我需要检索员工的信息,这些员工的收入比他们部门的平均工资还多......我们有10,20,30,40,50,......等部门继续。在这里,我设法只检索一个部门需要的东西。 (40)我怎样才能为尽可能多的部门做到这一点?

这是我的查询:

SELECT  * FROM    EMPLOYEE where (Department_ID='40')and 
 (
 employee_salary > 
  (select avg(employee_salary) from EMPLOYEE  where  Department_ID='40')   
 )

Datatabledata table

4 个答案:

答案 0 :(得分:4)

希望这样做,

    SELECT  emp.* FROM    EMPLOYEE emp where emp.employee_salary >
      (  select avg(employee_salary) from EMPLOYEE new1 
         where emp.Department_ID=new1.Department_ID 
         group by  Department_ID  
      ) 

答案 1 :(得分:0)

尝试此查询

select * from EMPLOYEE as e1
where e1.employee_salary > (select avg(employee_salary) 
                         from EMPLOYEE as e2 
                         where e1.department_id=e2.department_id
                         group by Department_id)

答案 2 :(得分:0)

你可以试试这个:

    SELECT  *, avg(employee_salary) as average_salary 
FROM    EMPLOYEE 
where Department_ID='40' 
having average_salary<employee_salary

答案 3 :(得分:0)

select e.* from employee as e inner join
    (select department_id, avg(employee_salary) as avg_salary
        from employee group by department_id) as f
on e.department_id = f.department_id
where e.employee_salary > f.avg_salary;