Lesson 8: Aggregate Functions

So far, we've learned how to retrieve individual rows, filter data, and perform calculations with expressions. But what if you want to answer questions like "How many orders do we have?" or "What's our total revenue?" These questions require summarizing data across multiple rows, which is where aggregate functions come in.

Aggregate functions perform calculations on a set of rows and return a single result. Think of them as powerful calculators that can process entire columns of data at once.

🧮 What are Aggregate Functions?

Aggregate functions take multiple input values and combine them into a single output value. Instead of showing you every individual row, they give you summary information about your entire dataset.

Common business questions aggregate functions can answer:

  • How many? - Count of records
  • How much total? - Sum of values
  • What's the average? - Mean of numbers
  • What's the highest/lowest? - Maximum and minimum values

Sample Sales Data:

order_id | customer_id | order_amount | order_date
---------|-------------|--------------|------------
1        | 101         | 250.00       | 2024-01-15
2        | 102         | 175.50       | 2024-01-16
3        | 103         | 89.99        | 2024-01-17
4        | 101         | 320.00       | 2024-01-18
5        | 104         | 99.95        | 2024-01-19

🔢 The Five Essential Aggregate Functions

📊 COUNT() - Counting Rows

Returns the number of rows in your result set.

Basic Syntax:

SELECT COUNT(*) FROM table_name;
SELECT COUNT(column_name) FROM table_name;

Examples:

-- Count total number of orders
SELECT COUNT(*) AS total_orders FROM sales;
-- Result: 5

-- Count orders with non-NULL customer_id
SELECT COUNT(customer_id) AS orders_with_customers FROM sales;
-- Result: 5

💡 Key Difference: COUNT(*) counts all rows, including NULLs. COUNT(column_name) counts only non-NULL values in that specific column.


➕ SUM() - Adding Up Values

Returns the total sum of a numerical column.

Basic Syntax:

SELECT SUM(column_name) FROM table_name;

Example:

-- Calculate total revenue
SELECT SUM(order_amount) AS total_revenue FROM sales;
-- Result: 935.44

📈 AVG() - Finding Averages

Returns the average value of a numerical column.

Basic Syntax:

SELECT AVG(column_name) FROM table_name;

Example:

-- Calculate average order value
SELECT AVG(order_amount) AS average_order_value FROM sales;
-- Result: 187.09

🔽 MIN() - Finding Smallest Value

Returns the smallest value in a column.

Basic Syntax:

SELECT MIN(column_name) FROM table_name;

Example:

-- Find lowest order amount
SELECT MIN(order_amount) AS lowest_order FROM sales;
-- Result: 89.99

🔼 MAX() - Finding Largest Value

Returns the largest value in a column.

Basic Syntax:

SELECT MAX(column_name) FROM table_name;

Example:

-- Find highest order amount
SELECT MAX(order_amount) AS highest_order FROM sales;
-- Result: 320.00

🏷️ Using Aliases with Aggregate Functions

Just like with expressions, always use meaningful aliases for your aggregate results:

SELECT COUNT(*) AS total_records,
       SUM(order_amount) AS total_revenue,
       AVG(order_amount) AS average_order_value
FROM sales;

Result:

total_records | total_revenue | average_order_value
--------------|---------------|--------------------
5             | 935.44        | 187.09

🔗 Multiple Aggregates in One Query

You can use several aggregate functions in a single query to get a complete summary:

SELECT COUNT(*) AS total_orders,
       MIN(order_amount) AS lowest_order,
       MAX(order_amount) AS highest_order,
       AVG(order_amount) AS average_order,
       SUM(order_amount) AS total_revenue
FROM sales;

Result:

total_orders | lowest_order | highest_order | average_order | total_revenue
-------------|--------------|---------------|---------------|---------------
5            | 89.99        | 320.00        | 187.09        | 935.44

🔍 Combining Aggregates with WHERE

You can filter data before aggregating using the WHERE clause:

