查询部门名称是SALES和ACCOUNTING的员工
select *
from emp
where deptno in (select deptno from dept where dname='SALES' or dname='ACCOUNTING');
查询工资比30号部门任意一个员工高的员工信息
select *
from emp
where sal > any(select sal from emp where deptno = 30);
查询工资比30号部门所有员工高的员工信息
select *
from emp
where sal > all (select sal from emp where deptno=30);
等同于:
select *
from emp
where sal > (select max(sal) from emp where deptno=30);
在员工表中查询本身是领导的员工信息(是领导表示自己主键编码在领导编码mgr中出现)
from emp
where empno in (select mgr from emp);
在员工表中查询本身不是领导的员工信息(子查询not in中需要排除null)
select *
from emp
where empno not in (select mgr from emp where mgr is not null)
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO7844TURNERSALESMAN769808-9月 -8115000307521WARDSALESMAN769822-2月 -811250500307654MARTINSALESMAN769828-9月 -811250140030 子查询中的Null问题:
a) 所有和空值比较的函数都返回一个空值,
b) 只要子查询中包含一个空值,那么请不要使用not in操作,
c) 如果用的话,操作等同于 <>all
a not in(100,20,null) ---> 等同于 a!= 100 and a!= 20 and a!=all
因此 not in的子查询中要排除空值,否则不会得到任何数据