SQL连接表和MIN子句

时间:2017-02-10 20:08:36

标签: sql

我正在尝试运行一个查询,该查询将显示该经理的经理ID和最低薪员工。它还将排除最低工资为6000美元或更低的任何团体,并且不知道经理。我的代码显示所有这些,除了收入最低的员工。这是我的代码。

select departments.manager_id, employees.salary
from hr.employees, hr.departments, hr.jobs
where employees.department_id = departments.department_id
AND jobs.job_id = employees.job_id
AND departments.manager_id IS NOT NULL
AND salary NOT IN
(select salary
from hr.employees
where salary <= 6000)

1 个答案:

答案 0 :(得分:0)

以下是基于EMP表格可能有帮助的内容......

select * from (
  select min(e1.sal) over( partition by m1.ename ) minsal
, e1.mgr
, e1.ename
, m1.ename Manager
, e1.sal sal
from emp e1
join emp m1
  on e1.mgr = m1.empno
)
where minsal > 1500

    MINSAL        MGR ENAME      MANAGER           SAL
---------- ---------- ---------- ---------- ----------
      3000       7566 FORD       JONES            3000
      3000       7566 SCOTT      JONES            3000
      2450       7839 CLARK      KING             2450
      2450       7839 BLAKE      KING             2850
      2450       7839 JONES      KING             2975