Selection (relational algebra)

In relational algebra, 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.

A selection is written as $$\sigma_{a \theta b}( R )$$ or $$\sigma_{a \theta v}( R )$$ where:
 * $a$ and $b$ are attribute names
 * $&theta;$ is a binary operation in the set $$\{\;<, \le, =, \ne, \ge, \;>\}$$
 * $v$ is a value constant
 * $R$ is a relation

The selection $$\sigma_{a \theta b}( R )$$ denotes all tuples in $R$ for which $&theta;$ holds between the $a$ and the $b$ attribute.

The selection $$\sigma_{a \theta v}( R )$$ denotes all tuples in $R$ for which $&theta;$ holds between the $a$ attribute and the value $v$.

For an example, consider the following tables where the first table gives the relation $Person$, the second table gives the result of $$\sigma_{\text{Age} \ge 34}( \text{Person} )$$ and the third table gives the result of $$\sigma_{\text{Age} = \text{Weight}}( \text{Person} )$$.

More formally the semantics of the selection is defined as follows:


 * $$\sigma_{a \theta b}( R ) = \{\ t : t \in R,\ t(a) \ \theta \ t(b) \ \}$$


 * $$\sigma_{a \theta v}( R ) = \{\ t : t \in R,\ t(a) \ \theta \ v \ \}$$

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 $$\sigma_\varphi(R)$$ where $$\varphi$$ is a propositional formula that consists of atoms as allowed in the normal selection and, in addition, the logical operators &and; (and), &or; (or) and $$\lnot$$ (negation). This selection selects all those tuples in $R$ for which $$\varphi$$ holds.

For an example, consider the following tables where the first table gives the relation $Person$ and the second the result of $$\sigma_{\text{Age} \ge 30 \ \land \ \text{Weight} \le 60}(\text{Person})$$.

Formally the semantics of the generalized selection is defined as follows:


 * $$\sigma_\varphi(R) = \{ \ t : t \in R, \ \varphi(t) \ \}$$

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:


 * $$\sigma_{\varphi \land \psi}(R) = \sigma_\varphi(R) \cap \sigma_\psi(R)$$
 * $$\sigma_{\varphi \lor \psi}(R) = \sigma_\varphi(R) \cup \sigma_\psi(R)$$
 * $$\sigma_{\lnot \varphi}(R) = R - \sigma_\varphi(R)$$

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  definitions in ,  , and   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  definitions with ,  , or   operands in  ,  , and   statements.