Custom Search

Saturday, August 23, 2008

SQL SUM Function

The SUM function returns the total sum of a column in a given selection. NULL values are not included in the calculation.
Syntax

SELECT SUM(column) FROM table

Example

This example returns the sum of all ages in the "person" table:

SELECT SUM(Age) FROM Persons

Result:

98

Example

This example returns the sum of ages for persons that are more than 20 years old:

SELECT SUM(Age) FROM Persons WHERE Age>20

Result:

79

SQL MIN Function

The MIN function returns the lowest value in a column. NULL values are not included in the calculation.
Syntax

SELECT MIN(column) FROM table

Example

SELECT MIN(Age) FROM Persons

Result:

19

Note: The MIN and MAX functions can also be used on text columns, to find the highest or lowest value in alphabetical order.

SQL MIN Function

The MIN function returns the lowest value in a column. NULL values are not included in the calculation.
Syntax

SELECT MIN(column) FROM table

Example

SELECT MIN(Age) FROM Persons

Result:

19

Note: The MIN and MAX functions can also be used on text columns, to find the highest or lowest value in alphabetical order.

SQL MAX Function

The MAX function returns the highest value in a column. NULL values are not included in the calculation.
Syntax

SELECT MAX(column) FROM table

Example

SELECT MAX(Age) FROM Persons

Result:

45

Note: The MIN and MAX functions can also be used on text columns, to find the highest or lowest value in alphabetical order.

SQL COUNT(*) Function

The COUNT(*) function returns the number of selected rows in a selection.
Syntax

SELECT COUNT(*) FROM table

Example

With this "Persons" Table:
Name Age
Hansen, Ola 34
Svendson, Tove 45
Pettersen, Kari 19

This example returns the number of rows in the table:

SELECT COUNT(*) FROM Persons

Result:

3

Example

Return the number of persons that are older than 20 years:

SELECT COUNT(*) FROM Persons WHERE Age>20

Result:

2

SQL COUNT Function

The COUNT(column) function returns the number of rows without a NULL value in the specified column.
Syntax

SELECT COUNT(column) FROM table

Example

With this "Persons" Table:
Name Age
Hansen, Ola 34
Svendson, Tove 45
Pettersen, Kari

This example finds the number of persons with a value in the "Age" field in the "Persons" table:

SELECT COUNT(Age) FROM Persons

Result:

2

The COUNT(column) function is handy for finding columns without a value. Note that the result is one less than the number of rows in the original table because one of the persons does not have an age value stored.

SQL AVG Function

The AVG function returns the average value of a column in a selection. NULL values are not included in the calculation.

Syntax

SELECT AVG(column) FROM table

Example

This example returns the average age of the persons in the "Persons" table:

SELECT AVG(Age) FROM Persons

Result

32.67

Example

This example returns the average age for persons that are older than 20 years:

SELECT AVG(Age) FROM Persons WHERE Age>20

Result

39.5