第十五天 子查询

子查询:

凡是需要分步进行而后几步需要依赖前几步时,就需要用到子查询。
分布进行时先计算子查询。
可以把子查询当做函数。

练习:

1.工资大于全公司平均工资的员工姓名。

select last_name,salary
from employees
where salary>
(select avg(salary) from employees); //先查询出全公司平均员工作为子查询

2.在Seattle工作的所有员工姓名

select last_name

from employees

where department_id in

(select department_id from departments

where location_id=

(select location_id from locations where city='Seattle'));

配对子查询:

练习:和Feeney在同一个部门、做同一职位的员工姓名:

select last_name, department_id, job_id

from employees

where (department_id, job_id)=

(select department_id, job_id from employees where last_name='Feeney')

and last_name != 'Feeney';

 

posted @ 2017-07-31 20:31  我会发光kk  阅读(75)  评论(0编辑  收藏  举报