-- Get statistics for orders over $100
SELECT COUNT(*) AS large_orders,
       SUM(order_amount) AS large_order_revenue,
       AVG(order_amount) AS large_order_average
FROM sales
WHERE order_amount > 100;

Result:

large_orders | large_order_revenue | large_order_average
-------------|---------------------|--------------------
3            | 745.50              | 248.50

💡 Important: The WHERE clause filters rows before the aggregate function processes them.


❓ Important Notes About NULL Values

Understanding how aggregate functions handle NULL values is crucial:

  • Aggregate functions ignore NULL values (except COUNT(*))
  • COUNT(*) counts all rows including NULLs
  • COUNT(column) counts only non-NULL values in that column
  • SUM, AVG, MIN, MAX skip NULL values entirely

Example with NULL values:

-- Sample data with NULL
-- order_amount: 100, 200, NULL, 300

SELECT COUNT(*) AS all_rows,              -- Result: 4
       COUNT(order_amount) AS non_null,   -- Result: 3
       SUM(order_amount) AS total,        -- Result: 600
       AVG(order_amount) AS average       -- Result: 200
FROM sales_with_nulls;

🎯 When to Use Aggregate Functions

Aggregate functions are perfect for:

  • Business Reports - Total sales, average prices, customer counts
  • Data Analysis - Finding trends and patterns in your data
  • Data Quality - Counting missing values or detecting outliers
  • Summary Statistics - Quick data overview and KPIs
  • Dashboard Metrics - Key performance indicators

Real-world scenarios:

-- Monthly sales summary
SELECT COUNT(*) AS orders_this_month,
       SUM(order_amount) AS monthly_revenue,
       AVG(order_amount) AS avg_order_value,
       MAX(order_amount) AS largest_sale
FROM sales
WHERE order_date >= '2024-01-01';

📊 Real-World Examples

Instead of looking at individual rows, aggregate functions let you answer:

-- "How many customers do we have?"
SELECT COUNT(*) AS total_customers FROM customers;

-- "What's our total revenue?"
SELECT SUM(order_amount) AS total_revenue FROM sales;

-- "What's the average order value?"
SELECT AVG(order_amount) AS avg_order_value FROM sales;

-- "What's our highest sale?"
SELECT MAX(order_amount) AS highest_sale FROM sales;

-- "What's our smallest order?"
SELECT MIN(order_amount) AS smallest_order FROM sales;

💡 Did You Know? Aggregate functions are incredibly efficient! Instead of retrieving thousands of rows and calculating totals in your application, the database does all the work and returns just one number. This saves both time and memory, especially with large datasets. Different databases might have additional aggregate functions, but COUNT, SUM, AVG, MIN, and MAX are supported by all major SQL databases.


✅ Key Takeaways

  • Aggregate functions summarize data across multiple rows into single values
  • COUNT(*) counts all rows, while COUNT(column) counts only non-NULL values
  • SUM and AVG work only with numerical data types
  • MIN and MAX work with numbers, text, and dates
  • Always use meaningful aliases for aggregate results to improve readability
  • Use WHERE to filter data before aggregating for targeted analysis
  • Aggregate functions ignore NULL values (except COUNT(*))
  • You can combine multiple aggregates in a single query for comprehensive summaries
  • Aggregate functions are efficient and should be used instead of application-level calculations
  • Understanding NULL handling is crucial for accurate aggregate calculations

📝 Exercise Overview

In this lesson's exercises, you'll work with a Sales table containing transaction data. You'll practice counting records, calculating totals, finding averages, and identifying minimum and maximum values.

These practical scenarios will help you understand how aggregate functions provide valuable business insights from raw data. You'll work with:

  • Counting total records and specific conditions
  • Calculating revenue totals and averages
  • Finding highest and lowest values
  • Combining multiple aggregates for comprehensive analysis
  • Using WHERE clauses to filter data before aggregating

Take your time to understand how these functions transform rows of data into meaningful business metrics!


Ready to practice SQL? Try our coding challenges →