Relational Algebra

Basic Operations

 * Selection ($$\sigma$$) Selects a subset of rows from the relation (horizontal)
 * Projection ($$\pi$$) Selects a subset of columns from the relation (vertical)
 * Cross-product ($$x$$) Operation that takes each instance of one relation and maps it to every instance of the other relation.
 * Set-difference ($$-$$) Operation that keeps all tuples in $$R _1$$ but not in $$R _2$$
 * Union ($$\cup$$) Operations that keeps all tuples in $$R _1$$ and/or in $$R _2$$

Selection


Selection allows us to select rows that satisfy a selection condition. In the general case, we can select in the following way
 * $$\sigma _c$$

Projection


Projection allows us to select columns that have certain names. In the general case, we can select in the following way Note: Because projection is a relational algebra operation, it removes duplicates. In real world systems, duplicate elimination is not usually done unless the user specifies for it to occur.
 * $$\pi _{a1,a2,...}$$

Union


Union allows us to combine two relations that are union compatible

Union Compatible

 * The two relations must have the same number of fields
 * 'Corresponding' fields must have the same type

Duplication Elimination

 * Duplication elimination must be performed, as it is possible that the two relations contain duplicate records

Set Difference
The set difference of two relations $$R _1$$ and $$R _2$$ are all the records that are in $$R _1$$ and not in $$R _2$$

Note: It is important to see that it matters what order set difference is performed in, similar to real subtraction.

Cross Product


The cross product of two relations $$S$$. and $$R$$ is each row of $$S$$ paired with each row of $$R$$

Number of Records Generated
If $$S$$ has s records and $$R$$ has r records, the total number of records that is generated from the cross product of the two relations is rs.

Compound Operators
Compound operators can be defined with the five basic operators

Intersection


The intersection of two relations $$S$$ and $$R$$ is the records that are in both S and R. In terms of the five basic operators, the intersection is computed as follows:
 * $$R \cap S = R - (R - S)$$

Joins


Conceptually, a join is performed between two relations $$R$$ and $$S$$ (denoted $$R \bowtie S$$) as follows:
 * 1) Take the cross product of $$R$$ and $$S$$
 * 2) Select rows where attributes that appear in both relations have equal values
 * 3) Project all unique attributes and one copy of the common ones

Theta Join
The theta join is a special case of normal join. It is defined as:


 * R \bowtie _c S = \sigma _c (R \times S)