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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between upgrade and migration in sql server?

559


What is difference between after and before?

486


Give an example of SQL injection attack ?

551


What is sql stored procedure?

576


What are the different SQL Server Versions you have worked on?

523






Which is the best place or learning center for MS SQL?????In Bangladesh?????

1572


What are the restrictions while creating batches in sql server?

557


How to turn off warning messages during php execution?

511


Explain nested trigger in sql?

558


How many types of local tables are there in sql server?

488


What does select 1 mean?

508


How do I find the sql server database version?

504


What is the default fill factor value?

553


How do you size a resultset?

475


How use inner join in sql server?

589