Gossamer Forum
Home : General : Databases and SQL :

Explain GROUP BY

Quote Reply
Explain GROUP BY
Hi,
Could someone give me a basic explaination of what GROUP BY does? It appears to me to be similar to DISTINCT.
Does it just group together rows with the same value?
So in the following example -

SELECT colname1, colname2, colname3
FROM tablename
WHERE colname1 = something
GROUP BY colname1

does it just group together all the colname1 values that are the same and only return one result for that value???

What happens if I put two colnames in the GROUP BY like -

SELECT colname1, colname2, colname3
FROM tablename
WHERE colname1 = something
GROUP BY colname1, colname2

what does that do??

I have read through a lot of posts and tutorials but kind find a simple explaination.
Thank you.
Simon.
Quote Reply
Re: [jai] Explain GROUP BY In reply to
Hi,

Group by is used to group a result set into multiple sets for some calculation. It's primary use is for aggregator functions like: count(), sum(), min(), max(), etc. i.e.:

SELECT product, COUNT(*) FROM product_table GROUP BY product

would give you a list of each product and how many times it appears in the table. See:

http://www.mysql.com/...up_by_functions.html

for other common functions.

Cheers,

Alex
--
Gossamer Threads Inc.