Formal notation, giving name to set function results and
set function on single group
Formal Notation for Grouping and Set functions
The operation where:
a set of tuples (i.e., a relation)
is first separated into different groups based on one or more attributes
and then applying one or more set functions on each group
is denoted as follows:
The
output
of the set function is a
relation
containing the following attributes:
The content
(i.e., tuples )
of the relation consists of:
all
distinct values
of the
grouping attributes attr1,
attr2, ...,
attrN.
and the
function values f1(a1),
f2(a2), ...,
fM(aM) on
the corresponding group.
Example:
Notice that the output is indeed a set of tuples -
i.e., a relation.
Notice that there is NO tuple with
attribute values (DNO=4, Sex='F')
when this group (set) is empty !!!
The attribute name for the function values is named
after the function name.
Giving attributes a "proper name" in the result relation
Often -- for clarity reasons -- one or more
attribute
in the result relation
are
renamed
The attribute
in the result relation
are given new names
by
sepcifying explicit names
for the attributes when the
result is computed
Example:
Applying (set) functions without any grouping attributes
Recall:
if we do not specify
any grouping attributes:
ℱsetfunc(attr)(R)
^
|
no grouping attributes specified
Then:
ℱsetfunc(attr)(R)
will compute the
set function "setfunc" on
tuples in
the entire set R
(we have seen this when we
discussed the
set functions)
Example:
Find the
average salary
of all employees
and the total number of employees
in the company:
Time for more interesting examples....
We will next do some examples to help you develop skills
to formulate queries in relation algebra -
these skills will help you formulate queries in SQL.