SlideShare a Scribd company logo
Oracle OCP 考试系列培训
                之
         1Z0-007 Lesson7
                   www.OracleOnLinux.cn




7-1   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7
        Using the Set Operators




Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Objectives


      After completing this lesson, you should be able to do
      the following:
       • Describe set operators
       • Use a set operator to combine multiple queries
           into a single query
       • Control the order of rows returned




7-3          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Set Operators
      A         B                 A          B


                                                       UNION/UNION ALL


      A         B


                           INTERSECT


      A         B


                           MINUS



7-4       Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Tables Used in This Lesson


      The tables used in this lesson are:
       • EMPLOYEES: Provides details regarding all
          current employees
       • JOB_HISTORY: Records the details of the start
          date and end date of the former job, and the job
          identification number and department when an
          employee switches jobs




7-5          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-6   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-7   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-8   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
UNION Operator


                         A                           B




      The UNION operator returns results from both
         queries after eliminating duplications.

7-9     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Using the UNION Operator


       Display the current and previous job details of all
       employees. Display each employee only once.
        SELECT   employee_id, job_id
        FROM     employees
        UNION
        SELECT   employee_id, job_id
        FROM     job_history;




         …
         …



7-10          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-11   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
UNION ALL Operator


                            A                           B




       The UNION ALL operator returns results from both
              queries, including all duplications.

7-12       Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Using the UNION ALL Operator
       Display the current and previous departments of all
       employees.
        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;



         …

         …



7-13          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
INTERSECT Operator


                          A                           B




       The INTERSECT operator returns rows that are
                common to both queries.

7-14     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Using the INTERSECT Operator


       Display the employee IDs and job IDs of those
       employees who currently have a job title that is the
       same as their job title when they were initially hired
       (that is, they changed jobs but have now gone back to
       doing their original job).
         SELECT employee_id, job_id
         FROM   employees
         INTERSECT
         SELECT employee_id, job_id
         FROM   job_history;




7-15          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
MINUS Operator


                            A                           B




       The MINUS operator returns rows in the first query
           that are not present in the second query.

7-16       Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
MINUS Operator

   Display the employee IDs of those employees who have not
   changed their jobs even once.
       SELECT   employee_id,job_id
       FROM     employees
       MINUS
       SELECT   employee_id,job_id
       FROM     job_history;




       …




7-17        Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Set Operator Guidelines


       •   The expressions in the SELECT lists must match in
           number and data type.
       •   Parentheses can be used to alter the sequence of
           execution.
       •   The ORDER BY clause:
           – Can appear only at the very end of the statement
           – Will accept the column name, aliases from the first
             SELECT statement, or the positional notation




7-18         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
The Oracle Server and Set Operators


       •   Duplicate rows are automatically eliminated
           except in UNION ALL.
       •   Column names from the first query appear in the
           result.
       •   The output is sorted in ascending order by default
           except in UNION ALL.




7-19         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Matching the SELECT Statements
   Using the UNION operator, display the department ID,
   location, and hire date for all employees.
       SELECT department_id, TO_NUMBER(null)
              location, hire_date
       FROM   employees
       UNION
       SELECT department_id, location_id, TO_DATE(null)
       FROM   departments;




   …




7-20        Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Matching the SELECT Statement:
                      Example

   Using the UNION operator, display the employee ID, job
   ID, and salary of all employees.

       SELECT   employee_id, job_id,salary
       FROM     employees
       UNION
       SELECT   employee_id, job_id,0
       FROM     job_history;




   …



7-21        Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Controlling the Order of Rows

       Produce an English sentence using two UNION
       operators.
       COLUMN a_dummy NOPRINT
       SELECT 'sing' AS "My dream", 3 a_dummy
       FROM dual
       UNION
       SELECT 'I''d like to teach', 1 a_dummy
       FROM dual
       UNION
       SELECT 'the world to', 2 a_dummy
       FROM dual
       ORDER BY a_dummy;




7-22         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-23   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Summary


       In this lesson, you should have learned how to:
        • Use UNION to return all distinct rows
        • Use UNION ALL to return all rows, including
            duplicates
        • Use INTERSECT to return all rows that are shared
            by both queries
        • Use MINUS to return all distinct rows that are
            selected by the first query but not by the second
        • Use ORDER BY only at the very end of the
            statement



7-24          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Practice 7: Overview


       In this practice, you use the set operators to create
       reports:
        • Using the UNION operator
        • Using the INTERSECTION operator
        • Using the MINUS operator




