GROUP BY clause in sql server

SQL Server group by  clause is an essential part of when you are working with SQL. It’s a part of the SQL training also. Here we are going to describe the processes of how to do it with help of Group by SQL Command.



GROUP BY Clause(Group by Clause in Oracle,Group by Clause in MySQL,Group by Statement in SQL)

The SQL Group BY Clause is utilized to output a row across specified column values. It’s generally utilized in conjunction with combined functions like add or Count to summarize values. In SQL teams are distinctive combinations of fields. Instead of returning each row in a table, once values are sorted, only the distinctive combinations are returned.

The group BY Clause is more to the SQL Statement once after the WHERE Clause.There are some of things to notice. First, the columns we would like to summarize are listed, separated by commas, within the group BY clause. Second, this same list of columns should be listed within the chosen statement; otherwise the statement fails.

When this statement is run, not each filtered row is returned. But still there is a big difference, The DISTINCT modifier end at outputting single combination of rows, but with the group BY statement, we can calculate values that are supported by the original filtered rows for every distinctive combination. COUNT is an example of a blend of perform, these are what gives the GROUP BY statement its special worth.


Aggregate Functions

Some functions, like SUM, are used to perform calculations on a bunch of rows; these are known as aggregate functions. In most cases these functions treat a group of values that are outlined using the group BY clause. Once there isn’t a group BY clause, it’s usually understood the aggregate perform applies to all filtered results.

Some of the foremost common combination functions include:

  • AVG(expression) Calculate the average of the expression.
  • COUNT(expression) Count occurrences of non-null values returned by the expression.
  • COUNT(*) Counts all rows within the specified table.
  • MIN(expression) Finds the minimum expression value.
  • MAX(expression) Finds the maximum expression worth.
  • SUM(expression) Calculate the total of the expression.

These functions can be used on their own on in combination with the GROUP BY clause. They operate across the entire table; but, when used with GROUP BY, their calculations are “reset” every time and the grouping changes. In this way they act as subtotals.


General Syntax of an Aggregate Function

While using the aggregate function you can calculate the result on all values or separate values.

Using Aggregate Functions with GROUP BY

In simple words, to aggregate is making whole from individual parts. Aggregate functions are those functions that work on more than one row to go back to a result.


The SUM function totals up the values that are returned; in the same way AVG calculates the average.

We can even type by the total to get the top orders first

In similar manner we can calculate the average order detail amount as it is calculated as the sum of the sample divided by the sample count, then use AVG.

Below are some important points to remember:

  1. An aggregate function can review an expression such as SUM(A + B)
  2. You should assume name the aggregate functions, so that the column names are significant.
  3. When working with aggregate functions and GROUP BY, IT is at times is easy to think about the details first, write a simple SELECT statement, check the results, then add in the consider stuff.

Example group by SQL query


Consider a table Result

Column namedata typeAllow null
SubjectChar (10)no



To calculate sum of total marks

Select Rollnumber, SUM (Marks) from result group by RollNumber



To calculate average of Subject

Select Subject, AVG (Marks) from result group by Subject




The COUNT function is used when you want to know the number of records that exist in a table or within a group.  COUNT (*) will count each record in the grouping; while COUNT counts every record where expression’s result is not null.  You can also use Distinct with COUNT to look for the amount characteristic values within a group.

Select Count (*) from result

ANS =4;


Select subject ,count (subject) from result group by subject







Using MIN and MAX will enable you to find the smallest and largest values, within a table or group. You can also find the MIN or MAX value of a calculation.

Calculate minimum marks in respective subject

Select subject, MIN (Marks) from result group by subject




Calculate Maximum marks in respective subject

Select subject, MAX (Marks) from result group by subject







Having group by Clause in SQL

The Having Clause in SQL is used to filter groups according to the outcome of the aggregate functions.  This makes it likely to solve problems like selecting all orders that have more than two orders detail lines.Group by clause with having mostly used to find duplicate records.

Having clause with MysQL example

Calculate duplicate employee Id

Select EmpId,count(*) Duplicate Record from Table group by EmpId having Count(*) > 1

group by clause
group by clause

Here I will explain various group by clause syntax and example.See SQL queries interview questions and answers

🏅 Login Guide|✅US Etrade Tips And Tricks

Are you looking for some information related to Etrade login, or simply you want the guidance for Etrade login, or you just...

👉Best 2020 GUIDE For login|All about Merrick Bank Login Tips and Tricks

If you want to do a successful Merrick Bank Login and if you do not understand the complete step-by-step login processes,...

👉Everything about Avid Login|✅Best Guidelines|💯For Amazing 2020

Are you looking for Avid login guidelines, or you just want the information related to Avid, or else you have some query...

👉Everything about MyVanillaDebitCard- Application, Activation & Benefits-[✅Updated 2020]

One of the major features MyVanillaDebitCard provides is that it makes cash management very simple for the...

🏅BAML Cards|✅Best Bank Of America Merrill Lynch Visa Reward Card Review 2020 {GUIDE}

Are you doubtful about the Bank Of America Merrill Lynch Card? Do you need a complete guide on the reward card? The...

Quick Tips About Intel Iris Graphics 6100 | Updated

Are you looking for the information related to Intel Iris Graphics 6100, or you just looking for Intel information to keep yourself...

Download Free Spotify Premium Apk | Spotify Mod Apk Free

Are you looking for a free spotify premium apk? If the answer the question is a yes, then you're at the right place. We've...

How To Download Xender For PC Windows 7,8,10 and Mac

Are you looking for information related to Xender application, or you looking for how to download Xender for PC, or you just...

✅Best Credit One Bank Login|🔒Secure Registration Guide|2020

Are you in search of a reliable Credit One Bank login and registration guide? If yes, then this is the place to...

🏅Everbank Login|✅Best Guidelines|For Amazing 2020

Are you looking for information about EverBank, or you just simply looking for the guidelines to log into the Everbank financial...

Related Stories


  1. An impressive share! I have just forwarded this onto a
    colleague who had been doing a little research on this. And he actually ordered
    me dinner due to the fact that I stumbled upon it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to talk about this matter here on your blog.

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox