Earn income with your data and sql skills
We're building the largest freelancing marketplace for people like you.

# SQL SELECT COUNT, SUM, and AVG

COUNT, SUM, and AVG are aggregate functions.

COUNT returns a count of the number of data values.

SUM returns the sum of the data values.

AVG returns the average of the data values.

For a list of all aggregate functions see our aggregate functions reference.

### #

Get the total number of products.

``````SELECT COUNT(Id) AS 'Product Count'
FROM Product
``````
Result
Product Count
78

## Syntax

COUNT syntax.

```SELECT COUNT(column-name)
FROM table-name
```

SUM syntax.

```SELECT SUM(column-name)
FROM table-name
```

AVG syntax.

```SELECT AVG(column-name)
FROM table-name
```

## SELECT COUNT

Problem: Get the total number of customers.
``````SELECT COUNT(Id) AS 'Customer Count'
FROM Customer
``````
Result
Customer Count
91

## SELECT SUM

Problem: Calculate the total sales in 2013.
``````SELECT SUM(TotalAmount) AS 'Total Sales'
FROM [Order]
WHERE YEAR(OrderDate) = 2013
``````
Result
Total Sales
658388.75

## SELECT AVG

Problem: Calculate the average size of all orders.
``````SELECT AVG(TotalAmount) AS 'Avg Order'
FROM [Order]
``````
Result
Avg Order
1631.877819

## You may also like

Last updated on Dec 21, 2023

Earn income with your data and sql skills