/  Technology   /  Set Difference, Set Intersection, Cartesian Product Operations in Python

Set Difference, Set Intersection, Cartesian Product Operations in Python

 

Lets us understand these operations with some examples: 

Consider the following table: 

R1 :    FACULTY-1

FNoFNameDNoQualSalary
22Riya21Ph.D55000
24Priya22M. Tech50000
25Zayn22M. Tech42000
27Harry23M. Tech28000
30Sia23M. Tech32000
33Max24Ph.D53000
35Fred24Ph.D52000
37Diva25M. Tech26000
39Ben25M. Tech24000
40Trent25M. Tech34000

 

Set Difference Operation ( U ):  

‘Set difference’ operation eliminates second relation data from first relation data.

 

Example-1 :  Find the tuples only in Dno = 23,  but they should not be in DNo = 25.

         sDNo = 23 (Faculty-1)  –  sDNo = 25 (Faculty-1)

 Output :   

FNoFNameDNoQualSalary
27Harry23M. Tech28000
30Sia23M. Tech32000

 

Set Intersection Operation ( Ç ):  

‘Set intersection’ operation takes only common tuples from both the relations.

 

Example-1:  Find the tuples which are in both Dno = 23 and DNo = 25.

        sDNo = 23 (Faculty-1)  Ç  sDNo = 25  (Faculty-1)

 

Output :   No Output

 

Cartesian Product Operation ( X ):

This operation combines every tuple from relation-1 with every tuple from relation-2.

 

Consider the following relations :

                                                                              

R1 :  STUDR2 : DEPT
SNoSNameDNoDNoDName
21Riya2121CSE
23Priya2122IT
26Zayn2223ECE
28Harry2224ME
30Sia2325EEE

 

Example-2 :  Combine the above two relations with Cartesian product operators.

                                               STUD   X   DEPT

 

Output :   Here every tuple in STUD is combined with every tuple in DEPT. There will be a total of 25 tuples and 5 attributes.

 

 

Leave a comment