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

Oracle_Union

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Oracle_Union

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Set Operators in Oracle : UNION , UNION ALL,

INTERSECT , MINUS

Union : The UNION operator is a set operator that combines result


sets of two or more SELECT statements into a single result set.

FIELDS order SHOULD BE SAME

DATA TYPE SHOULD BE SAME

LENGTH SHOULD BE SAME.

Oracle UNION illustration

Suppose, we have two tables T1 and T2:

 T1 has three rows 1, 2 and 3


 T2 also has three rows 2, 3 and 4

The following picture illustrates the UNION of T1 and T2 tables:

The UNION removed the duplicate rows 2 and 3

Union All :

The following picture illustrates the result of the UNION ALL of


the T1 and T2 tables:
As you can see, the UNION ALL retains the duplicate rows 2 and 3.
Oracle UNION Example :
SELECT first_name, last_name,email,'contact' FROM contacts
UNION
SELECT first_name, last_name,email,'employee' FROM employees;

SELECT first_name || ' ' || last_name name,email, 'contact' FROM


contacts
UNION
SELECT first_name || ' ' || last_name name, email,'employee' FROM
employees
ORDER BY name DESC;

Oracle UNION ALL example

UNION :

SELECT last_name FROM employees


UNION
SELECT last_name FROM contacts
ORDER BY last_name; -- 357 unique records

SELECT last_name FROM employees


UNION ALL
SELECT last_name FROM contacts
ORDER BY last_name; -- 426 records WITH DUPLICATES
INTERSECT : The Oracle INTERSECT operator compares the result
of two queries and returns the distinct rows that are output by both
queries.

The following picture illustrates the intersection of T1 and T2:

Example :
SELECT last_name FROM employees - 10
INTERSECT
SELECT last_name FROM contacts -10
ORDER BY last_name; --29 RECORDS

1 field
2 records

Emp -10

Dept -10

Select a.empid,a.ename,a.deptno, b.deptno, b.deptid,b.dname


from emp a dept b where a.deptno=b.deptno

6 fields

10 records
MINUS :

MINUS operator is used to return all rows in the first SELECT statement
that are not in the second SELECT statement.

The MINUS operator is used to subtract the result set obtained by first
SELECT query from the result set obtained by second SELECT query.

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

The following picture illustrates the result of the MINUS of T1 and T2:

Here are the last names returned by the first query but are
not found in the result set of the second query:
Example :
SELECT last_name FROM employees
minus
SELECT last_name FROM contacts
ORDER BY last_name;

The following statement returns a list of product id from


the products table, but do not exist in the inventories table:

SELECT product_id FROM products


MINUS
SELECT product_id FROM inventories;

You might also like