SlideShare a Scribd company logo
2
Most read
6
Most read
16
Most read
Using the Set Operators
Set Operators
UNION/UNION ALL
A B A B
A B
INTERSECT
A B
MINUS
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
UNION Operator
A B
The UNION operator returns results from both
queries after eliminating duplications.
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;
…
…
UNION ALL Operator
A B
The UNION ALL operator returns results from both
queries, including all duplications.
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;
…
…
INTERSECT Operator
A B
The INTERSECT operator returns rows that are
common to both queries.
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;
MINUS Operator
A B
The MINUS operator returns rows in the first query
that are not present in the second query.
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;
…
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
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.
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;
…
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;
…
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;

More Related Content

PPTX
Computer Science:Sql Set Operation
PPTX
set operators.pptx
PPTX
Sql joins inner join self join outer joins
PPTX
SQL JOIN
PPT
Advanced Sql Training
PPTX
STRUCTURE OF SQL QUERIES
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
PPT
Aggregate functions
Computer Science:Sql Set Operation
set operators.pptx
Sql joins inner join self join outer joins
SQL JOIN
Advanced Sql Training
STRUCTURE OF SQL QUERIES
Types Of Join In Sql Server - Join With Example In Sql Server
Aggregate functions

What's hot (20)

PPT
Introduction to structured query language (sql)
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPTX
SQL - DML and DDL Commands
PPTX
Aggregate function
PPT
SQL subquery
PDF
PL/SQL TRIGGERS
PPTX
SQL Queries Information
PPTX
Sql subquery
PPTX
Dbms anomalies
PPTX
SQL commands
PDF
Data manipulation language
PPTX
SQL Functions
PPT
SQL Views
PDF
Basic Sql Handouts
PDF
SQL Joins With Examples | Edureka
PPTX
Relational model
PPTX
DDL And DML
PPTX
SQL(DDL & DML)
Introduction to structured query language (sql)
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
SQL - DML and DDL Commands
Aggregate function
SQL subquery
PL/SQL TRIGGERS
SQL Queries Information
Sql subquery
Dbms anomalies
SQL commands
Data manipulation language
SQL Functions
SQL Views
Basic Sql Handouts
SQL Joins With Examples | Edureka
Relational model
DDL And DML
SQL(DDL & DML)
Ad

Similar to Using the set operators (20)

PPT
PPT
Les07 (using the set operators)
PPT
PPT
e computer notes - Using set operator
PPT
plsql Les07
PPT
Les08 set operators by Szabist for the MS and MPM
PDF
Lesson07
PPTX
OPerators.pptx Best topics dbms. Good one
PPTX
MYSQL using set operators
PPT
Chinabankppt
PPT
IBM Informix Database SQL Set operators and ANSI Hash Join
PDF
Database management system lecture note.
PPTX
Day-2 SQL Theory_V1.pptx
PPT
Ch7
PPT
Introduction to-sql
PPTX
Oracle: Joins
PPTX
Oracle: Joins
PPT
Day1_SQL_1 for learning about sql cldarly.ppt
PPT
ALL ABOUT SQL AND RDBMS
Les07 (using the set operators)
e computer notes - Using set operator
plsql Les07
Les08 set operators by Szabist for the MS and MPM
Lesson07
OPerators.pptx Best topics dbms. Good one
MYSQL using set operators
Chinabankppt
IBM Informix Database SQL Set operators and ANSI Hash Join
Database management system lecture note.
Day-2 SQL Theory_V1.pptx
Ch7
Introduction to-sql
Oracle: Joins
Oracle: Joins
Day1_SQL_1 for learning about sql cldarly.ppt
ALL ABOUT SQL AND RDBMS
Ad

More from Syed Zaid Irshad (20)

PDF
Data Structures & Algorithms - Spring 2025.pdf
PDF
Operating System.pdf
PDF
DBMS_Lab_Manual_&_Solution
PPTX
Data Structure and Algorithms.pptx
PPTX
Design and Analysis of Algorithms.pptx
PPTX
Professional Issues in Computing
PDF
Reduce course notes class xi
PDF
Reduce course notes class xii
PDF
Introduction to Database
PDF
C Language
PDF
Flowchart
PDF
Algorithm Pseudo
PDF
Computer Programming
PDF
ICS 2nd Year Book Introduction
PDF
Security, Copyright and the Law
PDF
Computer Architecture
PDF
Data Communication
PDF
Information Networks
PDF
Basic Concept of Information Technology
PDF
Introduction to ICS 1st Year Book
Data Structures & Algorithms - Spring 2025.pdf
Operating System.pdf
DBMS_Lab_Manual_&_Solution
Data Structure and Algorithms.pptx
Design and Analysis of Algorithms.pptx
Professional Issues in Computing
Reduce course notes class xi
Reduce course notes class xii
Introduction to Database
C Language
Flowchart
Algorithm Pseudo
Computer Programming
ICS 2nd Year Book Introduction
Security, Copyright and the Law
Computer Architecture
Data Communication
Information Networks
Basic Concept of Information Technology
Introduction to ICS 1st Year Book

Recently uploaded (20)

