Reprence Html CSS javascript PHP MySQL Bootsrap

MySQL COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criterion.



COUNT() Syntax

48

The AVG() function returns the average value of a numeric column.



AVG() Syntax

49

The SUM() function returns the total sum of a numeric column.


SUM() Syntax

50

Demo Database

Below is a selection from the "Products" table in the Northwind sample database:

51

COUNT() Example

The following SQL statement finds the number of products:


Note: NULL values are not counted.



AVG() Example

The following SQL statement finds the average price of all products:


Note: NULL values are ignored.



Demo Database

Below is a selection from the "OrderDetails" table in the Northwind sample database:

52


SUM() Example

The following SQL statement finds the sum of the "Quantity" fields in the "OrderDetails" table:


Note: NULL values are ignored.