设为首页 收藏本站
查看: 1785|回复: 0

[经验分享] SQL 基础之子查询(十一)

[复制链接]

尚未签到

发表于 2018-10-20 12:38:18 | 显示全部楼层 |阅读模式
  子查询:类型、语法、和注意事项
  使用子查询能解决哪些问题?
DSC0000.jpg

  子查询语法:
  select select_list from table where expr operator (select select_list from table);

  •   子查询(内查询)在主查询(外查询)之前执行。
  •   主查询使用子查询结果。
  •   位置:select,where,from,having
  1、查询谁的工资比Abel高
  select last_name, salary from employees
  where salary >
  (select salary
  from employees
  where last_name = 'Abel');
DSC0001.jpg

  使用子查询注意事项

  •   子查询要包含在括号内。
  •   将子查询放在比较条件的右侧增强可读性(子查询可以出现在比较运算符的两侧)
  •   单行操作符对应单行子查询,多行操作符对应多行子查询
  单行子查询:
  – 子查询中的组函数
  – HAVING 子句中的子查询

  •   只返回一行
  •   使用单行比较操作符
操作符含义=等于>大于>=大于等于, ,  'IT_PROG';
DSC0002.jpg

  在多行子查询中使用 ALL  操作符
  select employee_id, last_name, job_id, salary
  from employees
  where salary < all
  (select salary
  from employees
  where job_id = 'IT_PROG')
  and job_id  'IT_PROG';
DSC0003.jpg

  子查询中的空值
  select emp.last_name
  from employees emp
  where emp.employee_id not in
  (select mgr.manager_id
  from employees mgr);
  1、HR 部门的同事想要你帮忙写一个 SQL 语句,该 SQL 语句可以传入一个值(员工的 last_name),然后返回结果是该员工同一部门同事的 last_name 和 hire_date,且要求该员工不在返回结果中。
  举个例子,如果用户输入”Zlotkey”,结果就会返回除了 Zlotkey 之外的同一部门的其他同事的
  last_name 和 hire_date.
  select last_name,hire_date
  from employees
  where department_id =(select department_id from employees
  where last_name= '&&enter_name')
  and last_name < > '&enter_name';
  2、请查询出所有高于平均工资的员工的 employee_id,last_name,salary,并将最终结果根据salary 降序排列。
  select employee_id,last_name,salary
  from employees
  where salary > (select avg(salary)
  from employees)
  order by salary;
  3、请写一条 SQL 语句,要求查询出那些同一部门员工 last_name 里面包含字母”u”的员工的employee_id,last_name。
  select employee_id,last_name from employees where department_id in (select department_id from employees where last_name like '%u%');
  4、请帮助HR部门的同事查出所有部门location_id是1700的员工的last_name,department_id,job_id。
  select last_name,department_id,job_id
  from employees
  where department_id in(select department_id
  from departments
  where location_id=1700);
  让用户可以选择输入一个 location_id,然后输出结果。
  select last_name,department_id,job_id
  from employees
  where department_id in(select department_id
  from departments
  where location_id=&enter_location);
  5、请查出所有需要向 King 汇报的员工的 last_name 以及 salary
  select last_name,salary,manager_id
  from employees
  where manager_id = (select employee_id
  from employees
  where last_name like 'King' and manager_id is null);
  6、请查出所有是执行部(Executive)的员工的 department_id,last_name,job_id
  select department_id,last_name,job_id
  from employees
  where department_id in(select department_id
  from departments
  where department_name like 'Executive');
  7、请查出比 department_id 是 60 的任何员工薪水高的所有的员工的 last_name。
  select department_id,last_name,salary from employees
  where salary > any
  (select salary from employees
  where department_id=60);
  8、查询所有高于平均工资,并且同一部门员工 last_name 里面包含字母”u”的员工的 employee_id,last_name,salary。
  select employee_id,last_name,salary
  from employees
  where department_id in(select department_id
  from employees
  where last_name like '%u%')
  and salary > (select avg(salary) from employees);


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-624076-1-1.html 上篇帖子: 美团点评SQL优化工具 SQLAdvisor 安装和使用 下篇帖子: SQL基础之使用集合运算符进行多表查询(十二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表