Relation algebra in pure mathematics is an algebraic structure, relevant to mathematical logic and set theory.
Relational algebra is essentially equivalent in expressive power to relational calculus (and thus firstorder logic); this result is known as Codd's theorem. Some care, however, has to be taken to avoid a mismatch that may arise between the two languages since negation, applied to a formula of the calculus, constructs a formula that may be true on an infinite set of possible tuples, while the difference operator of relational algebra always returns a finite result. To overcome these difficulties, Codd restricted the operands of relational algebra to finite relations only and also proposed restricted support for negation (NOT) and disjunction (OR). Analogous restrictions are found in many other logicbased computer languages. Codd defined the term relational completeness to refer to a language that is complete with respect to firstorder predicate calculus apart from the restrictions he proposed. In practice the restrictions have no adverse effect on the applicability of his relational algebra for database purposes.
The six primitive operators of Codd's algebra are the selection, the projection, the Cartesian product (also called the cross product or cross join), the set union, the set difference, and the rename. (Actually, Codd omitted the rename, but the compelling case for its inclusion was shown by the inventors of ISBL.) These six operators are fundamental in the sense that none of them can be omitted without losing expressive power. Many other operators have been defined in terms of these six. Among the most important are set intersection, division, and the natural join. In fact ISBL made a compelling case for replacing the Cartesian product by the natural join, of which the Cartesian product is a degenerate case.
Altogether, the operators of relational algebra have identical expressive power to that of domain relational calculus or tuple relational calculus. However, for the reasons given in the Introduction above, relational algebra has strictly less expressive power than that of firstorder predicate calculus without function symbols. Relational algebra actually corresponds to a subset of firstorder logic that is Horn clauses without recursion and negation.
The Cartesian product is defined differently from the one defined in set theory in the sense that tuples are considered to be 'shallow' for the purposes of the operation. That is, unlike in set theory, where the Cartesian product of a ntuple by an mtuple is a set of 2tuples, the Cartesian product in relational algebra has the 2tuple "flattened" into an n+mtuple. More formally, R × S is defined as follows:
Natural join is a binary operator that is written as (R $bowtie$ S) where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names. For an example consider the tables Employee and Dept and their natural join:
! Name
 ! DeptName
 ! Name

This can also be used to define composition of relations. In category theory, the join is precisely the fiber product.
The natural join is arguably one of the most important operators since it is the relational counterpart of logical AND. Note carefully that if the same variable appears in each of two predicates that are connected by AND, then that variable stands for the same thing and both appearances must always be substituted by the same value. In particular, natural join allows the combination of relations that are associated by a foreign key. For example, in the above example a foreign key probably holds from Employee.DeptName to Dept.DeptName and then the natural join of Employee and Dept combines all employees with their departments. Note that this works because the foreign key holds between attributes with the same name. If this is not the case such as in the foreign key from Dept.manager to Emp.empnumber then we have to rename these columns before we take the natural join. Such a join is sometimes also referred to as an equijoin (see θjoin).
More formally the semantics of the natural join is defined as follows:
where $p$ is a predicate that is true for a binary relation $r$ iff $r$ is a functional binary relation. It is usually required that $R$ and $S$ must have at least one common attribute, but if this constraint is omitted then the natural join becomes exactly the Cartesian product.
The natural join can be simulated with Codd's primitives as follows. Assume that b_{1},...,b_{m} are the attribute names common to R, S, a_{1},...,a_{n} are the attribute names unique to R and c_{1},...,c_{k} are the attribute unique to S. Furthermore assume that the attribute names d_{1},...,d_{m} are neither in R nor in S. In a first step we can now rename the common attribute names in S:
Then we take the Cartesian product and select the tuples that are to be joined:
Finally we take a projection to get rid of the renamed attributes:
! CarModel
 ! BoatModel
 ! CarModel

