(11)子查询、列子查询
作者:互联网
子查询:
含义:
嵌套在其他语句内部的select语句成为自居查询或者内查询
外面的语句可以是insert/updatae/delete/SELECT
外面如果为select语句,则外面的语句成为外查询或主查询
select (SELECT last_name from employees)
分类:
按出现的位置:
select后面:
仅仅只支持标量子查询
from后面:
表子查询
where或having后面:
标量子查询
列查询
行子查询
实例:
where或having后面
@1 标量子查询
需求:查询最低工资的员工姓名和工资
(1)先查询最低工资
SELECT min(salary)from employees;
(2)查询员工的姓名和工资,要求工资=(1)
SELECT last_name,salary from employees where salary=(SELECT min(salary) from employees);
需求:谁的工资比Abel高?
SELECT salary FROM employees where last_name='Abel';
需求:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
select job_id from employees where employee_id=141;
select salary from employees where employee_id=143;
SELECT last_name,salary,job_id
from employees
where job_id=(select job_id from employees where employee_id=141)
end salary>(select job_id from employees where employee_id=143);
列子查询:
查询所有领导的员工姓名
(1)查询所有员工的manger_id
SELECT DISTINCT manager_id from employees;
(2)查询姓名,employee_id属于(1)列表
SELECT last_name from employees where employee_id in (SELECT DISTINCT manager_id from employees);
需求:返回location_id是1400或1700的部门中的所有员工姓名
(1)查询location_id 是1400或1700的部门编号
select department_id from departments where location_id in (1400,1700);
(2)查询员工姓名,要求部门编号为(1)列表中的结果
SELECT last_name from employees where department_id in (select department_id from departments where location_id in (1400,1700));
标签:11,employees,查询,select,列子,where,id,SELECT 来源: https://blog.csdn.net/weixin_44847304/article/details/90716923