0% found this document useful (0 votes)
43 views

Set Operators: Union/Union All

The document discusses SQL set operators such as UNION, INTERSECT, and MINUS. It provides examples of how to use these operators to combine result sets from multiple queries and describes guidelines for using set operators such as columns needing to match in number and data type. It also notes that duplicate rows are eliminated in UNION by default but not in UNION ALL and that the output is sorted by default except in UNION ALL.

Uploaded by

Dan Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Set Operators: Union/Union All

The document discusses SQL set operators such as UNION, INTERSECT, and MINUS. It provides examples of how to use these operators to combine result sets from multiple queries and describes guidelines for using set operators such as columns needing to match in number and data type. It also notes that duplicate rows are eliminated in UNION by default but not in UNION ALL and that the output is sorted by default except in UNION ALL.

Uploaded by

Dan Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

A B

Set Operators
A B

UNION/UNION ALL

A B

INTERSECT

A B

MINUS
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.
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.
Sample Queries
SELECT employee_id, job_id, department_id
FROM hr.employees
UNION
SELECT employee_id, job_id, department_id
FROM hr.job_history

 Result wil be sorted in ascending order for above query.

SELECT employee_id, job_id, department_id


FROM hr.employees
UNION ALL
SELECT employee_id, job_id, department_id
FROM hr.job_history
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;
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;
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.

You might also like