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

Oracle Class - SET Operators

set operators

Uploaded by

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

Oracle Class - SET Operators

set operators

Uploaded by

Peeyush Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

+91 - 7841938548

+91 – 7841038548

SQL CLASS –

TOPICS – SET Operators

SET Operator –

 The SET operators combine the results of two or more queries into one
result. Queries containing SET operators are called compound queries.
 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.
 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.
 Different Types of SET operators are –
1. UNION Operator
2. UNION ALL Operator
3. INTERSECT Operator
4. MINUS Operator
Operator Returns

UNION Combine two or more result sets into a single set, without duplicates.

UNION ALL Combine two or more result sets into a single set, including all duplicates.

INTERSECT Takes the data from both result sets which are in common.

Takes the data from first result set, but not the second (i.e. no matching to
MINUS
each other)

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
1
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

What is the Difference between Join and Set operators?


 Joins and Set Operators can be used to combine data from one or more
tables. The difference lies in how the data is combined.
 In simple terms, joins combine data into new columns. If two tables are
joined together, then the data from the first table is shown in one set of
columns alongside the second table’s column in the same row.
 Unions combine data into new rows. If two tables are union together,
then the data from the first table is in one set of rows, and the data from
the second table in another set. The rows are in the same result.
 Both joins and unions can be used to combine data from one or more
tables into a single result. They both go about this in different ways.
Whereas a join is used to combine columns from different tables, the Set
Operators is used to combine rows.

Example of Joins –

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
2
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

Example of Set operators –

Note – Whenever set operators are used select statement must have
 Equal no of columns.
 Similar data type columns.

The Generic Syntax –

SELECT column_names FROM Table_name1;


UNION| UINON ALL |MINUS| INTERSECT
SELECT column_names FROM Table_name2;

UNION –

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
3
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

 The UNION operator returns all rows selected by either query. Use the
UNION operator to return all rows from multiple tables and eliminate
any duplicate rows.

Guidelines –

 NULL values are not ignored during duplicate checking.


 By default, the output is sorted in ascending order of the first column of
the SELECT clause.
 The IN operator has a higher precedence than the UNION operator.
 Return distinct result from two result sets.
 Output is sorted by default.
 Not desired from a performance aspect because it involved duplicate
removal and sorting operation.

Example –

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
4
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

CREATE TABLE Employee1


(Empid NUMBER,
Ename VARCHAR2(25 ),
Elocation VARCHAR2( 25 ),
Hiredate DATE );

CREATE TABLE Employee2


(Empid NUMBER,
name VARCHAR2(25 ),
loc VARCHAR2( 25 ),
Joindate DATE );

INSERT INTO Employee1 VALUES (1, 'Sam', 'Russia', '14-Nov-2017');


INSERT INTO Employee1 VALUES (2, 'Merry', null, '24-Jan-2018');
INSERT INTO Employee1 VALUES (3, 'John', 'USA', '05-May-2017');
INSERT INTO Employee1 VALUES (4, 'Tom', 'SA', '15-Aug-2016');
INSERT INTO Employee1 VALUES (5, 'Jerry', 'Russia', '09-Sep-2017');
INSERT INTO Employee1 VALUES (6, 'Nick', 'USA', '09-Sep-2019');
INSERT INTO Employee1 VALUES (7, 'Mickey', null, '21-May-2017');
INSERT INTO Employee1 VALUES (8, 'Roy', 'USA', '04-June-2017');
INSERT INTO Employee1 VALUES (9, 'Tim', null, '29-Dec-2018');
INSERT INTO Employee1 VALUES (10, 'Duke', 'Russia', '23-Jun-2019');

INSERT INTO Employee2 VALUES (11, 'Rashu', 'SA', '14-Nov-2017');