7-25          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-26   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
7-27   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

More Related Content

Similar to Lesson07

Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
Salman Memon
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
Maria
 
Lesson01 学会使用基本的SQL语句
Lesson01 学会使用基本的SQL语句Lesson01 学会使用基本的SQL语句
Lesson01 学会使用基本的SQL语句
renguzi
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句
renguzi
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Salman Memon
 
Database Management Systems SQL And DDL language
Database Management Systems SQL And DDL languageDatabase Management Systems SQL And DDL language
Database Management Systems SQL And DDL language
HSibghatUllah
 
Les01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptLes01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.ppt
DrZeeshanBhatti
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
Jagadish Prasath
 
Les07
Les07Les07
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
Salman Memon
 
Les07 (using the set operators)
Les07 (using the set operators)Les07 (using the set operators)
Les07 (using the set operators)Achmad Solichin
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
rehaniltifat
 

Similar to Lesson07 (20)

plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
Les07
Les07Les07
Les07
 
Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565Les07.ppt 0125566655656959652323265656565
Les07.ppt 0125566655656959652323265656565
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Les02.ppt
Les02.pptLes02.ppt
Les02.ppt
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 
Lesson01 学会使用基本的SQL语句
Lesson01 学会使用基本的SQL语句Lesson01 学会使用基本的SQL语句
Lesson01 学会使用基本的SQL语句
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Database Management Systems SQL And DDL language
Database Management Systems SQL And DDL languageDatabase Management Systems SQL And DDL language
Database Management Systems SQL And DDL language
 
Les01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptLes01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.ppt
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 
Les15
Les15Les15
Les15
 
Les06
Les06Les06
Les06
 
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
 
Les07
Les07Les07
Les07
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Les07 (using the set operators)
Les07 (using the set operators)Les07 (using the set operators)
Les07 (using the set operators)
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 

Recently uploaded

在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
5spllj1l
 
5 Tips for Creating Standard Financial Reports
5 Tips for Creating Standard Financial Reports5 Tips for Creating Standard Financial Reports
5 Tips for Creating Standard Financial Reports
EasyReports
 
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Godwin Emmanuel Oyedokun MBA MSc PhD FCA FCTI FCNA CFE FFAR
 
Turin Startup Ecosystem 2024 - Ricerca sulle Startup e il Sistema dell'Innov...
Turin Startup Ecosystem 2024  - Ricerca sulle Startup e il Sistema dell'Innov...Turin Startup Ecosystem 2024  - Ricerca sulle Startup e il Sistema dell'Innov...
Turin Startup Ecosystem 2024 - Ricerca sulle Startup e il Sistema dell'Innov...
Quotidiano Piemontese
 
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
qntjwn68
 
APP I Lecture Notes to students 0f 4the year
APP I  Lecture Notes  to students 0f 4the yearAPP I  Lecture Notes  to students 0f 4the year
APP I Lecture Notes to students 0f 4the year
telilaalilemlem
 
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
bbeucd
 
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdfBONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
coingabbar
 
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdfTumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
Henry Tapper
 
NEW NORMAL! WHAT BECOMES OF ACCOUNTING PROFESSION
NEW NORMAL!  WHAT BECOMES OF ACCOUNTING PROFESSION NEW NORMAL!  WHAT BECOMES OF ACCOUNTING PROFESSION
NEW NORMAL! WHAT BECOMES OF ACCOUNTING PROFESSION
Godwin Emmanuel Oyedokun MBA MSc PhD FCA FCTI FCNA CFE FFAR
 
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic DataThe Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
Champak Jhagmag
 
Instant Issue Debit Cards
Instant Issue Debit CardsInstant Issue Debit Cards
Instant Issue Debit Cards
egoetzinger
 
What website can I sell pi coins securely.
What website can I sell pi coins securely.What website can I sell pi coins securely.
What website can I sell pi coins securely.
DOT TECH
 
This assessment plan proposal is to outline a structured approach to evaluati...
This assessment plan proposal is to outline a structured approach to evaluati...This assessment plan proposal is to outline a structured approach to evaluati...
This assessment plan proposal is to outline a structured approach to evaluati...
lamluanvan.net Viết thuê luận văn
 
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFiTdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
nimaruinazawa258
 
