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 SalesRepresentative, count( * )
FROM SalesOrders
GROUP BY SalesRepresentative

SalesRepresentative

Count()

129

57

195

50

299

114

467

56

667

54

690

52

856

55

902

47

949

53

1,142

57

1,596

53

The results of this query consist of one row for each SalesRepresentative ID number, containing the SalesRepresentative ID, and the number of rows in the SalesOrders 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.