Result sets of aggregated data can be generated. With these additional relational operations a single result tuple can represent the aggregate result of an operation applied to one, many, or all of the tuples in a table. Further, these operations can be applied to a series of subsets of a result set, i.e. tuples can be grouped together based upon some selection condition. These operations include:
COUNT()
SUM()
AVG()
MAX()
MIN()
By default each of these aggregate operations is applied to all tuples
in a result set. To generate subsets, or groups of results, SQL provides
the GROUP BY
operator.
SQL: SELECT ... [WHERE ...] GROUP BY attribute
The GROUP BY
operator cannot be used on its own. It must be
used with one of the additional relational operators.
Returns the count of the number of tuples fitting a given condition. NULL
values, if they exist, are counted. The other aggregate functions ignore
NULL
values.
SELECT COUNT(attribute) FROM table
SELECT COUNT(*) AS memberCount FROM member
Returns the number of members in the member table and
names the resulting attribute memberCount. The wildcard
attribute *
simply counts all rows.
SELECT COUNT(DISTINCT memberSurname) AS memberCount FROM member
WHERE YEAR(memberBirthDate) > 1950
Returns the number of members born after 1950, and counts only the number of unique surnames.
SELECT COUNT(*) AS expertiseCount FROM memberBroad
Returns the number of total number of different expertises held by all members in the DCRIS database.
SELECT memberBroadMemberId, COUNT(*) AS expertiseCount FROM memberBroad
GROUP BY memberBroadMemberId
Returns the number of expertises held by each individual member of
the DCRIS database. The GROUP BY
clause causes the
counts to be grouped together by individual member IDs.
The GROUP BY
clause allows the attribute
being grouped by to appear in the final result set.
SELECT memberBroadBroadId, COUNT(*) AS memberCount FROM memberBroad
GROUP BY memberBroadBroadId
Returns the number of members for each expertise in the DCRIS database. Again, the attribute resulting from the count is given a name.
Returns the sum of the values in a numeric attribute.
SELECT SUM(attribute) FROM table
SELECT SUM(Sales * Price) AS totalSales
FROM pub
Returns the total sales of all publications with the alias totalSales.
SELECT pubMemberId, SUM(Sales * Price) AS totalSales
FROM pub
GROUP BY pubMemberId
Returns the total sales of all publications with the alias totalSales for each individual member.
Returns the average value of a given attribute.
SELECT AVG(attribute) FROM table
SELECT AVG(Sales) AS averageSales FROM pub
Returns the average number of copies sold for each publication.
Returns the maximum and minimum values respectively of a given attribute.
SELECT MIN(attribute) FROM table
SELECT MAX(attribute) FROM table
SELECT MAX(Sales * Price) AS maxSales
FROM pub
WHERE pubPubType = 'B'
Returns the maximum sales from all publications that are books.
These operations may be used in combination.
SELECT pubMemberId, COUNT(*) AS members,
SUM(sales) AS totalSales, AVG(sales) AS averageSales,
MIN(sales) AS minSales, MAX(sales) AS maxSales
FROM pub
GROUP BY pubMemberId
This lists the number, total, average, minimum and maximum of publications sold grouped by individual members.