Understanding MySQL GROUP BY and ORDER BY

Retrieving Information from database table is most basic and necessary task in any project,but fetched data is of no use if it is not organised and easily understandable.SQL’s ORDER BY ,GROUP BY are few promising functions in sql qhich makes the data well manages and organised.

So today PHPHurdles is presenting a short but very useful tutorial for our new developers who have recently started their carrer in PHP Understanding MySQL GROUP BY and ORDER BY which will help them in retrieving data from database in a well managed and organised way.

Before moving ahead with tutorial let’s get understand what ORDER BY and GROUP BY Clause do.
GROUP BY : GROUP BY clause finds same result and clumps it together.
ORDER BY : ORDER BY clause returns the data in a order according to the query for example in asscending or descending based on some column.

Let’s discuss and understand about these MySql clauses with a example
We will first start and create a table members that gives us the name and postal codes of our members.
Fields: member_id, name, postalcode
Type: int, varchar(100), int
Extra: auto_increment

Now let’s understand ORDER BY clause:
FROM table_members

This Query will fetch all the results from the database table in a alphabetical order of name which is a more organised way rather than fetching results without ordering them alphabetically.

There can be a situation where we want the data to be fetched in a order where names are in reverse alphabetical order.then we can use DESC keyword with the same query
FROM table_members

This is how can fetch data based on different conditions with ORDER BY

Now lets understand about GROUP BY:
There can be a situation where we have to summarize data in groups.let’s take a situation where we want to count number of people with the same postal code or we want to count the number of members with the same name, then GROUP BY clause is required to retrieve data.
Lets see how GROUP BY clause is used to fetch data
SELECT postalcode, count(name)
FROM table_members
GROUP BY postalcode

This will give number of members in each postal code.and this is how GROUP BY clause returns a summarized information.

So this is it from the tutorial Understanding GROUP BY and ORDER BY clause Hope this will help all those developers who are finding difficulty in fetching data in a order or according to some condition.
You can also read our tutorial UNDERSTANDING SQL JOINS USING PHP on SQL joins.
and don’t forget to comment us or mail regarding any of your query. our team is always available to help you.
So guys work hard , defeat every difficulty
Because Hurdles aren’t really Hurdles.


  1. Jade Gardner said:

    Thanks for the information. As a developer i was searching for such information from a long time and finally i got it. Keep sharing such a good work.

    August 11, 2017
    • Saurabh Dubey said:

      Thank You Jade

      August 12, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *