Docy

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

Estimated reading: 1 minute 897 views

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;
				
			

COUNT() Example

The following SQL statement finds the number of products:

				
					SELECT COUNT(ProductID)
FROM Products;
				
			

AVG() Example

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

				
					SELECT AVG(Price)
FROM Products;
				
			

SUM() Example

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

				
					SELECT SUM(Quantity)
FROM OrderDetails;
				
			

Leave a Comment

Share this Doc
CONTENTS