Database Systems · SQL Aggregation and Views
Aggregate Functions: AVG, MIN, MAX, SUM, COUNT
Overview
Aggregate functions collapse a whole collection of values (a set or a multi-set) into a single value.
They are the standard SQL tools for answering questions like "what is the average salary" or "how many salaries are there", and each one has its own rules about the kind of data and the number of attributes it accepts.
Key concepts
- Aggregate function: a function that takes a collection — either a set or a multi-set of values — as input and returns a single value.
- Set: a collection in which duplicates are not there.
- Multi-set: a collection in which duplicates are there.
- The five aggregate functions: AVG (average), MIN (minimum), MAX (maximum), SUM (total), COUNT.
- EXISTS (the operator this discussion follows on from): if the inner (correlated) query returns at least one tuple, EXISTS returns true, and that row of the outer query is selected and its value printed.
- NOT EXISTS: if the inner query returns no tuple at all, only then does NOT EXISTS return true, so that particular row is selected and that particular value is printed.
How it works
- Choose the column to aggregate; the whole collection of values in that column is collapsed into one result value.
- Apply the function over that column, e.g. AVG over
salarygives the average of all salaries of the employees. - Respect the domain rule: AVG and SUM must be applied strictly on attributes whose domain is numeric — averaging or summing names is meaningless.
MIN and MAX can be applied even on non-numeric data, and COUNT can also be applied on non-numeric data. - Respect the arity rule: AVG, MIN, MAX, SUM must be applied only on one attribute — you cannot write more than one attribute inside them.
COUNT accepts any attribute, and you can even put the entire table (i.e. count all rows).
Formulas
AVG(salary) = (sum of all salary values in the column) / (number of salary values)
SUM(salary) = total of all salary values in the column
- MIN, MAX and COUNT each reduce the whole collection of values of one column to a single value.
Worked examples
Average of salaries over table EMP:
SELECT AVG(salary) FROM EMP;
Returns a single value: the average of all the salaries in the EMP table.
Minimum salary:
SELECT MIN(salary) FROM EMP;
Returns the smallest salary value present in EMP.
Maximum salary:
SELECT MAX(salary) FROM EMP;
Returns the largest salary value present in EMP.
Sum of all salaries:
SELECT SUM(salary) FROM EMP;
Returns one number, the total of all salaries in EMP.
Number of salaries:
SELECT COUNT(salary) FROM EMP;
Returns how many salary values there are (i.e. how many such tuples/values the collection contains).
Comparison
| Function | Argument can be | Non-numeric data allowed? | Number of attributes allowed |
|---|---|---|---|
| AVG | numeric only | No — meaningless on names | exactly one attribute |
| SUM | numeric only | No — meaningless on names | exactly one attribute |
| MIN | numeric or non-numeric | Yes | exactly one attribute |
| MAX | numeric or non-numeric | Yes | exactly one attribute |
| COUNT | any attribute, even the whole table | Yes | any / whole table |
Revision summary
- An aggregate function maps a collection (set or multi-set) of values to a single value.
- Set = no duplicates; multi-set = duplicates present.
- The five aggregate functions are AVG, MIN, MAX, SUM, COUNT.
- AVG and SUM work only on numeric domains; MIN, MAX, COUNT also work on non-numeric data.
- AVG, MIN, MAX, SUM accept exactly one attribute; COUNT accepts any attribute or the whole table.
- EXISTS is true when the inner query returns at least one tuple; NOT EXISTS is true only when the inner query returns no tuple.
Previous-year exam insights
- Aggregate functions ignore NULL values (except COUNT(*)); if a group/table has only NULLs in the aggregated column, COUNT(attr) returns 0 while SUM/AVG/MIN/MAX return NULL.
Asked inGATE 2006ISRO 2017 - COUNT(*) counts tuples/rows of a table or of a derived (sub)query result, so it is the standard way to count rows produced by a join or nested query.
Asked inGATE 2011ISRO 2017 - A correlated subquery counting rows with a strictly larger value gives rank with ties skipped (RANK): COUNT(*) of larger values = k−1 selects the k-th ranked row (< k gives the top k).
Dense rank needs COUNT(DISTINCT col) of values greater than or equal to the current one.
Asked inGATE 2006GATE 2005 - NATURAL JOIN joins on all common attribute names and keeps one copy of each common column; duplicate rows are not eliminated, so the result size can exceed the input and COUNT(*) over it reflects that.
Asked inGATE 2011 - Aggregate functions are only SUM, COUNT, AVG, MIN, MAX; GREATEST/LEAST/ABS, UPPER/LOWER/LENGTH, SQRT/POWER/MOD are scalar (single-row) functions, and SELECT/CREATE/MODIFY are SQL clauses/keywords, not functions.
Asked inUGC NET 2017UGC NET 2007 - An aggregate function cannot appear directly in WHERE; it may appear in the SELECT list or in HAVING, and without GROUP BY the whole table is one group.