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

[经验分享] SQL基础之使用集合运算符进行多表查询(十二)

[复制链接]

尚未签到

发表于 2018-10-20 12:39:21 | 显示全部楼层 |阅读模式
  集合操作:类型和注意事项
DSC0000.jpg

  集合操作注意事项

  •   在 SELECT 列表中的列名和表达式在数量上必须匹配
  •   第二个查询中的每一列的数据类型必须与第一个查询其对应的列的数据类型相匹配
  •   可以用括号来改变的执行顺序。
  •   ORDER BY  子句:
  –  只能在语句的最后出现
  –  可以使用第一个查询中的列名,别名或相对位置
  Oracle 服务器和集合操作符

  •   除 UNION ALL之外,系统会自动删除重复的记录
  •   列名为第一个查询返回的结果
  •   除 UNION ALL之外,系统自动按照第一个查询中的第一个列的升序排列
DSC0001.jpg

  使用 UNION  操作符
  1、显示当前和以前的工作的所有员工的详细信息。每个雇员只显示一次。
  select employee_id, job_id
  from employees
  union
  select employee_id, job_id
  from job_history;
DSC0002.jpg

  使用 UNION ALL  操作符
DSC0003.jpg

  1、显示当前和以前的员工的所有部门。
  select employee_id, job_id, department_id
  from employees
  union all
  select employee_id, job_id, department_id
  from job_history
  order by employee_id;
DSC0004.jpg

  使用 INTERSECT  操作符
DSC0005.jpg

  显示员工ID和工作ID,当前的职称相同(也就是说,他们换工作但是现在已经回到以前同样的工作)。
  select employee_id, job_id
  from employees
  intersect
  select employee_id, job_id
  from job_history;
DSC0006.jpg

  使用 MINUS  操作符 DSC0007.jpg
  1、显示员工表中一次都没有改变过工作的的员工ID
  select employee_id
  from employees
  minus
  select employee_id
  from job_history;
DSC0008.jpg

  相匹配的 SELECT  语句

  •   使用 UNION 操作符显示location_id,department_name,state_province
  •   当字段在一个或另一个表中不存在,必须匹配上数据类型(使用TO_CHAR函数或其他转换函数)
  select location_id, department_name "Department",
  to_char(null) "warehouse location"
  from departments
  union
  select location_id, to_char(null) "Department",
  state_province
  from locations;
  使用UNION操作符,显示雇员的ID,工作ID,和所有员工的工资
  select employee_id, job_id,salary
  from employees
  union
  select employee_id, job_id,0
  from job_history;
DSC0009.jpg

  集合操作中使用 ORDER BY  子句的注意事项

  •   复合查询中 ORDER BY 子句只能在结束时出现一次
  •   集合操作中每个查询不能有单独的 ORDER BY 子句
  •   ORDER BY 子句中 只能识别第一个 SELECT 查询的列。
  •   默认情况下,第一列的第一个 SELECT 查询使用升序对输出进行排序。
  请查询出所有的部门下没有 ST_CLERK 工种的 department_id,要求使用集合操作符
  select department_id
  from departments
  minus
  select department_id
  from employees
  where job_id not like 'ST_CLERK';
DSC00010.jpg

  2、请使用集合操作符写一条 SQL,查出所有的没有部门坐落的国家的 country_id,country_name
  select country_id,country_name
  from countries
  minus
  select l.country_id,c.country_name
  from locations l join countries c
  on (l.country_id=c.country_id)
  join departments d
  on d.location_id=l.location_id;
DSC00011.jpg

  3、请使用集合操作符写一条 SQL,查出部门号在 10,50,20 中的所有的 job_id,department_id,
  并以 10,50,20 的排列顺序显示。
  select distinct job_id,department_id
  from employees
  where department_id = 10
  union all
  select distinct job_id,department_id
  from employees
  where department_id= 50
  union all
  select distinct job_id,department_id
  from employees
  where department_id= 20;
DSC00012.jpg

  4、请查出所有工作发生过变动,但是多次变动后现在的工作是做的以前做过的工作的员工的employee_id 和 job_id
  select employee_id,job_id
  from employees
  intersect
  select employee_id,job_id
  from job_history;
DSC00013.jpg

  5、HR 部门的同事希望你能够帮助他们生成一个报告,要求满足以下 2 点:
  a)从 EMPLOYEES 表中获得所有员工的 last_name 和 department_id(不管是否属于同一个部门)
  b)从 DEPARTMENTS 表中获得所有的 department_id 和 department_name(不管是否该部门有员工)
  select last_name,department_id,to_char(null) dept_name
  from employees
  union
  select to_char(null),department_id,department_name
  from departments;
  LAST_NAME  DEPARTMENT_ID DEPT_NAME
  ------------------------- ------------- ------------------------------
  Abel     80
  Ande     80
  Atkinson     50
  Austin     60
  Baer     70
  Baida     30
  Banda     80
  Bates     80
  Bell     50
  Bernstein     80
  Bissot     50
  Bloom     80
  Bull     50
  Cabrio     50
  Cambrault     80
  Chen    100
  Chung     50
  Colmenares     30
  Davies     50
  De Haan      90
  Dellinger     50
  Dilly     50
  Doran     80
  Ernst     60
  Errazuriz     80
  Everett      50
  Faviet    100
  Fay     20
  Feeney     50
  Fleaur     50
  Fox     80
  Fripp     50
  Gates     50
  Gee     50
  Geoni     50
  Gietz    110
  Grant     50
  Grant
  Greenberg    100
  Greene     80
  Hall     80
  Hartstein     20
  Higgins     110
  Himuro     30
  Hunold     60
  Hutton     80
  Johnson      80
  Jones     50
  Kaufling     50
  Khoo     30
  King     80
  King     90
  Kochhar      90
  Kumar     80
  Ladwig     50
  Landry     50
  Lee     80
  Livingston     80
  Lorentz      60
  Mallin     50
  Markle     50
  Marlow     50
  Marvins      80
  Matos     50
  Mavris     40
  McCain     50
  McEwen     80
  Mikkilineni     50
  Mourgos      50
  Nayer     50
  OConnell     50
  Olsen     80
  Olson     50
  Ozer     80
  Partners     80
  Pataballa     60
  Patel     50
  Perkins      50
  Philtanker     50
  Popp    100
  Rajs     50
  Raphaely     30
  Rogers     50
  Russell      80
  Sarchand     50
  Sciarra     100
  Seo     50
  Sewall     80
  Smith     80
  Stiles     50
  Sullivan     50
  Sully     80
  Taylor     50
  Taylor     80
  Tobias     30
  Tucker     80
  Tuvault      80
  LAST_NAME  DEPARTMENT_ID DEPT_NAME
  ------------------------- ------------- ------------------------------
  Urman    100
  Vargas     50
  Vishney      80
  Vollman      50
  Walsh     50
  Weiss     50
  Whalen     10
  Zlotkey      80
  10 Administration
  20 Marketing
  30 Purchasing
  40 Human Resources
  50 Shipping
  60 IT

  70 Public>  80 Sales
  90 Executive
  100 Finance
  110 Accounting
  120 Treasury
  130 Corporate Tax
  140 Control And Credit
  150 Shareholder Services
  160 Benefits
  170 Manufacturing
  180 Construction
  190 Contracting
  200 Operations
  210 IT Support
  220 NOC
  230 IT Helpdesk
  240 Government Sales
  250 Retail Sales
  260 Recruiting
  270 Payroll


运维网声明 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-624077-1-1.html 上篇帖子: SQL 基础之子查询(十一) 下篇帖子: 如何做SQL Server性能测试?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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