How Non-Banking Financial Companies Empower Startups With Venture Debt Financing
How Non-Banking Financial Companies Empower Startups With Venture Debt FinancingHow Non-Banking Financial Companies Empower Startups With Venture Debt Financing
How Non-Banking Financial Companies Empower Startups With Venture Debt Financing
Vighnesh Shashtri
 
when will pi network coin be available on crypto exchange.
when will pi network coin be available on crypto exchange.when will pi network coin be available on crypto exchange.
when will pi network coin be available on crypto exchange.
DOT TECH
 
Seminar: Gender Board Diversity through Ownership Networks
Seminar: Gender Board Diversity through Ownership NetworksSeminar: Gender Board Diversity through Ownership Networks
Seminar: Gender Board Diversity through Ownership Networks
GRAPE
 
SWAIAP Fraud Risk Mitigation Prof Oyedokun.pptx
SWAIAP Fraud Risk Mitigation   Prof Oyedokun.pptxSWAIAP Fraud Risk Mitigation   Prof Oyedokun.pptx
SWAIAP Fraud Risk Mitigation Prof Oyedokun.pptx
Godwin Emmanuel Oyedokun MBA MSc PhD FCA FCTI FCNA CFE FFAR
 
The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.
DOT TECH
 

Recently uploaded (20)

在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
在线办理(TAMU毕业证书)美国德州农工大学毕业证PDF成绩单一模一样
 
5 Tips for Creating Standard Financial Reports
5 Tips for Creating Standard Financial Reports5 Tips for Creating Standard Financial Reports
5 Tips for Creating Standard Financial Reports
 
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
Tax System, Behaviour, Justice, and Voluntary Compliance Culture in Nigeria -...
 
Turin Startup Ecosystem 2024 - Ricerca sulle Startup e il Sistema dell'Innov...
Turin Startup Ecosystem 2024  - Ricerca sulle Startup e il Sistema dell'Innov...Turin Startup Ecosystem 2024  - Ricerca sulle Startup e il Sistema dell'Innov...
Turin Startup Ecosystem 2024 - Ricerca sulle Startup e il Sistema dell'Innov...
 
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
1:1制作加拿大麦吉尔大学毕业证硕士学历证书原版一模一样
 
APP I Lecture Notes to students 0f 4the year
APP I  Lecture Notes  to students 0f 4the yearAPP I  Lecture Notes  to students 0f 4the year
APP I Lecture Notes to students 0f 4the year
 
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB毕业证)圣芭芭拉分校毕业证如何办理
 
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdfBONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
BONKMILLON Unleashes Its Bonkers Potential on Solana.pdf
 
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdfTumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
Tumelo-deep-dive-into-pass-through-voting-Feb23 (1).pdf
 
NEW NORMAL! WHAT BECOMES OF ACCOUNTING PROFESSION
NEW NORMAL!  WHAT BECOMES OF ACCOUNTING PROFESSION NEW NORMAL!  WHAT BECOMES OF ACCOUNTING PROFESSION
NEW NORMAL! WHAT BECOMES OF ACCOUNTING PROFESSION
 
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic DataThe Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
The Rise of Generative AI in Finance: Reshaping the Industry with Synthetic Data
 
Instant Issue Debit Cards
Instant Issue Debit CardsInstant Issue Debit Cards
Instant Issue Debit Cards
 
What website can I sell pi coins securely.
What website can I sell pi coins securely.What website can I sell pi coins securely.
What website can I sell pi coins securely.
 
This assessment plan proposal is to outline a structured approach to evaluati...
This assessment plan proposal is to outline a structured approach to evaluati...This assessment plan proposal is to outline a structured approach to evaluati...
This assessment plan proposal is to outline a structured approach to evaluati...
 
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFiTdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
Tdasx: Unveiling the Trillion-Dollar Potential of Bitcoin DeFi
 
How Non-Banking Financial Companies Empower Startups With Venture Debt Financing
How Non-Banking Financial Companies Empower Startups With Venture Debt FinancingHow Non-Banking Financial Companies Empower Startups With Venture Debt Financing
How Non-Banking Financial Companies Empower Startups With Venture Debt Financing
 
when will pi network coin be available on crypto exchange.
when will pi network coin be available on crypto exchange.when will pi network coin be available on crypto exchange.
when will pi network coin be available on crypto exchange.
 
Seminar: Gender Board Diversity through Ownership Networks
Seminar: Gender Board Diversity through Ownership NetworksSeminar: Gender Board Diversity through Ownership Networks
Seminar: Gender Board Diversity through Ownership Networks
 
