子查询使用的关键字
1.IN关键字
确定某行的特定列的值是否包含在子查询的结果集中。例如,使用子查询查看所有部门在某一地区(1700)的雇员信息。
select employee_id,last_name,department_id
from employees
where department_id in(
select department_id
from departments
where location_id=1700);
2.EXISTS关键字
EXISTS关键字只注重子查询是否返回行,如果子查询返回一个或多个行,那么EXISTS便返回为TRUE,否则返回FALSE。要使EXISTS关键字有意义,则应在子查询中建立搜索条件。
select employee_id,last_name from employees em
where exists(
select * from departments dep
where em.department_id =dep.department_id
and location_id=1700);
3.比较运算符
如果可以确认子查询返回的结果只包含一个单值,那么可以直接使用比较运算符链接子查询。经常使用的比较运算符有:=(等于)、<>(不等于)、<、>、<=、>=。eg:查询employees表,将薪资大于本职位平均薪资的雇员信息显示出来。
select employee_id,last_name,job_id,salary
from employees
where job_id='PU_MAN' and
salary>=(select avg(salary)) from employees