DB2 SQL SET OPERATIONS : INTERSECT

SQL INTERSECT

SQL INTERSECT return records which are common in all the SELECT queries involved in operation. INTERSECT only return unique value, all duplicate values are removed from final result.
SELECT id1 FROM table1 INTERSECT SELECT id2 FROM table2


Here is how SQL INTERSECT works [SAMPLE DATA]
[mks_col]

[mks_one_quarter]1. Execute Query 1
“SELECT id1 FROM table1”
and get result set.

TABLE1.ID1
1
2
3
4
5
1
2

[/mks_one_quarter]

[mks_one_quarter]2. Execute Query 2
“SELECT id2 FROM table2”
and get result set.

TABLE2.ID2
4
5
6
7
8
5
6

[/mks_one_quarter]

[mks_one_quarter]3. Get common records from
Query 1 and Query 2.
 

ID1
4
5
4
5
5

[/mks_one_quarter]

[mks_one_quarter]4. Remove Duplicated values.
 
 

ID1
4
5

[/mks_one_quarter]
[/mks_col]

Leave a Reply

Your email address will not be published. Required fields are marked *