Server-side sorting in MySQL

When sorting results, MySQL allows for two types of ordering: by group and by item. Below, we look at each in turn in terms of their respective clauses:

  • GROUP BY
  • ORDER BY

Despite their syntactical similarities, each has its distinct applications and limitations.

GROUP BY

The GROUP BY clause is used to organize results according to the structure of the data returned. The clause itself is appended to the end of the SELECT statement. The basic syntax is:

SELECT <column name(s)> FROM <table name> GROUP BY <column name as key>;

As a rule, GROUP BY cannot be used in conjunction with a universal quantifier ('*') instead of the column name(s). Rather, the column used as the key for sorting must be stated among the column names indicated ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.