0% found this document useful (0 votes)
3 views9 pages

SET+Operators

The document discusses Set Operators in SQL, which are used to combine or compare datasets, including UNION, UNION ALL, INTERSECT, and MINUS. It outlines the rules for performing set operations, such as the need for the same number of columns and compatible datatypes. Additionally, it highlights the differences between UNION and UNION ALL in terms of duplicate elimination and performance considerations.

Uploaded by

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

SET+Operators

The document discusses Set Operators in SQL, which are used to combine or compare datasets, including UNION, UNION ALL, INTERSECT, and MINUS. It outlines the rules for performing set operations, such as the need for the same number of columns and compatible datatypes. Additionally, it highlights the differences between UNION and UNION ALL in terms of duplicate elimination and performance considerations.

Uploaded by

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

DATABASE AND

SQL

BY
JANARDHANA BANDI
SET OPERATORS
SET OPERATORS IN SQL
• Set Operators in SQL are same as Set Operators in Mathematics.
• Used to combine or compare two or more datasets.
• Below are the rules to perform Set operations
• Number of columns must be same in all Select queries
• Order of columns must be same in all Select queries
• Datatypes must be compatible
Below are the 4 set operators is SQL:
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION
UNION: Union is used to combine result sets of two or more Select queries.
After combining the result sets it eliminate duplicates if any, that means if
same record exists in both result sets it shows only one record in the
TABLE_B
output. TABLE_A
empid name
empid name
Syntax: 22 Gopal
21 Raja
SELECT col1, col2, …col_n FROM TABLE_A 25 Seetha
22 Gopal 28 Kamal
UNION
SELECT col1, col2, …col_n FROM TABLE_B TABLE_A UNION
TABLE_B
UNION empid name
SELECT col1, col2, …col_n FROM TABLE_C 21 Raja
22 Gopal
…..
25 Seetha
28 Kamal
UNION ALL
UNION ALL: Same as Union but Union all but it doesn’t eliminate
duplicates.
TABLE_B
TABLE_A
Syntax: empid name
empid name
SELECT col1, col2, …col_n FROM TABLE_A 22 Gopal
21 Raja
25 Seetha
UNION ALL 22 Gopal 28 Kamal
SELECT col1, col2, …col_n FROM TABLE_B
TABLE_A UNION ALL
UNION ALL TABLE_B
SELECT col1, col2, …col_n FROM TABLE_C empid name
….. 21 Raja
22 Gopal
22 Gopal
25 Seetha
28 Kamal
INTERSECT
INTERSECT: It returns only common rows between both the tables.
Syntax:
SELECT col1, col2, …col_n FROM TABLE_A TABLE_B
TABLE_A
INTERSECT empid name
empid name
22 Gopal
SELECT col1, col2, …col_n FROM TABLE_B 21 Raja
25 Seetha
INTERSECT 22 Gopal 28 Kamal
SELECT col1, col2, …col_n FROM TABLE_C
….. TABLE_A INTERSECT
TABLE_B
empid name
22 Gopal
MINUS
MINUS: It returns the records that exists in first result set and do not exists
in the next result set. Very useful for data validation between two tables.
TABLE_B
TABLE_A
Syntax: empid name
empid name
22 Gopal
SELECT col1, col2, …col_n FROM TABLE_A 21 Raja
25 Seetha
MINUS 22 Gopal 28 Kamal
SELECT col1, col2, …col_n FROM TABLE_B

TABLE_A MINUS TABLE_B TABLE_B MINUS TABLE_A


empid name empid name
21 Raja 25 Seetha
28 Kamal
SET OPERATORS IN SQL
Important Interview question:
What is the difference between UNION and UNION ALL, which gives better performance and
which one you choose?
Ans: Union combines two or more result sets and eliminate the duplicate records, but Union All
doesn’t eliminate duplicates.
Performance wise UNION ALL is better as the task of duplicate elimination is not required here.
So if you are concerned with duplicates better choose UNION and if you are sure that there are
no duplicates then choose UNION ALL which gives better performance.
Thank You

Janardhana
Bandi

You might also like