Wednesday, January 30, 2008

SQL Basics - Using Aggregate Functions

AGGREGATE FUNCTIONS
Aggregate functions are used to summarize values, such as getting the sum of many rows, the average, etc. Let's go through the most used functions.

AVERAGE
AVG() is used to get the average of a collection of rows.

Usage: SELECT AVG(colname) FROM ..... [WHERE...]
_________________________________________
COUNT
COUNT () is used to return the number of rows in a given column or rows that are defined in a WHERE clause.

Usage: SELECT COUNT(colname) FROM ..... [WHERE...]

Using the above syntax will return the number of rows that don't have a NULL value. To return the full number of rows including rows with a NULL value use:

SELECT COUNT(*) FROM .....
_________________________________________
MAX
MAX() is used to get the greatest value in the column or defined rows.

Usage: SELECT MAX(colname) FROM ..... [WHERE...]
__________________________________
MIN
MIN() returns the lowest value in the column or defined rows.

Usage: SELECT MAX(colname) FROM ..... [WHERE...]
_________________________________________
SUM
SUM() Returns the sum of the column or the defined rows.

Usage: SELECT SUM(colname) FROM ..... [WHERE...]

No comments: