5.17
The Group-by Option
The group by clause serves to group together tuples of tables based on the
common value of an attribute or of a group of attributes. Suppose, for instance,
that we wish to partition the table GRADES into groups based on the course
number. This can be done by using a construct like
select ... from GRADES group by cno
Conceptually, we operate on the table shown in Figure 5.2. The reader should
imagine that the table has been divided into five groups, each corresponding
to one course. In the previous select, we left open the target list following
select. Once a table has been partitioned into groups (using group by), the
select construct that we use must return one or more atomic pieces of data for
every group. The term atomic, in this context, refers to simple pieces of data
(numbers, strings, etc.). By contrast, a set of values is not an atomic piece of
data. For instance, the number of students enrolled in each course can be listed
by:
select cno, count(stno) as totenr from GRADES
group by cno
This results in the table:
CNOTOTENR
----- ----------