Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

I still dont understand GROUP BY

This may sound really stupid, but I don't understand the GROUP BY function in MYSQL and I have tried watching the video over and over again and I still don't understand it. Could someone explain how to use the GROUP BY function cause in the videos and everything I'm getting really confused.

hi this is my problem too ...

3 Answers

The best explanation I could find:

"The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups."

See this link (2nd example in particular)

Hope this helps :)

Here is an explanation with a working example database. The demonstration of the use is particularly helpful.

Your SQL query typically outputs to an array or similar storage method. You then want to use the output is some way, like displaying the information for the client. ORDER BY allows you to sort the data prior to putting it into the array. I am working on a project for a Cub Scout pack. Lets say that I want to list all the scouts in the pack by the highest badge they have completed. I would use order by to output the scouts in that order so I could easily place that data into the PHP for display. I could also sort by scouts that have not completed a certain badge, or by phone number, or by last name, or by first name, or any other data that is in the database. It saves having to manipulate a potentially large array.

Thank you for answering!! I understand ORDER BY now! However I still don't understand the GROUP BY method though, sorry!!

Sorry, I answered the wrong questions. :)