SQL GROUP BY

Are you a paid member?

Log in or Become a member
1 month unlimied use access pass for $25
or $10/month (yearly subscription)





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)

Are you a paid member?

Log in or Buy Membership 1 month unlimied use access pass for $25
or $10/month (yearly subscription)





Did you find this page useful? +2  |  -0
(2 Votes)