SlideShare a Scribd company logo
8
Copyright © 2007, Oracle. All rights reserved.
Using the Set Operators
Copyright © 2007, Oracle. All rights reserved.
8 - 2
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
Copyright © 2007, Oracle. All rights reserved.
8 - 3
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using the ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 4
Set Operators
UNION/UNION ALL
A B A B
A B
INTERSECT
A B
MINUS
Copyright © 2007, Oracle. All rights reserved.
8 - 5
Set Operator Guidelines
• The expressions in the SELECT lists must match in number.
• The data type of each column in the second query must
match the data type of its corresponding column in the first
query.
• Parentheses can be used to alter the sequence of execution.
• ORDER BY clause can appear only at the very end of the
statement.
Copyright © 2007, Oracle. All rights reserved.
8 - 6
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.
Copyright © 2007, Oracle. All rights reserved.
8 - 7
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using the ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 8
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
Copyright © 2007, Oracle. All rights reserved.
8 - 12
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using the ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 13
UNION Operator
A B
The UNION operator returns rows from both queries after eliminating
duplications.
Copyright © 2007, Oracle. All rights reserved.
8 - 14
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;
…
…
Copyright © 2007, Oracle. All rights reserved.
8 - 16
UNION ALL Operator
The UNION ALL operator returns rows from both queries, including all
duplications.
A B
Copyright © 2007, Oracle. All rights reserved.
8 - 17
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;
…
…
Copyright © 2007, Oracle. All rights reserved.
8 - 18
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 19
INTERSECT Operator
A B
The INTERSECT operator returns rows that are common to both queries.
Copyright © 2007, Oracle. All rights reserved.
8 - 20
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 previous one
(that is, they changed jobs but have now gone back to doing
the same job they did previously).
SELECT employee_id, job_id
FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;
Copyright © 2007, Oracle. All rights reserved.
8 - 21
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using the ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 22
MINUS Operator
A B
The MINUS operator returns all the distinct rows selected by the first
query, but not present in the second query result set.
Copyright © 2007, Oracle. All rights reserved.
8 - 23
Using the MINUS Operator
Display the employee IDs of those employees who have not
changed their jobs even once.
SELECT employee_id
FROM employees
MINUS
SELECT employee_id
FROM job_history;
…
Copyright © 2007, Oracle. All rights reserved.
8 - 24
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Matching the SELECT statements
• Using ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 25
Matching the SELECT Statements
• Using the UNION operator, display the location ID,
department name, and the state where it is located.
• You must match the data type (using the TO_CHAR function
or any other conversion functions) when columns do not
exist in one or the other table.
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;
Copyright © 2007, Oracle. All rights reserved.
8 - 26
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;
…
Copyright © 2007, Oracle. All rights reserved.
8 - 27
Lesson Agenda
• Set Operators: Types and guidelines
• Tables used in this lesson
• UNION and UNION ALL operator
• INTERSECT operator
• MINUS operator
• Match the SELECT statements
• Using the ORDER BY clause in set operations
Copyright © 2007, Oracle. All rights reserved.
8 - 28
Using the ORDER BY Clause in Set Operations
• The ORDER BY clause can appear only once at the end of the
compound query.
• Component queries cannot have individual ORDER BY
clauses.
• ORDER BY clause recognizes only the columns of the first
SELECT query.
• By default, the first column of the first SELECT query is used
to sort the output in an ascending order.
Copyright © 2007, Oracle. All rights reserved.
8 - 29
Summary
In this lesson, you should have learned how to use:
• UNION to return all distinct rows
• UNION ALL to return all rows, including duplicates
• INTERSECT to return all rows that are shared by both
queries
• MINUS to return all distinct rows that are selected by the first
query, but not by the second
• ORDER BY only at the very end of the statement
Copyright © 2007, Oracle. All rights reserved.
8 - 30
Practice 8: Overview
In this practice, you create reports by using:
• The UNION operator
• The INTERSECTION operator
• The MINUS operator

More Related Content

PPT
PPT
Les07 (using the set operators)
PPT
plsql Les07
PPT
PPT
Using the set operators
PPT
e computer notes - Using set operator
PDF
Lesson07
Les07 (using the set operators)
plsql Les07
Using the set operators
e computer notes - Using set operator
Lesson07

Similar to Les08 set operators by Szabist for the MS and MPM (20)

PPTX
OPerators.pptx Best topics dbms. Good one
PPTX
MYSQL using set operators
PPTX
Computer Science:Sql Set Operation
PPTX
Oracle: Joins
PPTX
Oracle: Joins
PPT
Ch7
PPTX
Subqueriesandjoins unit6
PPTX
Sql server ___________ (advance sql)
PDF
Database management system lecture note.
PDF
DBMS Nested & Sub Queries Set operations
PPTX
MergeResult_2024_02_09_08_59_11.pptx
PPT
Advanced Sql Training
PPT
PPT
PPT
IBM Informix Database SQL Set operators and ANSI Hash Join
PPTX
set operators.pptx
PPTX
Day-2 SQL Theory_V1.pptx
PDF
Sql1 vol2
PPTX
Relational Algebra in DBMS power ppoint pesenetation
PPTX
Sql intro
OPerators.pptx Best topics dbms. Good one
MYSQL using set operators
Computer Science:Sql Set Operation
Oracle: Joins
Oracle: Joins
Ch7
Subqueriesandjoins unit6
Sql server ___________ (advance sql)
Database management system lecture note.
DBMS Nested & Sub Queries Set operations
MergeResult_2024_02_09_08_59_11.pptx
Advanced Sql Training
IBM Informix Database SQL Set operators and ANSI Hash Join
set operators.pptx
Day-2 SQL Theory_V1.pptx
Sql1 vol2
Relational Algebra in DBMS power ppoint pesenetation
Sql intro
Ad

Recently uploaded (20)

PDF
Introduction to Data Science and Data Analysis
PDF
How to run a consulting project- client discovery
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PDF
Global Data and Analytics Market Outlook Report
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PPTX
Introduction to Inferential Statistics.pptx
PPT
Predictive modeling basics in data cleaning process
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
PPTX
Leprosy and NLEP programme community medicine
PPTX
A Complete Guide to Streamlining Business Processes
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
Managing Community Partner Relationships
PDF
Microsoft Core Cloud Services powerpoint
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PDF
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
PDF
annual-report-2024-2025 original latest.
Introduction to Data Science and Data Analysis
How to run a consulting project- client discovery
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
Global Data and Analytics Market Outlook Report
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
Introduction to Inferential Statistics.pptx
Predictive modeling basics in data cleaning process
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
Leprosy and NLEP programme community medicine
A Complete Guide to Streamlining Business Processes
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
Managing Community Partner Relationships
Microsoft Core Cloud Services powerpoint
Acceptance and paychological effects of mandatory extra coach I classes.pptx
[EN] Industrial Machine Downtime Prediction
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
annual-report-2024-2025 original latest.
Ad

