SQL SELECT COUNT, SUM, AVG

The SQL SELECT COUNT, SUM, and AVG syntax

The general COUNT syntax is:

  1. SELECT COUNT(columnname)
  2. FROM tablename

The general SUM syntax is:

  1. SELECT SUM(columnname)
  2. FROM tablename

The general AVG syntax is:

  1. SELECT AVG(columnname)
  2. FROM tablename

 


 

SQL SELECT COUNT, SUM, and AVG Examples

Problem: Find the number of customers

  1. SELECT COUNT(Id)
  2. FROM Customer

Results:

Count
91

 


Problem: Compute the total amount sold in 2013

  1. SELECT SUM(TotalAmount)
  2. FROM [Order]
  3. WHERE YEAR(OrderDate) = 2013

Results:

Sum
658388.75

 


Problem: Compute the average size of all orders

  1. SELECT AVG(TotalAmount)
  2. FROM [Order]

Results:

Average
1631.877819

댓글 남기기