检索info sql查询

时间:2012-12-19 10:30:48

标签: sql

SELECT  *
FROM    EMPLOYEE a
        LEFT JOIN EMPLOYEE b
            ON a.Employee_ID <> b.Employee_ID
WHERE   a.employee_salary < b.employee_salary and a.Department_ID='30'

您好我想要检索比30部门员工赚更多钱的员工的所有信息

image of database

3 个答案:

答案 0 :(得分:2)

SELECT  * FROM    EMPLOYEE 
where (Department_ID<>'30')
and 
 (
 employee_salary > 
  (select max(employee_salary) from EMPLOYEE  where  Department_ID='30')
 )

如果您需要员工的收入超过所有部门30,请使用SUM代替MAX。

答案 1 :(得分:0)

拆分问题将有助于我们:

  • 首先:您需要知道有30多名员工获得了多少钱。
  • 第二:雇佣赚取超过这笔钱的员工。

简单的方法是使用子查询进行查询。主查询检索员工的收入超过X,子查询返回X

<强>子查询 30多名员工获得了多少钱?

Select employee_salary from employee where Department_ID='30'
带有子查询的

主查询: 赚钱的员工比这笔钱多:

Select * from employee 
where employee_salary > ANY (
   Select employee_salary from employee where Department_ID='30'
)

请注意,您可以在子查询运算符中使用ANYALL

  • ALL表示您正在寻找的员工人数超过{30}员工的ALL
  • ANY表示您正在寻找的员工人数超过{30}员工的ANY

答案 2 :(得分:0)

SELECT * fROM EMPLOYEE a where a.Department_ID!='30' 
and (a.employee_salary > (select max(b.employee_salary) 
from EMPLOYEE b where b.Department_ID='30'))