If we want to combine tuples from two relations where the combination condition is not simply the equality of shared attributes then it is convenient to have a more general form of join operator, which is the θjoin (or thetajoin). The θjoin is a binary operator that is written as $begin\{matrix\}\; R\; bowtie\; S\; a\; theta\; bend\{matrix\}$ or $begin\{matrix\}\; R\; bowtie\; S\; a\; theta\; vend\{matrix\}$ where a and b are attribute names, θ is a binary relation in the set {<, ≤, =, >, ≥}, v is a value constant, and R and S are relations. The result of this operation consists of all combinations of tuples in R and S that satisfy the relation θ. The result of the θjoin is defined only if the headers of S and R are disjoint, that is, do not contain a common attribute.
The simulation of this operation in the fundamental operations is therefore as follows:
In case the operator θ is the equality operator (=) then this join is also called an equijoin.
Note, however, that a computer language that supports the natural join and rename operators does not need θjoin as well, as this can be achieved by selection from the result of a natural join (which degenerates to Cartesian product when there are no shared attributes).
The semijoin is joining similar to the natural join and written as R $ltimes$ S where R and S are relations. The result of the semijoin is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names. For an example consider the tables Employee and Dept and their semi join:
! Name
 ! DeptName
 ! Name

More formally the semantics of the semijoin is defined as follows:
where fun(r) is as in the definition of natural join.
The semijoin can be simulated using the natural join as follows. If a_{1}, ..., a_{n} are the attribute names of R, then
The antijoin, written as R $triangleright$ S where R and S are relations, is similar to the natural join, but the result of an antijoin is only those tuples in R for which there is NOT a tuple in S that is equal on their common attribute names.
For an example consider the tables Employee and Dept and their antijoin:
! Name
 ! DeptName
 ! Name

The antijoin is formally defined as follows:
or
where fun(r) is as in the definition of natural join.
The antijoin can also be defined as the complement of the semijoin, as follows:
! Student
 ! Task
 ! Student

If DBProject contains all the tasks of the Database project then the result of the division above contains exactly all the students that have completed the Database project.
More formally the semantics of the division is defined as follows:
where {a_{1},...,a_{n}} is the set of attribute names unique to R and t[a_{1},...,a_{n}] is the restriction of t to this set. It is usually required that the attribute names in the header of S are a subset of those of R because otherwise the result of the operation will always be empty.
The simulation of the division with the basic operations is as follows. We assume that a_{1},...,a_{n} are the attribute names unique to R and b_{1},...,b_{m} are the attribute names of S. In the first step we project R on its unique attribute names and construct all combinations with tuples in S:
In the prior example, T would represent a table such that every Student (because Student is the unique key / attribute of the Completed table) is combined with every given Task. So Eugene, for instance, would have two rows, Eugene > Database1 and Eugene > Database2 in T.
In the next step we subtract R from this relation:
Whereas the result of a join (or inner join) consists of tuples formed by combining matching tuples in the two operands, an outer join contains those tuples and additionally some tuples formed by extending an unmatched tuple in one of the operands by "fill" values for each of the attributes of the other operand.
The operators defined in this section assume the existence of a null value, ω, which we do not define, to be used for the fill values. It should not be assumed that this is the NULL defined for the database language SQL, nor should it be assumed that ω is a mark rather than a value, nor should it be assumed that the controversial threevalued logic is introduced by it.
Three outer join operators are defined: left outer join, right outer join, and full outer join. (The word "outer" is sometimes omitted.)
The left outer join is written as R =X S where R and S are relations. The result of the left outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition (loosely speaking) to tuples in R that have no matching tuples in S.
For an example consider the tables Employee and Dept and their left outer join:
! Name
 ! DeptName
 ! Name

In the resulting relation, tuples in S which have no common values in common attribute names with tuples in R take a null value, ω.
Since there are no tuples in Dept with a DeptName of Finance or Executive, ωs occur in the resulting relation where tuples in DeptName have tuples of Finance or Executive.
Let r_{1}, r_{2}, ..., r_{n} be the attributes of the relation R and let {(ω, ..., ω)} be the singleton relation on the attributes that are unique to the relation S (those that are not attributes of R). Then the left outer join can be described in terms of the natural join (and hence using basic operators) as follows:
The right outer join of relations R and S is written as R X= S. The result of the right outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R.
For example consider the tables Employee and Dept and their right outer join:
! Name
 ! DeptName
 ! Name

