Grouping Results

Use the GROUP BY clause to group the results by one or more table columns. Typically, a GROUP BY clause is used in conjunction with an aggregate expression such as COUNT, SUM, and AVG.

Note:

You can use the GROUP BY clause only if there exists an index that sorts the rows by the grouping columns.

For example, this query returns the average income of users, based on their age.

sql-> SELECT age, AVG(income) FROM Users GROUP BY age;
 +-------+-------------+
 | age   | AVG(income) |
 +-------+-------------+
 |  25   |  100000     |
 |  35   |  100000     |
 |  38   |  80000      |
 |  47   |  400000     |
 +-------+-------------+

4 rows returned