O'Reilly logo

Microsoft® SQL Server® 2008 Step by Step by Mike Hotek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Aggregating Data

A simple aggregate will return a single value for the entire table. Although a small number of requirements can be satisfied by simply aggregating all rows within a table, many aggregation questions need to roll up data into analysis buckets, such as sales by month or defects by product line. The GROUP BY clause allows you to define the columns that are used to compute aggregate values.

The most common aggregate functions are:

  • COUNT. number of items

  • MIN. minimum value

  • MAX. maximum value

  • AVG. average value

  • SUM. total of all values

The following query returns the number of employees who hold a particular job title.

SELECT JobTitle, count(*)
FROM HumanResources.Employee
GROUP BY JobTitle

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required