编写查询以显示在deptno 10或30中没有报表管理器的ename?

时间:2018-11-26 20:32:07

标签: sql oracle oracle11g

SQL> SELECT * FROM emp;  


  EMPNO  ENAME  JOB       MGR   HIREDATE   SAL   COMM  DEPTNO  
  7369   SMITH   CLERK     7902  17-DEC-80  2900        20
  7499   ALLEN   SALESMAN  7698  20-FEB-81  3600  300   30
  7521   WARD    SALESMAN  7698  22-FEB-81  3250  500   30
  7566   JONES   MANAGER   7839  02-APR-81  4975        20
  7654   MARTIN  SALESMAN  7698  28-SEP-81  3250  1400  30
  7698   BLAKE   MANAGER   7839  01-MAY-81  4850        30
  7782   CLARK   MANAGER   7839  09-JUN-81  4450        10
  7788   SCOTT   ANALYST   7566  19-APR-87  5000        20
  7839   KING    PRESIDENT       17-NOV-81  7000        10
  7844   TURNER  SALESMAN  7698  08-SEP-81  3500  0     30
  7876   ADAMS   CLERK     7788  23-MAY-87  3100        20
  7900   JAMES   CLERK     7698  03-DEC-81  2950        30
  7902   FORD    ANALYST   7566  03-DEC-81  5000        20
  7934   MILLER  CLERK     7782  23-JAN-82  3300        10  

这是Oracle SQL中提供的emp表。

我需要显示部门10或30中没有报告经理的员工。

5 个答案:

答案 0 :(得分:0)

您可以通过左外部联接使用自我联接,如下所示:

Select ename
from emp e1
left outer join emp e2 on e2.MGR = e1.EMPNO 
where e2.MGR is null AND e1.DEPTNO in (10,30)

答案 1 :(得分:0)

您可以将子查询与相关子查询一起使用:

select ename
  from 
  (
    select e.ename, e.mgr,
        ( select mgr 
            from emp 
           where empno = e.mgr 
             and nvl(deptno,0) not in (10,30) ) mgr2
      from emp e
  )
 where mgr is null 
    or mgr2 is not null;

ENAME
------
KING
SCOTT
FORD
SMITH
ADAMS

答案 2 :(得分:0)

我使用了With条款:

with emp_data as (select EMPNO,DEPTNO from emp)
select ENAME from emp e,emp_data ed
where e.mgr =ed.empno(+) and 
(ed.deptno not in (10,30) or e.mgr is null);
ENAME
------
FORD
SCOTT
ADAMS
SMITH
KING

答案 3 :(得分:-1)

我会使用not exists

select e.*
from emp e
where not exists (select 1
                  from emp em
                  where em.empno = e.mgr and em.deptno in (10, 30)
                 );

答案 4 :(得分:-1)

选择*从EMP MGR为空且确定为(10,30);