SQL Aggregate Functions – SUM, COUNT, MAX, MIN, AVG

Last modified date:

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.

Example

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:

SELECT
  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

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. 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 (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Functions

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. 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 (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.