Home/Curriculum/Lesson 9
Beginner3 min read

Summarizing Data with Aggregate Functions

Understanding Data Summarization

Database tables usually contain large amounts of data. Often, you do not need to retrieve the actual raw data itself. Instead, you require a summary of that data for business analysis or reporting. For instance, you might need to know the total number of registered customers, the sum of all sales for a particular month, or the most expensive product currently in your inventory.

SQL provides special functions, called aggregate functions, designed specifically for this purpose. Aggregate functions operate on a defined set of rows and return a single, summarized value. Calculating these metrics directly at the database level is highly efficient and significantly faster than downloading the raw records to calculate the summaries in a separate application.

Counting Records with COUNT

The COUNT function determines the number of rows that match a specific criteria. It is one of the most frequently used aggregate functions and can be applied in two primary ways.

When you use the asterisk wildcard, COUNT(*), the database counts every single row in the result set, regardless of whether the columns contain actual data or missing values.

POSTGRESQL CODE SNIPPET
SELECT COUNT(*)
FROM customers;

Alternatively, you can specify a distinct column name. When you specify a column, such as COUNT(email_address), the database counts only the rows where that specific column has a known, non-null value, ignoring any rows where the data is missing.

Calculating Totals with SUM

While counting rows tells you the volume of your data, you frequently need to calculate the total mathematical value of those records. The SUM function adds together all the values within a specified numeric column and returns a single grand total.

POSTGRESQL CODE SNIPPET
SELECT SUM(quantity)
FROM inventory;

The SUM function operates exclusively on numeric data types, such as integers or decimals. The database automatically ignores any NULL values during its calculation, ensuring that missing data does not break the mathematical operation.

Finding Averages with AVG

To understand the typical behavior or standard metric within your dataset, you use the AVG function. This function calculates the arithmetic mean of a specified numeric column. It internally sums all the valid values in the column and divides that total by the number of non-null rows.

POSTGRESQL CODE SNIPPET
SELECT AVG(price)
FROM products;

Because it ignores NULL values entirely in both the sum and the row count, your resulting average might be skewed if your dataset contains a high volume of incomplete records. You must ensure your data is clean before relying on average calculations for reporting.

Finding Extremes with MAX and MIN

To identify the lowest or highest data points in a table, SQL provides the MIN and MAX aggregate functions. MIN scans a specific column and returns the absolute lowest value, while MAX extracts the highest value.

POSTGRESQL CODE SNIPPET
SELECT MIN(price), MAX(price)
FROM products;

These functions are highly versatile across different data types. For numerical data, they find the highest or lowest numbers. For date columns, they identify the oldest or most recent timestamps. For text columns, they return the values that sort first or last in standard alphabetical order.

Using Aliases with Aggregates

When you utilize aggregate functions, the resulting column names generated by the database are often difficult to read, defaulting to strings like SUM(price) or COUNT(*). To present a clean, professional result set, you should always use the AS keyword to assign a column alias to your aggregated fields.

POSTGRESQL CODE SNIPPET
SELECT COUNT(*) AS total_customers, AVG(age) AS average_age
FROM customers;

Apply these aggregate functions to summarize your datasets and discover high-level trends within your tables before moving on to data grouping.

Advertisement