Group by with having in SQL server

Group by function in SQL server with Having Clause : Group by used to group the records based on the given column name and Having function acts like a function for the retrieved aggregate function


Creating the table

create table Temp_gro
(
USerID varchar(20),
Amount int
)

Inserting into the table

insert into Temp_gro values
('venki',1200),
('venki',1000),
('Ravi',200),
('Ravi',800),
('Charls',6000)

--select Query in Group By

select USerID,SUM(amount) from Temp_gro Group by userId

USerID
TotalAmount
Charls
6000
Ravi
1000
venki
2200

--Group by with Having Clause

select USerID,SUM(amount) as 'TotalAmount' from Temp_gro Group by userId having SUM(amount)< 4000


USerID
TotalAmount
Ravi
1000
venki
2200