Friday, July 14, 2023

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

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

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

COUNT() Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

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

AVG() Syntax

SELECT AVG(column_name)
FROM table_name
WHERE condition;

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

SUM() Syntax

SELECT SUM(column_name)
FROM table_name
WHERE condition;

Demo Database

Below is a selection from the “ProductSales” table in the sample database:

COUNT() Example

The following SQL statement finds the number of ProductSales:

SELECT COUNT(Product)
FROM ProductSales;

Note: NULL values are not counted.

AVG() Example

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

Example

SELECT AVG(ProductPrice)
FROM ProductSales;

Note: NULL values are ignored.

SUM() Example

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

Example

SELECT SUM(Quantity)
FROM OrderDetails;

Note: NULL values are ignored.

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...