PPTX
web development for engineering and engineering
PPTX
Geodesy 1.pptx...............................................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Project quality management in manufacturing
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Welding lecture in detail for understanding
web development for engineering and engineering
Geodesy 1.pptx...............................................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Project quality management in manufacturing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
additive manufacturing of ss316l using mig welding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Digital Logic Computer Design lecture notes
CYBER-CRIMES AND SECURITY A guide to understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Construction Project Organization Group 2.pptx
Lecture Notes Electrical Wiring System Components
Lesson 3_Tessellation.pptx finite Mathematics
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Welding lecture in detail for understanding

Using the set operators

  • 1. Using the Set Operators
  • 2. Set Operators UNION/UNION ALL A B A B A B INTERSECT A B MINUS
  • 3. 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
  • 4. UNION Operator A B The UNION operator returns results from both queries after eliminating duplications.
  • 5. 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; … …
  • 6. UNION ALL Operator A B The UNION ALL operator returns results from both queries, including all duplications.
  • 7. 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; … …
  • 8. INTERSECT Operator A B The INTERSECT operator returns rows that are common to both queries.
  • 9. 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;
  • 10. MINUS Operator A B The MINUS operator returns rows in the first query that are not present in the second query.
  • 11. 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; …
  • 12. 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
  • 13. 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.
  • 14. 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; …
  • 15. 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; …
  • 16. 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;

Editor's Notes

  • #3: Set Operators The 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.
  • #4: Tables Used in This Lesson Two tables are used in this lesson. They are the EMPLOYEES table and the JOB_HISTORY table. The EMPLOYEES table stores the employee details. For the human resource records, this table stores a unique identification number and e-mail address for each employee. The details of the employee’s job identification number, salary, and manager are also stored. Some of the employees earn a commission in addition to their salary; this information is tracked, too. The company organizes the roles of employees into jobs. 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 identification number, 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.
  • #5: 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. Similarly, consider the employee Whalen, who joined the company on 17-SEP-1987. Whalen held the job title AD_ASST for the period 17-SEP-87 to 17-JUN-93 and the job title AC_ACCOUNT for the period 01-JUL-94 to 31-DEC-98. Whalen moved back into the job title of AD_ASST, which is his current job title. DESCRIBE employees
  • #6: Tables Used in This Lesson (continued) SELECT employee_id, last_name, job_id, hire_date, department_id FROM employees; DESCRIBE job_history
  • #7: Tables Used in This Lesson (continued) SELECT * FROM job_history;
  • #8: 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 and the data types of the columns being selected must be identical in all the SELECT statements used in the query. 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. The IN operator has a higher precedence than the UNION operator. By default, the output is sorted in ascending order of the first column of the SELECT clause.
  • #9: 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 on 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;
  • #10: Using the UNION Operator (continued) In the preceding output, employee 200 appears three times. Why? Notice 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).
  • #11: 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. The DISTINCT keyword cannot be used.
  • #12: UNION ALL Operator (continued) 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 on 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 (as it is a duplicate):
  • #13: 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 need not be identical. Reversing the order of the intersected tables does not alter the result. INTERSECT does not ignore NULL values.
  • #14: INTERSECT Operator (continued) In the example in this slide, the query returns only the 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.
  • #15: MINUS Operator Use the MINUS operator to return rows returned by the first query that are not present in the second query (the first SELECT statement MINUS the second SELECT statement). 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 need not be identical. All of the columns in the WHERE clause must be in the SELECT clause for the MINUS operator to work.
  • #16: MINUS Operator (continued) In the example in the slide, the employee IDs and job 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.
  • #17: 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 type of columns in their SELECT list. For example: SELECT employee_id, department_id FROM employees WHERE (employee_id, department_id) IN (SELECT employee_id, department_id FROM employees UNION SELECT employee_id, department_id FROM job_history); The ORDER BY clause: Can appear only at the very end of the statement Will accept the column name, an alias, or the positional notation The column name or alias, if used in an ORDER BY clause, must be from the first SELECT list. Set operators can be used in subqueries.
  • #18: 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 data type CHAR, the returned values have data type CHAR. If either or both of the queries select values of data type VARCHAR2, the returned values have data type VARCHAR2.
  • #19: Matching the SELECT Statements Because the expressions in the select lists of the queries must match in number, you can use dummy columns and the data type conversion functions to comply with this rule. In the slide, the name location is given as the dummy column heading. The TO_NUMBER function is used in the first query to match the NUMBER data type of the LOCATION_ID column retrieved by the second query. Similarly, the TO_DATE function in the second query is used to match the DATE data type of the HIRE_DATE column retrieved by the first query.
  • #20: 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 preceding results, each row in the output that corresponds to a record from the JOB_HISTORY table contains a 0 in the SALARY column.
  • #21: Controlling the Order of Rows By default, the output is sorted in ascending order on the first column. You can use the ORDER BY clause to change this. 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. Without the ORDER BY clause, the code example in the slide produces the following output in the alphabetical order of the first column: Note: Consider a compound query where the UNION set operator is used more than once. In this case, the ORDER BY clause can use only positions rather than explicit expressions. The iSQL*Plus COLUMN Command You can use the iSQL*Plus COLUMN command to customize column headings.