SET Operators in Oracle With Examples
SET Operators in Oracle With Examples
QLsetoperatorsallowcombiningresultsfromtwoormoreSELECTstatements.Atfirstsight,thislookssimilarto
S
SQL joins although thereisabigdifference.SQLjoinstendstocombinecolumnsi.e.witheachadditionallyjoined
table it is possible to select more and more columns. SQL set operators on the other hand combine rows from
different querieswithstrongpreconditions–allinvolvedSELECTSmust.Joinswearecollectingthedatafromtwo
tables when there is common data. But in set operators the data is not joined, in this, the data is merged
1. Retrieve the same number of columns and
2. ThedatatypesofcorrespondingcolumnsineachinvolvedSELECTmustbecompatible(eitherthesameor
with possibility implicitly convert to the data types of the first SELECT statement).
ou can combine multiple queries using the set operators UNION, UNIONALL,INTERSECT,andMINUS.Allset
Y
operators have equal precedence. If a SQL statement contains multiple set operators, then Oracle Database
evaluates them from the left to right unless parentheses explicitly specify another order.
The corresponding expressions in the select lists of the component queries of a compound query mustmatchin
number and must be in the same datatype group.
If component queries select character data, then the data type of the return values are determined as follows:
1. If both queries select values of datatype CHAR of equal length, then the returned valueshavedatatype
CHARofthatlength.IfthequeriesselectvaluesofCHARwithdifferentlengths,thenthereturnedvalueis
VARCHAR2 with the length of the larger CHAR value.
2. IfeitherorbothofthequeriesselectvaluesofdatatypeVARCHAR2,thenthereturnedvalueshavedatatype
VARCHAR2.
Inqueriesusingsetoperators,Oracledoesnotperformimplicitconversionacrossdatatypegroups.Therefore,ifthe
correspondingexpressionsofcomponentqueriesresolvetobothcharacterdataandnumericdata,Oraclereturnsan
error.
Please use the below SQL Script to create the EmployeeUK and EmployeeUSA tables with the required data.
REATETABLEEmployeeUK
C
(
EmployeeIdINT,
FirstNameVARCHAR(20),
LastNameVARCHAR(20),
GenderVARCHAR(1 0),
DepartmentVARCHAR(2 0)
);
INSERTINTOEmployeeUKVALUES(1,'Pranaya','Rout','Male','IT');
INSERTINTOEmployeeUKVALUES(2,'Priyanka','Dewangan','Female','IT');
INSERTINTOEmployeeUKVALUES(3,'Preety','Tiwary','Female','HR');
INSERTINTOEmployeeUKVALUES(4,'Subrat','Sahoo','Male','HR');
INSERTINTOEmployeeUKVALUES(5,'Anurag','Mohanty','Male','IT');
INSERTINTOEmployeeUKVALUES(6,'Rajesh','Pradhan','Male','HR');
INSERTINTOEmployeeUKVALUES(7,'Hina','Sharma','Female','IT');
CREATETABLEEmployeeUSA
(
EmployeeIdINT,
FirstNameVARCHAR(20),
LastNameVARCHAR(20),
GenderVARCHAR(1 0),
DepartmentVARCHAR(2 0)
);
INSERTINTOEmployeeUSAVALUES(1 ,'James','Pattrick','Male','IT');
INSERTINTOEmployeeUSAVALUES(2 ,'Priyanka','Dewangan','Female','IT');
INSERTINTOEmployeeUSAVALUES(3 ,'Sara','Taylor','Female','HR');
INSERTINTOEmployeeUSAVALUES(4 ,'Subrat','Sahoo','Male','HR');
INSERTINTOEmployeeUSAVALUES(5 ,'Sushanta','Jena','Male','HR');
INSERTINTOEmployeeUSAVALUES(6 ,'Mahesh','Sindhey','Female','HR');
INSERTINTOEmployeeUSAVALUES(7
,'Hina','Sharma','Female','IT');
he above statement combines the results of two queries with the UNION operator, which eliminates duplicate
T
selectedrows.Onceyouexecutetheabovequery,youwillgetthefollowingresultset.Pleaseobserve,herewedon’t
haveanyduplicatedata.Here,intheresultset,wegotatotalof11rowsoutof14rows.Thisisbecause3rowsare
present in both the result set.
nceyouexecutetheaboveUNIONALLquery,youwillgetthefollowingresultset.Pleaseobserve,herewegotall
O
the 14 rows in the result set.
owwhenyouexecutetheabovequeryandyouwillgetthefollowingoutput.Hereyoucanseetheemployeesare
N
sorted according to their FirstName column values.
heabovestatementcombinestheresultswiththeINTERSECToperator,whichreturnsonlythoserowsreturnedby
T
bothqueries.OnceyouexecutetheaboveINTERSECTquery,youwillgetthefollowingresultset.Pleaseobserve,
here we got only 3 rows in the result set which are common in both the result set.
heabovestatementcombinesresultswiththeMINUSoperator,whichreturnsonlyuniquerowsreturnedbythefirst
T
query but not bythesecond.OnceyouexecutetheaboveMINUSOperatorquery,youwillgetthefollowingresult
set.Pleaseobserve,herewegotonly4rowsintheresultsetwhicharepresentinthefirstresultsetbutnotinthe
second result set.
In the next article, I am going to discuss U
NIONO
perator in OraclewithExamples.Here,inthisarticle,Itryto
explain SETO peratorsinOraclewithExamplesandIhopeyouenjoythisSETOperatorsinOraclewithExamples
article.IfyouhaveanyqueriesregardingtheOracleSETOperators,thenpleaseletusknowbyputtingyourqueryin
the comment section.