Les08 set operators by Szabist for the MS and MPM

  • 1. 8 Copyright © 2007, Oracle. All rights reserved. Using the Set Operators
  • 2. Copyright © 2007, Oracle. All rights reserved. 8 - 2 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
  • 3. Copyright © 2007, Oracle. All rights reserved. 8 - 3 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using the ORDER BY clause in set operations
  • 4. Copyright © 2007, Oracle. All rights reserved. 8 - 4 Set Operators UNION/UNION ALL A B A B A B INTERSECT A B MINUS
  • 5. Copyright © 2007, Oracle. All rights reserved. 8 - 5 Set Operator Guidelines • The expressions in the SELECT lists must match in number. • The data type of each column in the second query must match the data type of its corresponding column in the first query. • Parentheses can be used to alter the sequence of execution. • ORDER BY clause can appear only at the very end of the statement.
  • 6. Copyright © 2007, Oracle. All rights reserved. 8 - 6 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. Copyright © 2007, Oracle. All rights reserved. 8 - 7 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using the ORDER BY clause in set operations
  • 8. Copyright © 2007, Oracle. All rights reserved. 8 - 8 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
  • 9. Copyright © 2007, Oracle. All rights reserved. 8 - 12 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using the ORDER BY clause in set operations
  • 10. Copyright © 2007, Oracle. All rights reserved. 8 - 13 UNION Operator A B The UNION operator returns rows from both queries after eliminating duplications.
  • 11. Copyright © 2007, Oracle. All rights reserved. 8 - 14 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; … …
  • 12. Copyright © 2007, Oracle. All rights reserved. 8 - 16 UNION ALL Operator The UNION ALL operator returns rows from both queries, including all duplications. A B
  • 13. Copyright © 2007, Oracle. All rights reserved. 8 - 17 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; … …
  • 14. Copyright © 2007, Oracle. All rights reserved. 8 - 18 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using ORDER BY clause in set operations
  • 15. Copyright © 2007, Oracle. All rights reserved. 8 - 19 INTERSECT Operator A B The INTERSECT operator returns rows that are common to both queries.
  • 16. Copyright © 2007, Oracle. All rights reserved. 8 - 20 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 previous one (that is, they changed jobs but have now gone back to doing the same job they did previously). SELECT employee_id, job_id FROM employees INTERSECT SELECT employee_id, job_id FROM job_history;
  • 17. Copyright © 2007, Oracle. All rights reserved. 8 - 21 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using the ORDER BY clause in set operations
  • 18. Copyright © 2007, Oracle. All rights reserved. 8 - 22 MINUS Operator A B The MINUS operator returns all the distinct rows selected by the first query, but not present in the second query result set.
  • 19. Copyright © 2007, Oracle. All rights reserved. 8 - 23 Using the MINUS Operator Display the employee IDs of those employees who have not changed their jobs even once. SELECT employee_id FROM employees MINUS SELECT employee_id FROM job_history; …
  • 20. Copyright © 2007, Oracle. All rights reserved. 8 - 24 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Matching the SELECT statements • Using ORDER BY clause in set operations
  • 21. Copyright © 2007, Oracle. All rights reserved. 8 - 25 Matching the SELECT Statements • Using the UNION operator, display the location ID, department name, and the state where it is located. • You must match the data type (using the TO_CHAR function or any other conversion functions) when columns do not exist in one or the other table. 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;
  • 22. Copyright © 2007, Oracle. All rights reserved. 8 - 26 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; …
  • 23. Copyright © 2007, Oracle. All rights reserved. 8 - 27 Lesson Agenda • Set Operators: Types and guidelines • Tables used in this lesson • UNION and UNION ALL operator • INTERSECT operator • MINUS operator • Match the SELECT statements • Using the ORDER BY clause in set operations
  • 24. Copyright © 2007, Oracle. All rights reserved. 8 - 28 Using the ORDER BY Clause in Set Operations • The ORDER BY clause can appear only once at the end of the compound query. • Component queries cannot have individual ORDER BY clauses. • ORDER BY clause recognizes only the columns of the first SELECT query. • By default, the first column of the first SELECT query is used to sort the output in an ascending order.
  • 25. Copyright © 2007, Oracle. All rights reserved. 8 - 29 Summary In this lesson, you should have learned how to use: • UNION to return all distinct rows • UNION ALL to return all rows, including duplicates • INTERSECT to return all rows that are shared by both queries • MINUS to return all distinct rows that are selected by the first query, but not by the second • ORDER BY only at the very end of the statement
  • 26. Copyright © 2007, Oracle. All rights reserved. 8 - 30 Practice 8: Overview In this practice, you create reports by using: • The UNION operator • The INTERSECTION operator • The MINUS operator

