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

How can you manage sql azure security?

85


What are the steps to insert a table?

540


How we can refresh the view?

596


What are the differences between stored procedure and view in sql server?

625


What structure can you implement for the database to speed up table reads?

709






What stored by the msdb?

585


Can you name some of the dml commands in sql?

551


What is indexing and its types?

528


What do I need to start working with sql studio? : sql server management studio

591


Can group by and orderby be used together?

596


What is the difference between ddl,dml and dcl commands?

612


Explain the phases a transaction has to undergo?

558


What number aggregate functions are accessible there in sql?

590


When multiple after triggers are attached to sql table, how to control the order of execution?

531


Some queries related to SQL

588