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 员工多的员工的信息
SELECT * FROM EMPLOYEE
where (Department_ID<>'30')
and
(
employee_salary >
(select max(employee_salary) from EMPLOYEE where Department_ID='30')
)
或者,如果您需要的员工收入超过所有部门 30,请使用 SUM 而不是 MAX。
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'))
拆分问题将帮助我们:
最简单的方法是使用子查询进行查询。主查询检索员工比赚多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'
)
请注意,您可以在子查询运算符中使用ANY
or :ALL
ALL
意味着您正在寻找的员工比收入超过ALL
dpt 30 的员工。ANY
意味着您正在寻找的员工比收入超过ANY
dpt 30 的员工。