what is the difference between group and having
give an example with query and sample output

Answers were Sorted based on User's Feedback



what is the difference between group and having give an example with query and sample output..

Answer / krishna murari chaubey

You Have two table person and friend
create table person(id int,pname varchar(20),gender varchar(3))
insert into person(id,pname,Gender)values(1,'krishna','m')
insert into person(id,pname,Gender)values(2,'Radha','f')
insert into person(id,pname,Gender)values(3,'Anamika','f')
insert into person(id,pname,Gender)values(4,'raj','m')
insert into person(id,pname,Gender)values(5,'suhani','f')
insert into person(id,pname,Gender)values(6,'ravi','m')

create table friend(id int,fid int)
insert into friend(id,fid)values(1,2)
insert into friend(id,fid)values(1,3)
insert into friend(id,fid)values(1,5)
insert into friend(id,fid)values(2,3)
insert into friend(id,fid)values(1,4)
insert into friend(id,fid)values(1,6)
insert into friend(id,fid)values(6,2)
insert into friend(id,fid)values(6,3)
insert into friend(id,fid)values(3,2)
insert into friend(id,fid)values(3,2)
insert into friend(id,fid)values(3,1)

find person who is male and having more than two female friend
Asnswer : -

select id,count(fid) as numberOfFemaleFriend from friend where fid in(select id from person where gender='f')
and id in (select id from person where gender='m' )
group by id having count(fid) >2

OR You can use Inner Join


select f.id,p.pname,count(f.fid) as numberOfFemaleFriend
from person p
inner join friend f
on p.id=f.id and p.gender='m' and f.fid in
(select id from person where gender='f')
group by f.id,p.pname having(count(f.fid)>2)

Is This Answer Correct ?    4 Yes 0 No

what is the difference between group and having give an example with query and sample output..

Answer / veeresh kethari

Where: 'WHERE' will filter the data before grouping

Having:'HAVING' will filter,after the grouping the data.

Is This Answer Correct ?    3 Yes 0 No

what is the difference between group and having give an example with query and sample output..

Answer / soorai ganesh

create table lovepair (boyfrndname varchar(10),girlfrndname
varchar(10))

insert into lovepair values ('BF1','GF1')
insert into lovepair values ('BF1','GF2')
insert into lovepair values ('BF1','GF3')
insert into lovepair values ('BF2','GF1')
insert into lovepair values ('BF2','GF2')
insert into lovepair values ('BF3','GF3')
insert into lovepair values ('BF3','GF3')
insert into lovepair values ('BF3','GF3')

-- Here BF1 have 3 Girl Friends. like the BF2 and BF3 have
2 and 3 Girl Friends Simultaneously.

-- Here is the query for, how many girls friends each boys
have ???? USING group by

SELECT boyfrndname, COUNT (*)
FROM lovepair
GROUP BY boyfrndname

// Here another qry for who have more than 2 girl
friends ?? USING GroupBy and Having .

// Having is used for applying some condition in Aggregate
function

SELECT boyfrndname,COUNT(*)
FROM lovepair
Group BY boyfrndname
having count(*)>2


--- Now u clear...........

Is This Answer Correct ?    4 Yes 2 No

what is the difference between group and having give an example with query and sample output..

Answer / dinesh sharma

Group by is used To Group Result According To Group Name
Having Is Always Is Used With Group by Clause.
Having Contain Aggregate Function or Simple Sub Query .
as Show In Above Example

Is This Answer Correct ?    2 Yes 0 No

what is the difference between group and having give an example with query and sample output..

Answer / krishna murari chaubey

You Have two table person and friend
create table person(id int,pname varchar(20),gender varchar(3))
insert into person(id,pname,Gender)values(1,'krishna','m')
insert into person(id,pname,Gender)values(2,'Radha','m')
insert into person(id,pname,Gender)values(3,'Anamika','m')
insert into person(id,pname,Gender)values(4,'raj','m')
insert into person(id,pname,Gender)values(5,'suhani','m')
insert into person(id,pname,Gender)values(6,'ravi','m')

create table friend(id int,fid int)
insert into friend(id,fid)values(1,2)
insert into friend(id,fid)values(1,3)
insert into friend(id,fid)values(1,5)
insert into friend(id,fid)values(2,3)
insert into friend(id,fid)values(1,4)
insert into friend(id,fid)values(1,6)
insert into friend(id,fid)values(6,2)
insert into friend(id,fid)values(6,3)
insert into friend(id,fid)values(3,2)
insert into friend(id,fid)values(3,2)
insert into friend(id,fid)values(3,1)

find person who is male and having more than two female friend
Asnswer : -

select id,count(fid) as numberOfFemaleFriend from friend where fid in(select id from person where gender='f')
and id in (select id from person where gender='m' )
group by id having count(fid) >2

OR You can use Inner Join


select f.id,p.pname,count(f.fid) as numberOfFemaleFriend
from person p
inner join friend f
on p.id=f.id and p.gender='m' and f.fid in
(select id from person where gender='f')
group by f.id,p.pname having(count(f.fid)>2)

Is This Answer Correct ?    2 Yes 0 No

what is the difference between group and having give an example with query and sample output..

Answer / pawan378

in addition to the above correct answers ..

Having Clause allows to add conditions using functions

Ex: COUNT(*) > 2

at on the same phrase of the query. this will be basically
to avoid sub queries.

Is This Answer Correct ?    0 Yes 0 No

what is the difference between group and having give an example with query and sample output..

Answer / manub22

- GROUP BY clause works on the rows returned by a SELECT Query. This clause summaries identical rows into a single/distinct group and returns a single row with the summary for each group, by using appropriate Aggregate function in the SELECT list, like COUNT(), SUM(), MIN(), MAX(), AVG(), etc.

- HAVING clause works as a Filter on top of the Grouped rows returned by the Query containing the GROUP BY clause. This clause cannot be replaced by a WHERE clause and vice-versa.

Check the difference b/w GROUP BY & HAVING, link: http://sqlwithmanoj.com/2015/05/23/sql-basics-difference-between-where-group-by-and-having-clause/

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

how would you write a sql query to compute a frequency table of a certain attribute involving two joins? What changes would you need to make if you want to order by or group by some attribute? What would you do to account for nulls?

0 Answers   Facebook,


How to give a user the option of importing Excel and a delimited text file into a SQL Server Database without manually using SQL DTS?

1 Answers   GE,


Can sql server be linked with other servers like oracle?

0 Answers  


IN Vs OR operator which is best to use sql server.

4 Answers  


Why I have to use stored procedures?

0 Answers   Cognizant,






What is replication and database mirroring?

0 Answers  


What are the benefits of using stored procedures over sql statements?

3 Answers   Ness Technologies,


What are data files?

0 Answers  


What are the main sources of data?

0 Answers  


wat new abt truncate in sql server

2 Answers   HCL, HP,


Why I am getting this error when renaming a database in ms sql server?

0 Answers  


hi, how to match retrieve the unmatched records from 2 tables in which we dont have any primary key. example : table1 has 1,2,3,4,5 and table2 has 1,2,3,4,5,6,7,8,9,10 and i want the numbers from 6 to 10 to be displayed and should not come as null. i need the numbers. please reply as soon as possible.

4 Answers  


Categories