Group By and Order By

In MySQL query, GROUP BY and ORDER BY will give you 2 different results.

Group By will return the unique values of a field while Order By will return number of records with alphabetical or numeric order based on what field you specify.

This is an example of query using Group By, you will see that the results will only list a unique lastname:

SELECT firstname, lastname, email_address FROM somedb.users GROUP BY lastname;

The results will be:
   Alice, Bloom, bloom@gmail.com
   Daniel, Nguyen, dnguyen@yahoo.com
   Lynda, Dina, dina@mail.com

While the query using Order By will return the records that have multiple lastname in alphabetical order, usually it’s  descendent by default unless you specify ASC for ascendent order.

SELECT firstname, lastname, email_address FROM somedb.users ORDER BY lastname;

The returned records will be:
   Alice, Bloom, bloom@gmail.com
   Titinan, Bloom, tibloom123@yahoo.com
   Vivian, Bloom, vivian@mail.com
   Binh, Nguyen, binhnguyen@microsoft.com
   Daniel, Nguyen, dnguyen@yahoo.com
   Lynda, Dina, dina@mail.com
   Yvon, Dina, yvon@adobe.com

In conclusion, use Group By when you want to get unique records of a field, and use Order By when you want to get a full lists without unique record restriction. Group By expression is useful when you need to apply statistic data. For example, if you want to count how many users in specific state are using your application. While Order By expression is useful when you need a list of user’s info listed in order for your marketing purposes.