题目:查找未分配部门的员工具体信息
select * from employees
where emp_no not in
(select emp_no from dept_emp)
上述语句先运行from语句,再进行in后面的子查询,子查询结束后再进行where语句筛选。
select * from employees e
where not exists
(select emp_no from dept_emp d where d.emp_no = e.emp_no)
上述语句先运行select * from employees e,根据主查询得到的表与子查询的where条件对比返回符合条件的记录。
所以如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists。