SQL SELECT COUNT, SUM, and AVG

How are aggregate values calculated in SQL?

  • 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 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

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL SELECT COUNT, SUM,
and AVG Examples

Problem: Find the total number of customers.
SELECT COUNT(Id)
  FROM Customer
Result:
Count
91

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: Compute the total amount sold in 2013.
SELECT SUM(TotalAmount)
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013
Result:
Sum
658388.75

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: Compute the average size of all orders.
SELECT AVG(TotalAmount)
  FROM [Order]
Result:
Average
1631.877819


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas