Support our 100% FREE Projects: Donate Now OR Sponsor Now


  

SQL GROUP BY

SQL Group By

In this SQL tutorial I will cover SQL GROUP BY clause and give examples of how to use it. I will use SQL Server 2008 R2, but the examples in this tutorial should work with all SQL Server versions and some other databases. I will start with short and easy examples and explanations and move on to common group by questions that are slightly more involved.

 

 

Content:

 

SQL Group By Syntax

Below is group by syntax in the simpliest form:

SELECT FieldName

FROM TableName

GROUP BY FieldName

 

SQL Group By Example

Group By allows us to group rows together and additionally perform aggregate calculation which works on a group of rows. Let me first give you a simple example of what group by does using one field. I will use Customer table and group all customers by Country in other words I want to show all unique values that exist in Country Field, that is how group by works. See below example code with results (before and after)

SELECT *

from [dbo].[CUSTOMER]

 

SQL Group By clause

Group by example with Aggregate Function

You can group by fields in tables but this is rarely used on its own and very often that is combined with aggregate function. Aggregate function gets access to grouped rows. So if you group 3 rows into 1 then you can perform aggregate calculation like count which in this case would give result 3. You can also use other aggregate functions like SUM, MAX, MIN, AVG and several other ones. See below example that is the same as previous ones but now with Count aggregate function.

 

SELECT c.Country

,COUNT(*) as CustomerInEachCountry

,MAX(CustomerID) as TheHighestID

FROM dbo.Customer as c

GROUP BY c.Country


SQL Group by with aggregate function

Compare this example to previous one and you should understand how group by and aggregate function work using SQL. The count is performed on grouped rows so UK appear in results only once as it was grouped but there are 3 rows that contain country UK so when aggregate function is used it is applied to all rows "inside" the group therefore the result is 3. I also used MAX function with CustomerID field to get the highest customerID in each group.

Take care
Emil

 

Share: Share on FacebookShare on Google PlusTweet it
Comments Add Comment
Name: anni
Comment:

the images are not visible


Date Posted: 10/03/2012 5:53:01 AM

Name: anni
Comment:

i am talking abt the 1st  image


Date Posted: 10/03/2012 6:05:50 AM

Name: deepali
Comment:

yes, me too cant see the first image properly

 


Date Posted: 03/04/2012 8:53:02 AM

Name: Emil
Comment:

it is difficult to debug it for me. I can see 2 pictures in total; one group by and second ones with count and max. Maybe you think there should be an image but there isn't one ;)

I would appreciate some screenshots! (use contact us and I will send you my email address)


Date Posted: 04/04/2012 10:28:19 PM

Name: rajitha
Comment:

Thanku for your commands...It is very useful to me.......


Date Posted: 30/10/2012 12:19:19 PM

Name: Ashish kashyap
Comment:

good example keep it up.


Date Posted: 10/03/2013 7:24:34 PM

Name: Pavithra
Comment:

Well.Enjoying a lot  with SQL


Date Posted: 19/05/2013 10:22:47 AM

Name: Database Breaker
Comment:

Use full......can anyone be a master of SQL? 

 


Date Posted: 24/05/2013 10:26:25 PM

Share your thoughts, questions and suggest improvements:
Add Comment

You found us! Below are 50 most popular searched keywords

created at TagCrowd.com

Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.