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, email@example.com Daniel, Nguyen, firstname.lastname@example.org Lynda, Dina, email@example.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, firstname.lastname@example.org Titinan, Bloom, email@example.com Vivian, Bloom, firstname.lastname@example.org Binh, Nguyen, email@example.com Daniel, Nguyen, firstname.lastname@example.org Lynda, Dina, email@example.com Yvon, Dina, firstname.lastname@example.org
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.