Learn SQL
GROUP BY - more use cases

GROUP BY with ordering

Let’s revisit the original query we wrote to count the number of students in each grades.

Let’s say we want to get the same results sorted in the descending order of count of students in each grade.

For this case the aggregate function that we use to get individual result from a group (in this case, COUNT) can also be used in ORDER BY clause.

Additionally, the same query can be written like this:

If you notice, the alias used for the aggregate function itself can be used in the ORDER BY clause as well, in place of the aggregate function itself.