• 17. 11. 2019
  • Ing. Jan Zedníček - Data & Finance
  • 0

SQL aggregate functions AVG, COUNT, MAX, MIN, SUM are used the most. That is because we often need to add, make average or find the highest or lowest value.

It is mandatory to use GROUP BY clause anytime when we use aggregate function.  (except for operations over windows using OVER())

List of Aggregate Functions

AVG() – returns average value from the table
COUNT() – returns number of rows (values)
MAX() – returns maximal (highest) value
MIN() – returns minimal (lowest) value
SUM() – returns total of values

The combination of the aggregation function and the GROUP BY clause allows data to be aggregated (for example, in the GROUP BY article), this is important in order to reduce the atomicity of the data.


Let’s have a table with Sales [SalesAmount] and date of order [OrderDate].

aggregate functions example

We will apply all aggregate functions on the sales column. It can be seen well on this example:

  CAST(OrderDate AS DATE) AS OrderDate,
  SUM(SalesAmount)        AS Total_Sales,
  AVG(SalesAmount)        AS Average_Sales,
  COUNT(SalesAmount)      AS Count_Sales,
  MAX(SalesAmount)        AS Max_Sale,
  MIN(SalesAmount)        AS Min_Sale
FROM AdventureWorksDW2014.dbo.FactInternetSales
GROUP BY OrderDate
ORDER BY OrderDate;

sql aggregate functions example - sum,avg,min,max,count

Rate this post

Ing. Jan Zedníček - Data & Finance

My name is Jan Zedníček and I work as a freelancer. I have been working as a freelancer for many companies for more than 10 years.
I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums.
I am trying to summarize all my knowledge on this website not to forget them and to put them forward to anyone.

Leave a Reply

Your email address will not be published. Required fields are marked *