Aggregate functions allow you to calculate values based upon all data in an attribute of a table. The SQL aggregate functions are: Max, Min, Avg, Sum, Count, StdDev, Variance. Note that AVG and SUM work only with numeric values and both exclude NULL values from the calculations.
Example 1
How many students are there?
SELECT COUNT(*) FROM STUDENT;
COUNT(*) |
6 |
NOTE: COUNT can be used in two ways. COUNT(*) is used to count the number of tuples that satisfy a query. COUNT with DISTINCT is used to count the number of unique values in a named column.
Example 2
Find the number of departments that have faculty in them.
SELECT COUNT(DISTINCT DEPT) FROM FACULTY;
COUNT(DISTINCT) |
4 |
Example 3
Find the average number of credits for students who major in math.
SELECT AVG(CREDITS) FROM STUDENT WHERE MAJOR = ‘Math’;
AVG(CREDITS) |
29 |