Sunday, 28 October 2012

Set operators in sql

Set operators
Union
Intersect
Except

Tables:        
    EMP1
  

Classif_ID Classification
1 Pop
2 Country
3 Alternative
4 Metal
7 cad
8 auto

EMP2


Classif_ID Classification
5 cad
6 auto
1 Pop
2 Country
3 Alternative
4 Metal

UNION :
   
select * from  Emp1


union
 select * from Emp2

it will give the results from both the tables with matched and unmatched records
Result

Classif_ID Classification
1 Pop
2 Country
3 Alternative
4 Metal
5 cad
6 auto
7 cad
8 auto
INTERSECT:

 select * from  Emp1
    EXCEPT 
  select * from Emp2
Result:

Classif_ID Classification
1                Pop
2              Country
3              Alternative
4               Metal


EXCEPT

 select * from Emp1
    EXCEPT 
  select * from  Emp2
Result

Classif_ID Classification
7                 cad
8                 auto




















No comments:

Post a Comment