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

Set Operations (1)

The document explains SQL Set Operations, which combine multiple SQL SELECT statements, including Union, Union All, Intersect, and Minus. Union eliminates duplicate rows, Union All includes duplicates, Intersect returns common rows, and Minus shows rows present in the first query but absent in the second. Each operation has specific syntax and examples demonstrating their functionality using two sample tables.

Uploaded by

jaswanth.r090
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)
12 views

Set Operations (1)

The document explains SQL Set Operations, which combine multiple SQL SELECT statements, including Union, Union All, Intersect, and Minus. Union eliminates duplicate rows, Union All includes duplicates, Intersect returns common rows, and Minus shows rows present in the first query but absent in the second. Each operation has specific syntax and examples demonstrating their functionality using two sample tables.

Uploaded by

jaswanth.r090
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/ 5

SQL Set Operations

The SQL Set operation is used to combine the two or more SQL SELECT
statements.

Types of Set Operation

1. Union
2. UnionAll
3. Intersect
4. Minus

1. Union

o The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
o In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax

1. SELECT column_name FROM table1


2. UNION
3. SELECT column_name FROM table2;

Example:

ID NAME

1 Jack
2 Harry
3 Jackson
The First table

ID NAME

3 Jackson
4 Stephan
5 David
The Second table

Union SQL query will be:

1. SELECT * FROM First


2. UNION
3. SELECT * FROM Second;
The result set table will look like:

ID NAME

1 Jack
2 Harry
3 Jackson
4 Stephan
5 David

2. Union All

Union All operation is equal to the Union operation. It returns the set without
removing duplication and sorting the data.

Syntax:

1. SELECT column_name FROM table1


2. UNION ALL
3. SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;

ID NAME

1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
The result set table will look like:

3. Intersect

o It is used to combine two SELECT statements. The Intersect operation


returns the common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the
same.
o It has no duplicates and it arranges the data in ascending order by default.

Syntax

1. SELECT column_name FROM table1


2. INTERSECT
3. SELECT column_name FROM table2;

Example:

Using the above First and Second table.

Intersect query will be:

1. SELECT * FROM First


2. INTERSECT
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

3 Jackson
4. Minus

o It combines the result of two SELECT statements. Minus operator is used to


display the rows which are present in the first query but absent in the second
query.
o It has no duplicates and data arranged in ascending order by default.

Syntax:

1. SELECT column_name FROM table1


2. MINUS
3. SELECT column_name FROM table2;

Example

Using the above First and Second table.

Minus query will be:

1. SELECT * FROM First


2. MINUS
3. SELECT * FROM Second;

The result set table will look like:

ID NAME

1 Jack
2 Harry

You might also like