exists后面(相关子查询)
exists的作用是:判断子查询有没有结果的存在
案例:
select exists(select employee_id from employees); 返回的结果:1;
语法:
select exists(完整的子查询); 子查询有结果返回1,没有结果返回0;
案例:查询有员工的部门名
select department_name from departments d
where exists(select * from employees e where d.department_id=e.department_id );
注意:能用exists的绝对能用前面讲过的in来实现,所以exists很少使用
select department_name from departments d
where d.department_id in (
select department_id from employees
);