Introduction
My previous article explaiined how to use Group by clause using Stored Procedure in Windows Forms application now I will describe in this article the Having Clause and how to use this clause in Windows in a Stored Procedure.
Having Clause
The having clause is used in combination with the Group By clause and the Select statement to filter the records that a Group By returns. The Having clause is generally used with the Group by clause. The Having clause applies to the records in a result set to filter the grouped data. The where clause does not work with the aggregate functions so we use the having clause instead of a where clause, for example sum, max, avg, min and so on.
Syntax:
Select col1, col2, col3,......col n, aggregateFunction (exp)
from table_name
where predicates
Group By col1, col2, col3,....Con
having Condition
Write the following procedure for creating a sample database and table in SQL Server:
Create database StudentResults
use StudentResults
create table StudentMarks
(
RollNo int,
FirstName varchar(max),
LastName varchar(max),
Subject varchar(max),
Marks int
)
Write the following procedure to insert values into the table columns:
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(1,'Pankaj','Lohani','Math',70)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(1,'Pankaj','Lohani','Physics',65)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(1,'Pankaj','Lohani','Chemestry',75)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(2,'Pravesh','Khanduri','Math',85)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(2,'Pravesh','Khanduri','Physics',60)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(2,'Pravesh','Khanduri','Chemestry',70)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(3,'Nimit','Joshi','Math',90)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(3,'Nimit','Joshi','Physics',60)
insert into StudentMarks (RollNo,FirstName,LastName,Subject,Marks) values(3,'Nimit','Joshi','Chemestry',75)
Write the following query to execute the table schema:
select * from StudentMarks

Having Clause using Aggregate Functions
- Using Sum Function
Select FirstName, Sum(Marks) as 'Total Marks'
from StudentMarks
Group By FirstName
Having Sum(Marks)>200

- Using Count Function
Select FirstName, Count(*) as 'No. of Subject'
from StudentMarks
Where Marks>60
Group By FirstName
Having Count(*)>1

- Using Max Function
Select FirstName , Max(Marks) as 'Hightest Marks'
from StudentMarks
Group By FirstName
Having Max(Marks)<88

Create the following Stored Procedure for students marks who have scored more than 200 marks:
Create proc ScoreHaving
as
begin
select FirstName, Sum(Marks) as 'Scored > 200'
from StudentMarks
Group by FirstName
Having Sum(Marks)>210
end
Create the following Stored Procedure for students average marks:
Create proc UsingWhereConditon
as
begin
SELECT FirstName, Avg(Marks) AS 'Average > 60'
FROM StudentMarks
where RollNo between 1 and 3
GROUP BY FirstName
HAVING Avg(Marks) > 60
end








Comments
Join the conversation! Your thoughts help the community grow.