SQL Aggregate Functions




SQL aggregate functions are used to sum, count, get the average, get the minimum and get the maximum values from a column or from a sub-set of column values.

To count the rows in the Weather table we can use the SQL COUNT aggregate function:

SELECT COUNT(*)
FROM Weather

To get the average temperature for the Weather table use the AVG SQL aggregate function:

SELECT AVG(AverageTemperature)
FROM Weather

If you want to get the average temperature for a particular city you can do it this way:

SELECT AVG(AverageTemperature)
FROM Weather
WHERE City = 'New York'

To get the minimum value from a numeric table column, use the SQL MIN aggregate function:

SELECT MIN(AverageTemperature)
FROM Weather

To get the maximum value from a numeric table column, use the SQL MAX aggregate function:

SELECT MAX(AverageTemperature)
FROM Weather

Finally to sum up the values in the column use the SQL SUM aggregate function:

SELECT SUM(AverageTemperature)
FROM Weather

You can specify search criteria with the SQL WHERE clause for any of the above SQL aggregate functions.