Set Difference, Set Intersection, Cartesian Product Operations in Python
Lets us understand these operations with some examples:
Consider the following table:
R1 : FACULTY-1
FNo | FName | DNo | Qual | Salary |
22 | Riya | 21 | Ph.D | 55000 |
24 | Priya | 22 | M. Tech | 50000 |
25 | Zayn | 22 | M. Tech | 42000 |
27 | Harry | 23 | M. Tech | 28000 |
30 | Sia | 23 | M. Tech | 32000 |
33 | Max | 24 | Ph.D | 53000 |
35 | Fred | 24 | Ph.D | 52000 |
37 | Diva | 25 | M. Tech | 26000 |
39 | Ben | 25 | M. Tech | 24000 |
40 | Trent | 25 | M. Tech | 34000 |
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 :
FNo | FName | DNo | Qual | Salary |
27 | Harry | 23 | M. Tech | 28000 |
30 | Sia | 23 | M. Tech | 32000 |
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 : STUD | R2 : DEPT | ||||
SNo | SName | DNo | DNo | DName | |
21 | Riya | 21 | 21 | CSE | |
23 | Priya | 21 | 22 | IT | |
26 | Zayn | 22 | 23 | ECE | |
28 | Harry | 22 | 24 | ME | |
30 | Sia | 23 | 25 | EEE |
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.