What is Group By clause ?
In this Article Group by clause use in Select query in Sql Server when you fetch data with collection on basis particular unique field then use Group by Clause. Group by clause with where condition and order by clause.Syntex
SELECT ColumnName FROM TableName WHERE condition GROUP BY ColumnName ORDER BY ColumnName
Example
SELECT COUNT(Id) as Counts,City FROM Tbl_Employee GROUP BY City
Create table Tbl_Employee with some field and group by city.
if you run query with GROUP BY Clause then you find group wise city records.
In case if you use Group By Clause then always use aggregate functions.If you not use aggregate function then face error.
You can see in example
Select All records in image- image
Group by clause on city -
Create table Tbl_Employee with some field and group by city.
if you run query with GROUP BY Clause then you find group wise city records.
In case if you use Group By Clause then always use aggregate functions.If you not use aggregate function then face error.
You can see in example
Select All records in image- image
Group by clause on city -
The HAVING Clause
The HAVING clause use in SQL because the WHERE keyword could not be used with aggregate functions.
Syntex
SELECT ColumnName, Aggregate Function(ColumnName)
FROM TableName
WHERE ColumnName operator value
GROUP BY ColumnName
HAVING Aggregate Function(ColumnName) operator value
0 comments :
Post a Comment