In database theory, relational algebra is a theory that uses algebraic structures for modeling data and defining queries on it with well founded semantics. The theory was introduced by Edgar F. Codd as part of his relational model of data in 1970.
The main application of relational algebra is to provide a theoretical foundation for relational databases, particularly query languages for such databases, chief among which is SQL. Relational databases store tabular data represented as relations. Queries over relational databases often likewise return tabular data represented as relations.
The main purpose of relational algebra is to define operators that transform one or more input relations to an output relation. Given that these operators accept relations as input and produce relations as output, they can be combined and used to express complex queries that transform multiple input relations (whose data are stored in the database) into a single output relation (the query results).
Unary operators accept a single relation as input. Examples include operators to filter certain attributes (columns) or tuples (rows) from an input relation. Binary operators accept two relations as input and combine them into a single output relation. For example, taking all tuples found in either relation (union), removing tuples from the first relation found in the second relation (difference), extending the tuples of the first relation with tuples in the second relation matching certain conditions, and so forth.
Introduction
A relation of arity is a set of tuples. Relational algebra operates on homogeneous sets of tuples, where an tuple is a tuple (row; index ) with (or data domains), thus is the number of rows of tuples in a table and is the number of columns (and all entries in each column have the same ).
A relation also has a unique tuple called the header which gives each column a unique name or attribute inside the relation. Attributes are used in projections and selections.
Set operators
The relational algebra uses set union, set difference, and Cartesian product from set theory, and adds additional constraints to these operators to create new ones.
For set union and set difference, the two relations involved must be that is, the two relations must have the same set of attributes. Because set intersection is defined in terms of set union and set difference, the two relations involved in set intersection must also be union-compatible.
For the Cartesian product to be defined, the two relations involved must have disjoint headers (i.e. they must not have a common attribute name).
In addition, the Cartesian product is defined differently from the one in set theory, in the sense that tuples are considered to be "shallow" for the purposes of the operation. That means the Cartesian product of a set of -tuples with a set of -tuples yields a set of "flattened" -tuples (whereas basic set theory would have prescribed a set of 2-tuples, each containing an -tuple and an -tuple). In relational algebra, the Cartesian product is defined formally as
The cardinality of the Cartesian product is the product of the cardinalities of its factors, that is, .
Projection
A projection () is a unary operation written as where is a set of attribute names. The result of such projection is defined as the set that is obtained when all tuples in R are restricted to the set .
Note: when implemented in SQL standard the "default projection" returns a multiset instead of a set, and the projection to eliminate duplicate data is obtained by the addition of the DISTINCT keyword.
Selection
A selection (sometimes called a restriction in reference to E.F. Codd's 1970 paper and not, contrary to a popular belief, to avoid confusion with SQL's use of SELECT, since Codd's article predates the existence of SQL) is a unary operation that denotes a subset of a relation.
To obtain a listing of all friends or business associates in an address book, the selection might be written as
The result would be a relation containing every attribute of every unique record where is true or where is true.
A selection is written as or where: and are attribute names is a binary operation in the set is a value constant is a relation
The selection denotes all tuples in for which holds between the and the attribute.
The selection denotes all tuples in for which holds between the attribute and the value .
For an example, consider the following tables where the first table gives the relation , the second table gives the result of and the third table gives the result of .
Name Age Weight Harry 34 80 Sally 28 64 George 29 70 Helena 54 54 Peter 34 80 Name Age Weight Harry 34 80 Helena 54 54 Peter 34 80 Name Age Weight Helena 54 54
More formally the semantics of the selection is defined as follows:
The result of the selection is only defined if the attribute names that it mentions are in the heading of the relation that it operates upon.
Generalized selection A generalized selection (σ) is a unary operation written as where is a propositional formula that consists of atoms as allowed in the normal selection and the logical operators (and), (or) and (negation). This selection selects all those tuples in R for which holds.
For an example, consider the following tables where the first table gives the relation and the second the result of .
Name Age Weight Harry 34 80 Sally 28 64 George 29 70 Helena 54 54 Peter 34 80
Name Age Weight Helena 54 54
Formally the semantics of the generalized selection is defined as follows:
The result of the selection is only defined if the attribute names that it mentions are in the header of the relation that it operates upon.
The generalized selection is expressible with other basic algebraic operations. A simulation of generalized selection using the fundamental operators is defined by the following rules:
Computer languages In computer languages it is expected that any truth-valued expression be permitted as the selection condition rather than restricting it to be a simple comparison.
In SQL, selections are performed by using WHERE definitions in SELECT, UPDATE, and DELETE statements, but note that the selection condition can result in any of three truth values (true, false and unknown) instead of the usual two.
In SQL, general selections are performed by using WHERE definitions with AND, OR, or NOT operands in SELECT, UPDATE, and DELETE statements.
Rename
A rename is a unary operation written as where:
is a relation and are attribute names is an attribute of
To rename the "isFriend" attribute to "isBusinessContact" in a relation, might be used.
There is also the notation, where R is renamed to x and the attributes are renamed to .
The result is identical to except that the attribute in all tuples is renamed to . For an example, consider the following invocation of on an relation and the result of that invocation:
Name EmployeeId Harry 3415 Sally 2241
EmployeeName EmployeeId Harry 3415 Sally 2241
Formally, the semantics of the rename operator is defined as follows:
where is defined as the tuple , with the attribute renamed to , so that:
Joins and join-like operators
Common extensions In practice the classical relational algebra described above is extended with various operations such as outer joins, aggregate functions and even transitive closure.
Outer joins
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. Outer joins are not considered part of the classical relational algebra discussed so far.
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; in practice this corresponds to the NULL in SQL. In order to make subsequent selection operations on the resulting table meaningful, a semantic meaning needs to be assigned to nulls; in Codd's approach the propositional logic used by the selection is extended to a three-valued logic, although we elide those details in this article.
Three outer join operators are defined: left outer join, right outer join, and full outer join. (The word "outer" is sometimes omitted.)
Left outer join
The left outer join (⟕) is written as R ⟕ 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:
+ Employee Name EmpId DeptName Harry 3415 Finance Sally 2241 Sales George 3401 Finance Harriet 2202 Sales Tim 1123 Executive
+ Dept DeptName Manager Sales Harriet Production Charles
+ Employee ⟕ Dept Name EmpId DeptName Manager Harry 3415 Finance ω Sally 2241 Sales Harriet George 3401 Finance ω Harriet 2202 Sales Harriet Tim 1123 Executive ω
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 Employee have a DeptName of Finance or Executive.
Let r1, r2, ..., rn 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:
Right outer join The right outer join (⟖) behaves almost identically to the left outer join, but the roles of the tables are switched.
The right outer join of relations R and S is written as R ⟖ 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:
+ Employee Name EmpId DeptName Harry 3415 Finance Sally 2241 Sales George 3401 Finance Harriet 2202 Sales Tim 1123 Executive
+ Dept DeptName Manager Sales Harriet Production Charles
+ Employee ⟖ Dept Name EmpId DeptName Manager Sally 2241 Sales Harriet Harriet 2202 Sales Harriet ω ω Production Charles
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 and EmpId attributes of the resulting relation where tuples in Dept had DeptName of Production.
Let s1, s2, ..., sn 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:
Full outer join
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 ⟗ 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:
+ Employee Name EmpId DeptName Harry 3415 Finance Sally 2241 Sales George 3401 Finance Harriet 2202 Sales Tim 1123 Executive
+ Dept