Introduction:

In this tutorial we will learn how to use group by with an aggregate function in SQL Server with Example.

Description:

In SQL Server, we have group by clause for grouping the records of the database table according to our need in other words the SQL GROUP BY Clause is used to retrieve data grouped according to our need based on one or more columns. GROUP BY returns one record for each group. GROUP BY  Clause must be using an aggregate: COUNT, MAX, SUM, AVG, etc.

For Example:

Following example Without GROUP BY

SELECT S.id,S.Name,S.Mobile,S.address1,F.ammount AS amount FROM STUDENT AS S INNER 
JOIN Fee AS F ON S.id=F.StudentId

 

Result:

GROUP BY IN SQL SERVER


Above Example we use without group by  Select statement there are showing Name ,Mobile,Address1 Repeated. 

Following With GROUP BY Example: 

SELECT S.id,S.Name,S.Mobile,S.address1,SUM(F.ammount) AS amount FROM
STUDENT AS S INNER
JOIN Fee AS F ON S.id=F.StudentId
group by S.id,S.Name,S.Mobile,S.address1


Result:

GROUP BY IN SQL SERVER

Above Example we use group by select statement there are not showing Name, Mobile, Address1 Repeated.

Thanks.

 


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like