In relational algebra, 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
are restricted to the set
. For an example, consider the following two tables which are the relation
and its projection on the attributes
and
:
|
|
Name
|
Age
|
Weight
|
Harry
|
34
|
80
|
Sally
|
28
|
64
|
George
|
29
|
70
|
Helena
|
54
|
54
|
Peter
|
34
|
80
|
|
Age
|
Weight
|
34
|
80
|
28
|
64
|
29
|
70
|
54
|
54
|
|
Note that Harry and Peter have the same age and weight, but since the result is a relation, and therefore a set, this combination only appears once in the result.
More formally the semantics of the projection is defined as follows:
![{\displaystyle \pi _{a_{1},...,a_{n}}(R)=\{\ t[a_{1},...,a_{n}]:\ t\in R\ \}}](/media/api/rest_v1/media/math/render/svg/cb0ae79c7c10bcb22be3ae976923cb051a0cf59c)
where
is the restriction of the tuple
to the set
so that
![{\displaystyle t[a_{1},...,a_{n}]=\{\ (a',v)\ |\ (a',v)\in t,\ a'\in a_{1},...,a_{n}\ \}}](/media/api/rest_v1/media/math/render/svg/8bd3afe87d8492f4eb933bbf7cefe5087b8590da)
The result of a projection
is only defined if
is a subset of the header of
.
In SQL, projections are done by using the SELECT
statement.