Selection (relational algebra): Difference between revisions

From testwiki
Jump to navigation Jump to search
imported>Asilvering
m Removing link(s) Wikipedia:Articles for deletion/Where (SQL) closed as delete (XFDcloser)
 
(No difference)

Latest revision as of 21:58, 25 January 2025

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 σaθb(R) or σaθv(R) where:

The selection σaθb(R) denotes all tuples in Template:Mvar for which Template:Mvar holds between the Template:Mvar and the Template:Mvar attribute.

The selection σaθv(R) denotes all tuples in Template:Mvar for which Template:Mvar holds between the Template:Mvar attribute and the value Template:Mvar.

For an example, consider the following tables where the first table gives the relation Template:Math, the second table gives the result of σAge34(Person) and the third table gives the result of σAge=Weight(Person).

Person σAge34(Person) σAge=Weight(Person)
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:

σaθb(R)={ t:tR, t(a) θ t(b) }
σaθv(R)={ t:tR, t(a) θ 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 σφ(R) where φ is a propositional formula that consists of atoms as allowed in the normal selection and, in addition, the logical operators ∧ (and), ∨ (or) and ¬ (negation). This selection selects all those tuples in Template:Mvar for which φ holds.

For an example, consider the following tables where the first table gives the relation Template:Math and the second the result of σAge30  Weight60(Person).

Person σAge30  Weight60(Person)
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:

σφ(R)={ t:tR, φ(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:

σφψ(R)=σφ(R)σψ(R)
σφψ(R)=σφ(R)σψ(R)
σ¬φ(R)=Rσφ(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 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.

References

Template:Reflist