Consider a table of employee IDs at a restaurant and the shifts that they work. If I have a data scheme given as Shifts(ShiftID, EmployeeID) where Shift and EmployeeID together form a key for the relation, how can I find all of the pairs of employees that only ever work together. That is, I want the tuples (A, B) and (B, A) if employee A works only when employee B works and viceversa.
Is this possible with basic relational algebra operators (select, project, cross product, natural join, rename) and set operations (union, intersect, difference)?
Sample input:
Shift Employee
1 A
1 B
2 A
2 B
3 C
Expected output
Employee1 Employee2
A B
B A
My attempt:
First take the cross product of Shifts and select all the tuples where the ShiftID is the same but the EmployeeID differs (i.e. get all the pairs of employees working the same shift).
Then I tried taking the cross product of the above resulting relation with itself and choosing the tuples where the ShiftID is the same, the first EmployeeID is the same but the second EmployeeID differs. I was trying to get the "DifferentPairs" so I then select the first EmployeeID in the first copy and the second Employee ID in the second copy.
But then I got stuck because I realized that Pairs = DifferentPairs for employees with more than 1 pair since my second cross product could have employees (A, B | A, C) and (A, C | A, B) and so if I take the 1st and 4th IDs, I get (A, C) and (A, B) and now I'm back where I started (Not in the example I gave but just in general).