DB2 SQL SET OPERATIONS : EXCEPT

SQL EXCEPT

SQL EXCEPT returns records from first SELECT query which are not present in any other SELECT queries. EXCEPT only return unique value, all duplicate values are removed from final result.

SELECT id1 FROM table1 EXCEPT SELECT id2 FROM table2

Here is how SQL EXCEPT 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 Query 1 records
which are not in Query 2.
 

ID1
1
2
3
1
2

[/mks_one_quarter]

[mks_one_quarter]4. Remove Duplicated values.
 
 

ID1
1
2
3

[/mks_one_quarter]
[/mks_col]

Leave a Reply

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