Data

Joins, Intersection, Union, Difference

We can combine relations together in interesting ways.

Join (AND)

If two relations have one or more attributes in common, then they're talking about the same things. This means we can join them using the natural join, & (or if your keyboard can create it).

S & SP
SNOSNAMESTATUSCITYPNOQTY
S1Smith20LondonP1300
S1Smith20LondonP2200
S1Smith20LondonP3400
S1Smith20LondonP4200
S1Smith20LondonP5100
S1Smith20LondonP6100
S2Jones10ParisP1300
S2Jones10ParisP2400
S3Blake30ParisP2200
S4Clark20LondonP2200
S4Clark20LondonP4300
S4Clark20LondonP5400

Notice how we defined our relations with key attribute names specific to the type of relation, e.g. SNO and PNO rather than id for both. This allows us to use the natural join, and other operators, to combine them sensibly with other relations. It also helps with schema design and constraint definition. This goes against many current naming conventions which do use id for every SQL table, to make life easier for the ORM systems.

We can still join relations if the attribute names don't initially match - just rename one of them, e.g. R{*, r_id:=id}

Cross Join (Times / Cartesian Product)

If two relations have no attributes in common, we can still join every tuple from one with every tuple from the other. This is a cross join and is not so useful. We can use * (or × if your keyboard can create it).

S{SNO}
SNO
S1
S2
S3
S4
S5
P{PNO}
PNO
P1
P2
P3
P4
P5
P6
S{SNO} * P{PNO}
SNOPNO
S1P1
S1P2
S1P3
S1P4
S1P5
S1P6
S2P1
S2P2
S2P3
S2P4
S2P5
S2P6
S3P1
S3P2
S3P3
S3P4
S3P5
S3P6
S4P1
S4P2
S4P3
S4P4
S4P5
S4P6
S5P1
S5P2
S5P3
S5P4
S5P5
S5P6

Set operations

If two relations have all the same attributes, we can combine them in three ways:

Intersection

We can use & again (or if your keyboard can create it) to get the intersection.

S[STATUS>10]
SNOSNAMESTATUSCITY
S1Smith20London
S3Blake30Paris
S4Clark20London
S5Adams30Athens
S[STATUS<30]
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S4Clark20London
S[STATUS>10] & S[STATUS<30]
SNOSNAMESTATUSCITY
S1Smith20London
S4Clark20London

Union (OR)

We can use | (or if your keyboard can create it) to get the union.

S[STATUS=10]
SNOSNAMESTATUSCITY
S2Jones10Paris
S[STATUS=30]
SNOSNAMESTATUSCITY
S3Blake30Paris
S5Adams30Athens
S[STATUS=10] | S[STATUS=30]
SNOSNAMESTATUSCITY
S2Jones10Paris
S3Blake30Paris
S5Adams30Athens

Difference (MINUS)

We can use - to get the difference.

S
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S3Blake30Paris
S4Clark20London
S5Adams30Athens
S[STATUS=30]
SNOSNAMESTATUSCITY
S3Blake30Paris
S5Adams30Athens
S - S[STATUS=30]
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S4Clark20London

Difference is not commutative: (R - S) != (S - R) in general.