SQL SELECT COUNT, SUM, AVG

  • SELECT COUNT returns a count of the number of data values.
  • SELECT SUM returns the sum of the data values.
  • SELECT AVG returns the average of the data values.
The definitive guide
for data professionals


Order today!
 See 2 min video



The SQL SELECT COUNT, SUM, and AVG syntax


The general COUNT syntax is:
SELECT COUNT(column-name)
  FROM table-name

The general SUM syntax is:
SELECT SUM(column-name)
  FROM table-name

The general AVG syntax is:
SELECT AVG(column-name)
  FROM table-name




SQL SELECT COUNT, SUM, and AVG Examples




Problem: Find the number of customers
SELECT COUNT(Id)
  FROM Customer


Results:

Count
91





Problem: Compute the total amount sold in 2013
SELECT SUM(TotalAmount)
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013


Results:

Sum
658388.75





Problem: Compute the average size of all orders
SELECT AVG(TotalAmount)
  FROM [Order]


Results:

Average
1631.877819