Editor's Notes

  • #2: Objectives In this lesson, you learn how to write queries by using set operators.
  • #4: Set Operators Set operators combine the results of two or more component queries into one result. Queries containing set operators are called compound queries. All set operators have equal precedence. If a SQL statement contains multiple set operators, the Oracle server evaluates them from left (top) to right (bottom)—if no parentheses explicitly specify another order. You should use parentheses to specify the order of evaluation explicitly in queries that use the INTERSECT operator with other set operators.
  • #5: Set Operator Guidelines The expressions in the SELECT lists of the queries must match in number and data type. Queries that use UNION, UNION ALL, INTERSECT, and MINUS operators in their WHERE clause must have the same number and data type of columns in their SELECT list. The data type of the columns in SELECT list of the queries in the compound query may not be exactly the same. The column in second query must be in the same data type group (such as numeric or character) as the corresponding column in the first query. Set operators can be used in subqueries. You should use parentheses to specify the order of evaluation in queries that use the INTERSECT operator with other set operators. This ensures compliance with emerging SQL standards that will give the INTERSECT operator greater precedence than the other set operators.
  • #6: The Oracle Server and Set Operators When a query uses set operators, the Oracle server eliminates duplicate rows automatically except in the case of the UNION ALL operator. The column names in the output are decided by the column list in the first SELECT statement. By default, the output is sorted in ascending order of the first column of the SELECT clause. The corresponding expressions in the SELECT lists of the component queries of a compound query must match in number and data type. If component queries select character data, the data type of the return values is determined as follows: If both queries select values of CHAR data type, of equal length, then the returned values have the CHAR data type of that length. If the queries select values of CHAR with different lengths, then the returned value is VARCHAR2 with the length of the larger CHAR value. If either or both of the queries select values of VARCHAR2 data type, then the returned values have the VARCHAR2 data type. If component queries select numeric data, then the data type of the return values is determined by numeric precedence. If all queries select values of the NUMBER type, then the returned values have the NUMBER data type. In queries using set operators, the Oracle server does not perform implicit conversion across data type groups. Therefore, if the corresponding expressions of component queries resolve to both character data and numeric data, the Oracle server returns an error.
  • #8: Tables Used in This Lesson Two tables are used in this lesson. They are the EMPLOYEES table and the JOB_HISTORY table. You are already familiar with the EMPLOYEES table that stores employee details such as a unique identification number, email address, job identification (such as ST_CLERK, SA_REP, and so on), salary, manager and so on. Some of the employees have been with the company for a long time and have switched to different jobs. This is monitored using the JOB_HISTORY table. When an employee switches jobs, the details of the start date and end date of the former job, the job_id (such as ST_CLERK, SA_REP, and so on), and the department are recorded in the JOB_HISTORY table. The structure and data from the EMPLOYEES and JOB_HISTORY tables are shown on the following pages.
  • #9: Tables Used in This Lesson (continued) There have been instances in the company, of people who have held the same position more than once during their tenure with the company. For example, consider the employee Taylor, who joined the company on 24-MAR-1998. Taylor held the job title SA_REP for the period 24-MAR-98 to 31-DEC-98 and the job title SA_MAN for the period 01-JAN-99 to 31-DEC-99. Taylor moved back into the job title of SA_REP, which is his current job title. DESCRIBE employees
  • #10: Tables Used in This Lesson (continued) SELECT employee_id, last_name, job_id, hire_date, department_id FROM employees; DESCRIBE job_history
  • #11: Tables Used in This Lesson (continued) SELECT * FROM job_history;
  • #13: UNION Operator The UNION operator returns all rows that are selected by either query. Use the UNION operator to return all rows from multiple tables and eliminate any duplicate rows. Guidelines The number of columns being selected must be the same. The data types of the columns being selected must be in the same data type group (such as numeric or character). The names of the columns need not be identical. UNION operates over all of the columns being selected. NULL values are not ignored during duplicate checking. By default, the output is sorted in ascending order of the columns of the SELECT clause.
  • #14: Using the UNION Operator The UNION operator eliminates any duplicate records. If records that occur in both the EMPLOYEES and the JOB_HISTORY tables are identical, the records are displayed only once. Observe in the output shown in the slide that the record for the employee with the EMPLOYEE_ID 200 appears twice because the JOB_ID is different in each row. Consider the following example: SELECT employee_id, job_id, department_id FROM employees UNION SELECT employee_id, job_id, department_id FROM job_history;
  • #15: Using the UNION Operator (continued) In the preceding output, employee 200 appears three times. Why? Note the DEPARTMENT_ID values for employee 200. One row has a DEPARTMENT_ID of 90, another 10, and the third 90. Because of these unique combinations of job IDs and department IDs, each row for employee 200 is unique and therefore not considered to be a duplicate. Observe that the output is sorted in ascending order of the first column of the SELECT clause (in this case, EMPLOYEE_ID).
  • #16: UNION ALL Operator Use the UNION ALL operator to return all rows from multiple queries. Guidelines The guidelines for UNION and UNION ALL are the same, with the following two exceptions that pertain to UNION ALL: Unlike UNION, duplicate rows are not eliminated and the output is not sorted by default.
  • #17: Using the UNION ALL Operator In the example, 30 rows are selected. The combination of the two tables totals to 30 rows. The UNION ALL operator does not eliminate duplicate rows. UNION returns all distinct rows selected by either query. UNION ALL returns all rows selected by either query, including all duplicates. Consider the query in the slide, now written with the UNION clause: SELECT employee_id, job_id,department_id FROM employees UNION SELECT employee_id, job_id,department_id FROM job_history ORDER BY employee_id; The preceding query returns 29 rows. This is because it eliminates the following row (because it is a duplicate):
  • #19: INTERSECT Operator Use the INTERSECT operator to return all rows that are common to multiple queries. Guidelines The number of columns and the data types of the columns being selected by the SELECT statements in the queries must be identical in all the SELECT statements used in the query. The names of the columns, however, need not be identical. Reversing the order of the intersected tables does not alter the result. INTERSECT does not ignore NULL values.
  • #20: Using the INTERSECT Operator In the example in this slide, the query returns only those records that have the same values in the selected columns in both tables. What will be the results if you add the DEPARTMENT_ID column to the SELECT statement from the EMPLOYEES table and add the DEPARTMENT_ID column to the SELECT statement from the JOB_HISTORY table, and run this query? The results may be different because of the introduction of another column whose values may or may not be duplicates. Example: SELECT employee_id, job_id, department_id FROM employees INTERSECT SELECT employee_id, job_id, department_id FROM job_history; Employee 200 is no longer part of the results because the EMPLOYEES.DEPARTMENT_ID value is different from the JOB_HISTORY.DEPARTMENT_ID value.
  • #22: MINUS Operator Use the MINUS operator to return all distinct rows selected by the first query, but not present in the second query result set (the first SELECT statement MINUS the second SELECT statement). Note: The number of columns must be the same and the data types of the columns being selected by the SELECT statements in the queries must belong to the same data type group in all the SELECT statements used in the query. The names of the columns, however, need not be identical.
  • #23: Using the MINUS Operator In the example in the slide, the employee IDs in the JOB_HISTORY table are subtracted from those in the EMPLOYEES table. The results set displays the employees remaining after the subtraction; they are represented by rows that exist in the EMPLOYEES table, but do not exist in the JOB_HISTORY table. These are the records of the employees who have not changed their jobs even once.
  • #25: Matching the SELECT Statements Because the expressions in the SELECT lists of the queries must match in number, you can use the dummy columns and the data type conversion functions to comply with this rule. In the slide, the name, Warehouse location, is given as the dummy column heading. The TO_CHAR function is used in the first query to match the VARCHAR2 data type of the state_province column that is retrieved by the second query. Similarly, the TO_CHAR function in the second query is used to match the VARCHAR2 data type of the department_name column that is retrieved by the first query. The output of the query is shown:
  • #26: Matching the SELECT Statement: Example The EMPLOYEES and JOB_HISTORY tables have several columns in common (for example, EMPLOYEE_ID, JOB_ID, and DEPARTMENT_ID). But what if you want the query to display the employee ID, job ID, and salary using the UNION operator, knowing that the salary exists only in the EMPLOYEES table? The code example in the slide matches the EMPLOYEE_ID and JOB_ID columns in the EMPLOYEES and JOB_HISTORY tables. A literal value of 0 is added to the JOB_HISTORY SELECT statement to match the numeric SALARY column in the EMPLOYEES SELECT statement. In the results shown in the slide, each row in the output that corresponds to a record from the JOB_HISTORY table contains a 0 in the SALARY column.
  • #28: Using the ORDER BY Clause in Set Operations The ORDER BY clause can be used only once in a compound query. If used, the ORDER BY clause must be placed at the end of the query. The ORDER BY clause accepts the column name or an alias. By default, the output is sorted in ascending order in the first column of the first SELECT query. Note: The ORDER BY clause does not recognize the column names of the second SELECT query. To avoid confusion over column names, it is a common practice to ORDER BY column positions. For example, in the following statement, the output will be shown in ascending order of the job_id. SELECT employee_id, job_id,salary FROM employees UNION SELECT employee_id, job_id,0 FROM job_history ORDER BY 2; If you omit the ORDER BY, then by default the output will be sorted in the ascending order of employee_id. You cannot use the columns from the second query to sort the output.
  • #29: Summary The UNION operator returns all the distinct rows selected by each query in the compound query. Use the UNION operator to return all rows from multiple tables and eliminate any duplicate rows. Use the UNION ALL operator to return all rows from multiple queries. Unlike the case with the UNION operator, duplicate rows are not eliminated and the output is not sorted by default. Use the INTERSECT operator to return all rows that are common to multiple queries. Use the MINUS operator to return rows returned by the first query that are not present in the second query. Remember to use the ORDER BY clause only at the very end of the compound statement. Make sure that the corresponding expressions in the SELECT lists match in number and data type.
  • #30: Practice 8: Overview In this practice, you write queries using the set operators.
  • #31: Practice 8 1. The HR department needs a list of department IDs for departments that do not contain the job ID ST_CLERK. Use the set operators to create this report. 2. The HR department needs a list of countries that have no departments located in them. Display the country ID and the name of the countries. Use the set operators to create this report. 3. Produce a list of jobs for departments 10, 50, and 20, in that order. Display the job ID and department ID by using the set operators. Create a report that lists 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 by the company (that is, they changed jobs but have now gone back to doing their original job).
  • #32: Practice 8 (continued) 5. The HR department needs a report with the following specifications: Last name and department ID of all employees from the EMPLOYEES table, regardless of whether or not they belong to a department Department ID and department name of all departments from the DEPARTMENTS table, regardless of whether or not they have employees working in them Write a compound query to accomplish this.