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
Can primary key be null?
What do you understand by intent locks?
What are transactions and its controls?
What is the distinction amongst delete and truncate?
What is the purpose of data source?
What is sql server used for?
What is join and name different type of joins?
Why I have to use stored procedures?
What are the types of normalization?
How do I run sql server 2014?
You are designing a strategy for synchronizing an SQL Azure database and multiple remote Microsoft SQL Server 2008 databases. The SQL Azure database contains many tables that have circular foreign key relationships?
What are the elements of dbms?
What is difference between index and primary key?
How secure is sql server database?
What is the difference between functions and scalar functions?