SWAIAP Fraud Risk Mitigation Prof Oyedokun.pptx
SWAIAP Fraud Risk Mitigation   Prof Oyedokun.pptxSWAIAP Fraud Risk Mitigation   Prof Oyedokun.pptx
SWAIAP Fraud Risk Mitigation Prof Oyedokun.pptx
 
The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.The secret way to sell pi coins effortlessly.
The secret way to sell pi coins effortlessly.
 

Lesson07

  • 1. Oracle OCP 考试系列培训 之 1Z0-007 Lesson7 www.OracleOnLinux.cn 7-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 2. 7 Using the Set Operators Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 3. Objectives After completing this lesson, you should be able to do the following: • Describe set operators • Use a set operator to combine multiple queries into a single query • Control the order of rows returned 7-3 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 4. Set Operators A B A B UNION/UNION ALL A B INTERSECT A B MINUS 7-4 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 5. Tables Used in This Lesson The tables used in this lesson are: • EMPLOYEES: Provides details regarding all current employees • JOB_HISTORY: Records the details of the start date and end date of the former job, and the job identification number and department when an employee switches jobs 7-5 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 6. 7-6 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 7. 7-7 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 8. 7-8 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 9. UNION Operator A B The UNION operator returns results from both queries after eliminating duplications. 7-9 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 10. Using the UNION Operator Display the current and previous job details of all employees. Display each employee only once. SELECT employee_id, job_id FROM employees UNION SELECT employee_id, job_id FROM job_history; … … 7-10 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 11. 7-11 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 12. UNION ALL Operator A B The UNION ALL operator returns results from both queries, including all duplications. 7-12 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 13. Using the UNION ALL Operator Display the current and previous departments of all employees. 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; … … 7-13 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 14. INTERSECT Operator A B The INTERSECT operator returns rows that are common to both queries. 7-14 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 15. Using the INTERSECT Operator Display the employee IDs and job IDs of those employees who currently have a job title that is the same as their job title when they were initially hired (that is, they changed jobs but have now gone back to doing their original job). SELECT employee_id, job_id FROM employees INTERSECT SELECT employee_id, job_id FROM job_history; 7-15 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 16. MINUS Operator A B The MINUS operator returns rows in the first query that are not present in the second query. 7-16 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 17. MINUS Operator Display the employee IDs of those employees who have not changed their jobs even once. SELECT employee_id,job_id FROM employees MINUS SELECT employee_id,job_id FROM job_history; … 7-17 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 18. Set Operator Guidelines • The expressions in the SELECT lists must match in number and data type. • Parentheses can be used to alter the sequence of execution. • The ORDER BY clause: – Can appear only at the very end of the statement – Will accept the column name, aliases from the first SELECT statement, or the positional notation 7-18 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 19. The Oracle Server and Set Operators • Duplicate rows are automatically eliminated except in UNION ALL. • Column names from the first query appear in the result. • The output is sorted in ascending order by default except in UNION ALL. 7-19 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 20. Matching the SELECT Statements Using the UNION operator, display the department ID, location, and hire date for all employees. SELECT department_id, TO_NUMBER(null) location, hire_date FROM employees UNION SELECT department_id, location_id, TO_DATE(null) FROM departments; … 7-20 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 21. Matching the SELECT Statement: Example Using the UNION operator, display the employee ID, job ID, and salary of all employees. SELECT employee_id, job_id,salary FROM employees UNION SELECT employee_id, job_id,0 FROM job_history; … 7-21 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 22. Controlling the Order of Rows Produce an English sentence using two UNION operators. COLUMN a_dummy NOPRINT SELECT 'sing' AS "My dream", 3 a_dummy FROM dual UNION SELECT 'I''d like to teach', 1 a_dummy FROM dual UNION SELECT 'the world to', 2 a_dummy FROM dual ORDER BY a_dummy; 7-22 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 23. 7-23 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 24. Summary In this lesson, you should have learned how to: • Use UNION to return all distinct rows • Use UNION ALL to return all rows, including duplicates • Use INTERSECT to return all rows that are shared by both queries • Use MINUS to return all distinct rows that are selected by the first query but not by the second • Use ORDER BY only at the very end of the statement 7-24 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 25. Practice 7: Overview In this practice, you use the set operators to create reports: • Using the UNION operator • Using the INTERSECTION operator • Using the MINUS operator 7-25 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 26. 7-26 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 27. 7-27 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.