ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL-子查询

2021-11-17 17:34:43  阅读:131  来源: 互联网

标签:salary employees 查询 MySQL department where id select


子查询

单行子查询

--主查询
select employee _id, last_name.salary
from employees
where salary >(
    select salary
    from employees
    where last_name='abel'
);

多行子查询

  • any:满足值列表中,任意一个条件即可
  • OR 满足所有值列表中的任意一个。
  • in :等于值列表中任意一个
-- 题目:返回其它部门中,比job_id为‘IT_PROG’部门 任一工资低的员工的 员工号、姓名、job_id以及salary
select employee_id , last_name, job_id,salary
from employees
where salary <any (
    select salary 
    from employees 
    where job_id='IT_PROG'
) AND job_id !='IT_PROG';

相关子查询

-- 问题:查询员工中工资大于本部门平均工资的员工的last_name, salary 和其department_id及平均工资

select last_name, salary, department_id, (select avg(salary) from employees where department_id=e1.department_id)
from employees e1
where salary >(
    select avg(salary)
    from employees e2
    where e2.departent_id= e1.department_id
)
-- 解法二
select last_name, salary, e1.department_id,e2.avg_sal
from employees e1, (
    select avg(salary) avg_sal, department_id
    from employees 
    group by departent_id
)e2
where e1.departent_id =e2.department_id
and e1.salary>e2.avg_sal;

其它子查询

--问题:查询与141号或者174号员工的manager_id和department_id相同的其他员工的employee_id, manager_id ,department_id 
select employee_id, manager_id, department_id
from employees
where manager_id in (
    select manager_id 
    from employees
    where employee_id in (141,174)
) and department_id in (
    delect department_id 
    form employees 
    where employee_id in (141, 174)
);

标签:salary,employees,查询,MySQL,department,where,id,select
来源: https://blog.csdn.net/lhjueuue/article/details/121383307

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有