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
How do use Having,Group by,Group function in SQL?
What is the meaning of lock escalation and why/how to stop this? : sql server database administration
What is an etl file?
You have a table ‘test’ which is a copy of northwind employee table you have written a trigger to update the field ‘hiredate’ with the current date
What are the steps to take to improve performance of a poor performing query? : sql server database administration
How do I trace a query in sql server?
What different steps will a sql server developer take to secure sql server?
What is difference between getdate and sysdatetime in sql server 2008?
What to perform pattern match with the like operator?
What is meant by indexing?
Differentiate between sql temp table vs table variable?
What is merge join?
Can we join two tables without primary key?
How to create sub reports?
What is ssrs?