0% found this document useful (0 votes)
20 views3 pages

Set Operation

Uploaded by

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

Set Operation

Uploaded by

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

Chapter : 4

Structured Query Language (SQL)


-----------------------------------------------------------------------------------------------------------------------------------------

SQL Set Operation


SET operators are mainly used to combine the same type of data from two or more tables. Although
more than one select statement will then be present, only one result set is returned.

Four Set Operators:


The four set operators union, union all, intersect and except allow us to serially combine two or more
select statements.
1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT

Rules on Set Operations:


● The result sets of all queries must have the same number of columns.
● In every result set the data type of each column must match the data type of its
corresponding column in the first result set.
● In order to sort the result, an ORDER BY clause should be part of the last statement.
● The records from the top query must match the positional ordering of the records from
the bottom query.
● The column names or aliases must be found out by the first select statement.

For Example :
Create two tables with the same column name and data type.

CREATE TABLE Students2000(


Name VARCHAR(15),
TotalMark INT)

INSERT INTO Students2000 VALUES('Robert',1063);


INSERT INTO Students2000 VALUES('John',1070);
INSERT INTO Students2000 VALUES('Rose',1032);
INSERT INTO Students2000 VALUES('Abel',1002);

CREATE TABLE Stundents2005(


Name VARCHAR(15),
TotalMark INT)

INSERT INTO Stundents2005 VALUES('Robert',1063);


INSERT INTO Stundents2005 VALUES('Rose',1032);
INSERT INTO Stundents2005 VALUES('Boss',1086);
INSERT INTO Stundents2005 VALUES('Marry',1034);

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
UNION Operation

UNION is used to combine the results of two or more SELECT statements. However
it will eliminate duplicate rows from its resultset. In case of union, number of
columns and datatype must be same in both the tables, on which UNION operation
is being applied.

SELECT Name,TotalMark FROM students2000

UNION

SELECT Name,TotalMark FROM Stundents2005

UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
SELECT Name,TotalMark FROM students2000

UNION ALL

SELECT Name,TotalMark FROM Stundents2005

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore
Chapter : 4
Structured Query Language (SQL)
-----------------------------------------------------------------------------------------------------------------------------------------
INTERSECT
Intersect operation is used to combine two SELECT statements, but it only retuns the records
which are common from both SELECT statements.

In case of Intersect the number of columns and datatype must be same.

NOTE: MySQL does not support INTERSECT operator.

SELECT Name,TotalMark FROM students2000

INTERSECT

SELECT Name,TotalMark FROM Stundents2005

EXCEPT
The EXCEPToperation combines results of two SELECT statements and return only those in the
final result, which belongs to the first set of the result.
EXCEPT clause in SQL Server is working as like MINUS operation in Oracle.
EXCEPT returns any distinct values from the left select query that are not also found on
the right select query.

SELECT Name,TotalMark FROM students2000

EXCEPT

SELECT Name,TotalMark FROM Stundents2005

-----------------------------------------------------------------------------------------------------------------------------------------------------
Subject : DBMS Class : SE SEM: IV Dr.Anirudh Mangore

You might also like