Using aggregate functions to obtain grouped data

In addition to providing information about an entire table, aggregate functions can be used on groups of rows.

Using an aggregate function on groups of rows

To list the number of orders for which each sales representative is responsible, type:

SELECT sales_rep, count( * )
FROM sales_order
GROUP BY sales_rep

sales_rep

count( * )

129

57

195

50

299

114

467

56

667

54

The results of this query consist of one row for each sales_rep ID number, containing the sales_rep ID, and the number of rows in the sales_order table with that ID number.

Whenever GROUP BY is used, the resulting table has one row for each different value found in the GROUP BY column or columns.