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

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

COUNT() Syntax

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

SQL
SELECT COUNT(column_name)
FROM table_name
WHERE condition; 

AVG() Syntax

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

SQL
SELECT AVG(column_name)
FROM table_name
WHERE condition; 

SUM() Syntax

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

SQL
SELECT SUM(column_name)
FROM table_name
WHERE condition; 

COUNT() Example

The following SQL statement finds the number of products:

SQL
SELECT COUNT(ProductID)
FROM Products;

Note: NULL values are not counted.

AVG() Example

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

SQL
SELECT AVG(Price)
FROM Products;

Note: NULL values are ignored.

SUM() Example

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

SQL
SELECT SUM(Quantity)
FROM OrderDetails; 

Note: NULL values are ignored.