INSERT INTO Employee2 VALUES (12, 'Bhim', null, '24-Jan-2018');
INSERT INTO Employee2 VALUES (13, 'Rajesh', 'INDIA', '05-May-2017');
INSERT INTO Employee2 VALUES (14, 'Anjana', 'DENMARK', '15-Aug-2016');
INSERT INTO Employee2 VALUES (15, 'Laxmi', 'USA', '09-Sep-2017');
INSERT INTO Employee2 VALUES (16, 'Sabin', 'UK', '09-Sep-2019');
INSERT INTO Employee2 VALUES (7, 'Mickey', null, '21-May-2017');
INSERT INTO Employee2 VALUES (8, 'Roy', 'USA', '04-June-2017');
INSERT INTO Employee2 VALUES (9, 'Tim', null, '29-Dec-2018');
INSERT INTO Employee2 VALUES (10, 'Duke', 'Russia', '23-Jun-2019');

Select Elocation From Employee1


UNION

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
5
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

Select loc From Employee2;

Select Empid, Elocation From Employee1 Where Empid = 1


UNION
Select Empid, loc From Employee2 Where Empid = 13;

Select Empid, Elocation From Employee1 ORDER BY Empid


UNION
Select Empid, loc From Employee2 ORDER BY Empid;

Select Elocation, Empid, From Employee1


UNION
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Elocation From Employee1


UNION
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Ename From Employee1 Where Empid = 1


UNION
Select name, Empid From Employee2 Where Empid = 12 Order by 2;

UNION ALL –

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
6
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

 Combines the results of two SELECT statement into one result set
including the duplicates.

Guidelines –

 Unlike UNION, duplicate rows are not eliminated and the output is not
sorted by default.

Example –

Select Elocation From Employee1


UNION ALL
Select loc From Employee2;

Select Empid, Elocation From Employee1


UNION ALL
Select Empid, loc From Employee2;

Select Empid, Elocation From Employee1 ORDER BY Empid

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
7
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

UNION ALL
Select Empid, loc From Employee2 ORDER BY Empid;

Select Elocation, Empid From Employee1


UNION ALL
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Elocation From Employee1


UNION ALL
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Ename From Employee1 Where Empid = 1


UNION ALL
Select name, Empid From Employee2 Where Empid = 12 Order by 2;

Select Empid, Ename From Employee1 Where Empid = 1


UNION ALL
Select Empid, Ename From Employee2 Where Empid = 2 Order by 2;

INTERSECT –

 Use the INTERSECT operator to return all rows common to multiple queries.

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
8
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

Guidelines –
 Reversing the order of the intersected tables does not alter the result.
 Intersect does not ignore NULL values.

Example –

Select Elocation From Employee1


INTERSECT
Select loc From Employee2;

Select Empid, Elocation From Employee1


INTERSECT
Select Empid, loc From Employee2;

Select Empid, Elocation From Employee1 ORDER BY Empid


INTERSECT
Select Empid, loc From Employee2 ORDER BY Empid;

Select Elocation, Empid From Employee1


INTERSECT
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Elocation From Employee1


INTERSECT
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Ename From Employee1 Where Empid = 1


INTERSECT
Select name, Empid From Employee2 Where Empid = 12 Order by 2;

MINUS –
 The MINUS operator returns rows from the first query that is not present in
the second query.

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
9
Website - https://minskole.in/ Email - [email protected]
+91 - 7841938548
+91 – 7841038548

Notes –
 The Queries are all executed independently but their output is merged.
 Only final query ends with a Semicolon.

Example –

Select Empid, Elocation From Employee1


MINUS
Select Empid, loc From Employee2;

Select Empid, Elocation From Employee1 ORDER BY Empid


MINUS
Select Empid, loc From Employee2 ORDER BY Empid;

Select Elocation, Empid From Employee1


MINUS
Select Empid, loc From Employee2 ORDER BY Empid;

Select Empid, Elocation From Employee1


MINUS
Select Empid, loc From Employee2 ORDER BY Empid;

__________________________THANK YOU____________________________

2ND FLOOR, KRUSHNA PARK, ABOVE MASA MASA RESTAURANT, KHARADI, PUNE, 411014
10
Website - https://minskole.in/ Email - [email protected]

You might also like