In the resulting relation, tuples in R which have no common values in common attribute names with tuples in S take a null value, ω.
Since there are no tuples in Employee with a DeptName of Production, ωs occur in the Name attribute of the resulting relation where tuples in DeptName had tuples of Production.
Let s_{1}, s_{2}, ..., s_{n} be the attributes of the relation S and let {(ω, ..., ω)} be the singleton relation on the attributes that are unique to the relation R (those that are not attributes of S). Then, as with the left outer join, the right outer join can be simulated using the natural join as follows:
The outer join or full outer join in effect combines the results of the left and right outer joins.
The full outer join is written as R =X= S where R and S are relations. The result of the full outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R and tuples in R that have no matching tuples in S in their common attribute names.
For an example consider the tables Employee and Dept and their full outer join:
! Name
 ! DeptName
 ! Name

In the resulting relation, tuples in R which have no common values in common attribute names with tuples in S take a null value, ω. Tuples in S which have no common values in common attribute names with tuples in R also take a null value, ω.
The full outer join can be simulated using the left and right outer joins (and hence the natural join and set union) as follows:
It can be proven that there is no relational algebra expression E(R) taking R as a variable argument which produces R^{+}. The proof is based on the fact that, given a relational expression E for which it is claimed that E(R) = R^{+}, where R is a variable, we can always find an instance r of R (and a corresponding domain d) such that E(r) ≠ r^{+}.
Queries can be represented as a tree, where
Our primary goal is to transform expression trees into equivalent expression trees, where the average size of the relations yielded by subexpressions in the tree are smaller than they were before the optimization. Our secondary goal is to try to form common subexpressions within a single query, or if there are more than one queries being evaluated at the same time, in all of those queries. The rationale behind that second goal is that it is enough to compute common subexpressions once, and the results can be used in all queries that contain that subexpression.
Here we present a set of rules, that can be used in such transformations.
Rules about selection operators play the most important role in query optimization. Selection is an operator that very effectively decreases the number of rows in its operand, so if we manage to move the selections in an expression tree towards the leaves, the internal relations (yielded by subexpressions) will likely shrink.
Selection is idempotent (multiple applications of the same selection have no additional effect beyond the first one), and commutative (the order selections are applied in has no effect on the eventual result).
A selection whose condition is a conjunction of simpler conditions is equivalent to a sequence of selections with those same individual conditions, and selection whose condition is a disjunction is equivalent to a union of selections. These identities can be used to merge selections so that fewer selections need to be evaluated, or to split them so that the component selections may be moved or optimized separately.
This can be effectively done, if the cross product is followed by a selection operator, e.g. $sigma\_\{A\}$($R$ × $P$). Considering the definition of join, this is the most likely case. If the cross product is not followed by a selection operator, we can try to push down a selection from higher levels of the expression tree using the other selection rules.
In the above case we break up condition $A$ into conditions $B$, $C$ and $D$ using the split rules about complex selection conditions, so that $A$ = $B$ $wedge$ $C$ $wedge$ $D$ and $B$ only contains attributes from $R$, $C$ contains attributes only from $P$ and $D$ contains the part of $A$ that contains attributes from both $R$ and $P$. Note, that $B$, $C$ or $D$ are possibly empty. Then the following holds:
Selection is associative with projection if and only if the fields referenced in the selection condition are a subset of the fields in the projection. Performing selection before projection may be useful if the operand is a cross product or join. In other cases, if the selection condition is relatively expensive to compute, moving selection outside the projection may reduce the number of tuples which must be tested (since projection may produce fewer tuples due to the elimination of duplicates resulting from elided fields).
Projection is idempotent, so that a series of (valid) projections is equivalent to the outermost projection.
Projection is distributive over set difference, union, and intersection.
Successive renames of a variable can be collapsed into a single rename. Rename operations which have no variables in common can be arbitrarily reordered with respect to one another, which can be exploited to make successive renames adjacent so that they can be collapsed.
Rename is distributive over set difference, union, and intersection.