Selection (relational algebra) explained

In relational algebra, a selection (sometimes called a restriction in reference to E.F. Codd's 1970 paper[1] 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

\sigmaa(R)

or

\sigmaa(R)

where:

\{<,\le,=,\ne,\ge,>\}

The selection

\sigmaa(R)

denotes all tuples in for which holds between the and the attribute.

The selection

\sigmaa(R)

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

\sigmaAge(Person)

and the third table gives the result of

\sigmaAge(Person)

.
- style="text-align: center"

Person

\sigmaAge(Person)

\sigmaAge(Person)

- style="vertical-align: top"
- style="background-color: silver; text-align: left" ! style="border: 1px solid black" width="34%" Name ! style="border: 1px solid black" width="33%" Age ! style="border: 1px solid black" width="33%" Weight - Harry 34 80 - Sally 28 64 - George 29 70 - Helena 54 54 - Peter 34 80
- style="background-color: silver; text-align: left" ! style="border: 1px solid black" width="34%" Name ! style="border: 1px solid black" width="33%" Age ! style="border: 1px solid black" width="33%" Weight - Harry 34 80 - Helena 54 54 - Peter 34 80
- style="background-color: silver; text-align: left" ! style="border: 1px solid black" width="34%" Name ! style="border: 1px solid black" width="33%" Age ! style="border: 1px solid black" width="33%" Weight - Helena 54 54

More formally the semantics of the selection is defined asfollows:

\sigmaa(R)=\{t:t\inR,t(a)\thetat(b)\}

\sigmaa(R)=\{t:t\inR,t(a)\thetav\}

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), ∨ (or) and

lnot

(negation). This selection selects all those tuples in for which

\varphi

holds.

For an example, consider the following tables where the first table gives the relation and the second the result of

\sigmaAge(Person)

.
- !

Person

\sigmaAge(Person)

- style="vertical-align: top"

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

\sigma\varphi(R)=\{t:t\inR,\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(R)=\sigma\varphi(R)\cap\sigma\psi(R)

\sigma\varphi(R)=\sigma\varphi(R)\cup\sigma\psi(R)

\sigmalnot(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 [[Where (SQL)|WHERE]] definitions in [[Select (SQL)|SELECT]], [[Update (SQL)|UPDATE]], and [[Delete (SQL)|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 (SQL)|WHERE]] definitions with AND, OR, or NOT operands in [[Select (SQL)|SELECT]], [[Update (SQL)|UPDATE]], and [[Delete (SQL)|DELETE]] statements.

External links

Notes and References

  1. E.F.. Codd. E.F. Codd. A Relational Model of Data for Large Shared Data Banks. Communications of the ACM. 13. 6. June 1970. 377–387. 10.1145/362384.362685. free.