题目描述:使用含有关键字exists查找未分配具体部门的员工的所有信息。
select *
from employees
where not exists
(
select emp_no
from dept_emp
where employees.emp_no=dept_emp.emp_no
)EXISTS语句:执行employees.length次
指定一个子查询,检测行的存在。遍历循环外表,然后看外表中的记录有没有和内表的数据一样的。匹配上就将结果放入结果集中。
select *
from employees
where emp_no not in
(
select emp_no
from dept_emp
)IN 语句:只执行一次
确定给定的值是否与子查询或列表中的值相匹配。in在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选。所以相对内表比较小的时候,in的速度较快。
两者详细资料(Sql 语句中 IN 和 EXISTS 的区别及应用):https://blog.csdn.net/wqc19920906/article/details/79800374

京公网安备 11010502036488号