SQL GROUP BY – Aggregating Data in Table

Last modified date:

GROUP BY clause is used to group data. In most of the cases, we talk of a situation when we need to make an aggregate operation in the table (sum of sales, number of records, average, etc.)

GROUP BY in SQL Script

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY

Syntax

SELECT [Column 1], [Column 2], Aggregate_function([Column 3]) AS Alias
FROM Table
GROUP BY Column 1, Column 2

We will use this clause in most cases of aggregate function usage. Adding operator OVER( PARTITION BY | ORDER BY | ROW | RANGE) is the only exception. This operator requires advanced skills and therefore we will not waste our time with it. The rule for beginners is that the clause is mandatory whenever there is any aggregate function in SELECT.

Example

Let’s have a table [FactInternetSales]. We will work with 2 fields:

  • Date [OrderDate]
  • Amount [SalesAmount]

group by example in sql

We want to aggregate the amount according to dates. The result will be in descending order. We will use:

SELECT
[OrderDate]
,SUM([SalesAmount]) AS [SalesAmount]
FROM [AdventureWorksDW2014].[dbo].[FactInternetSales]
GROUP BY [OrderDate]
ORDER BY [OrderDate] DESC

and the result:

group by clause in sql script

If we would miss out the clause, the result will be error: “Column ‘AdventureWorksDW2012.dbo.FactInternetSales.OrderDate’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.”

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 Basics

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.