Part 6: Mastering Aggregate Functions

Part 6: Mastering Aggregate Functions | SQL Challenges & Capstone
SQL Business Logic & Data Handling Practice · Part 6 · Lecture Notes

Mastering Aggregate Functions

How to summarize and measure business performance using COUNT, SUM, AVG, MIN, and MAX — producing the metrics that power dashboards and executive reports.

Part 6 of 6100% Complete
COUNT()
SUM()
AVG()
MIN() / MAX()
8 Practice Questions

Learning Objectives

  • Use COUNT(*) to count the total number of rows in a table
  • Use SUM() to calculate the total of a numeric column β€” revenue, fees, and cumulative amounts
  • Use AVG() to calculate the mean value of a numeric column, always paired with ROUND() for financial output
  • Use MIN() and MAX() to identify the smallest and largest values in a column, including both in a single query
Click any section header to open or close it

Aggregate functions summarize and measure business data. Instead of returning one row per record, they collapse an entire column β€” or the entire table β€” into a single calculated value. COUNT, SUM, AVG, MIN, and MAX are the five core aggregate functions in SQL, and they are among the most used commands in business analytics.

In industry, analysts use aggregate functions to calculate total revenue, measure average transaction values, count customers or tickets, identify price extremes, and produce the summary metrics that appear in dashboards, executive reports, and KPI tracking.

The Five Core Functions

COUNT() counts rows. SUM() totals a numeric column. AVG() calculates the arithmetic mean. MIN() returns the smallest value. MAX() returns the largest value. All five collapse many rows into one output number.

Basic Aggregate Syntax
SELECT COUNT(column_name) FROM table_name; SELECT SUM(column_name) FROM table_name; SELECT AVG(column_name) FROM table_name;

COUNT(*) counts every row in the table, including rows with NULL values in some columns. COUNT(column_name) counts only rows where that specific column is not NULL. COUNT(*) is the more common form in business analytics β€” it answers “how many records are there?” regardless of which columns are populated.

COUNT(*) vs COUNT(column)

COUNT(*) counts all rows unconditionally. COUNT(column_name) skips rows where that column is NULL. For counting total records, always use COUNT(*). Use COUNT(column_name) only when you specifically want to exclude NULLs from the count.

Count All Users
SELECT COUNT(*) AS total_users FROM users;
Count All Support Tickets
SELECT COUNT(*) AS total_tickets FROM support_tickets;

SUM() adds up all values in a numeric column β€” producing totals like revenue, total subscription fees, or cumulative transaction amounts. AVG() divides the total by the count of non-NULL values, producing the arithmetic mean. Both are almost always wrapped in ROUND() when working with decimal values to keep financial figures clean.

Always ROUND Financial Outputs

SUM(amount) or AVG(unit_price) on decimal columns can produce many trailing decimal places. Wrapping in ROUND(…, 2) keeps financial output at exactly 2 decimal places β€” the standard for currency in business reports.

Total Transaction Revenue
SELECT ROUND(SUM(amount), 2) AS total_transaction_amount FROM transactions;
Average Product Price
SELECT ROUND(AVG(unit_price), 2) AS average_product_price FROM products;

MIN() returns the smallest value in a column β€” the cheapest product, the earliest date, the lowest transaction. MAX() returns the largest β€” the most expensive product, the most recent date, the highest transaction. Both can be used in the same SELECT statement to show both extremes at once, giving a quick range snapshot.

MIN and MAX in the Same Query

SELECT MIN(amount) AS lowest_transaction, MAX(amount) AS highest_transaction FROM transactions β€” this returns a single row showing both extremes, making it easy to understand the full range of a column in one query.

Lowest and Highest Transaction Amounts
SELECT MIN(amount) AS lowest_transaction, MAX(amount) AS highest_transaction FROM transactions;
Min and Max Subscription Fees
SELECT ROUND(MIN(monthly_fee), 2) AS minimum_fee, ROUND(MAX(monthly_fee), 2) AS maximum_fee FROM subscriptions;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Count the total number of users
AI Prompt
Write a MySQL query to count the total number of users.
SQL Answer
SELECT COUNT(*) AS total_users FROM users;
Practice 2 Calculate the total transaction amount rounded to 2 decimal places
AI Prompt
Write a MySQL query to calculate the total transaction amount rounded to 2 decimal places.
SQL Answer
SELECT ROUND(SUM(amount), 2) AS total_transaction_amount FROM transactions;
Practice 3 Calculate the average product price rounded to 2 decimal places
AI Prompt
Write a MySQL query to calculate the average product price rounded to 2 decimal places.
SQL Answer
SELECT ROUND(AVG(unit_price), 2) AS average_product_price FROM products;
Practice 4 Retrieve the minimum and maximum monthly_fee values rounded to 2 decimal places
AI Prompt
Write a MySQL query to retrieve the minimum and maximum monthly_fee values rounded to 2 decimal places.
SQL Answer
SELECT ROUND(MIN(monthly_fee), 2) AS minimum_fee, ROUND(MAX(monthly_fee), 2) AS maximum_fee FROM subscriptions;
Practice 5 Count the total number of support tickets
AI Prompt
Write a MySQL query to count the total number of support tickets.
SQL Answer
SELECT COUNT(*) AS total_tickets FROM support_tickets;
Practice 6 Calculate the total subscription revenue rounded to 2 decimal places
AI Prompt
Write a MySQL query to calculate the total subscription revenue rounded to 2 decimal places.
SQL Answer
SELECT ROUND(SUM(monthly_fee), 2) AS total_subscription_revenue FROM subscriptions;
Practice 7 Calculate the average transaction amount rounded to 2 decimal places
AI Prompt
Write a MySQL query to calculate the average transaction amount rounded to 2 decimal places.
SQL Answer
SELECT ROUND(AVG(amount), 2) AS average_transaction_amount FROM transactions;
Practice 8 Retrieve the minimum and maximum product prices rounded to 2 decimal places
AI Prompt
Write a MySQL query to retrieve the minimum and maximum product prices rounded to 2 decimal places.
SQL Answer
SELECT ROUND(MIN(unit_price), 2) AS minimum_price, ROUND(MAX(unit_price), 2) AS maximum_price FROM products;

Knowledge Check

Five questions β€” select the best answer for each.

1. What does COUNT(*) return?
AThe sum of all values in the table
BThe number of rows in the table, including rows with NULL values
CThe number of non-NULL values in a specific column
DThe average number of records per group
2. Which function calculates the total of all values in a numeric column?
ACOUNT()
BAVG()
CMAX()
DSUM()
3. Why should ROUND() be wrapped around AVG() when calculating average prices?
AAVG() requires ROUND() to function correctly
BTo limit the number of decimal places in the output to a readable financial format
CROUND() speeds up the AVG() calculation
DTo prevent AVG() from returning NULL
4. Which query returns both the lowest and highest transaction amounts in one result?
ASELECT MIN(amount), MAX(amount) AS range FROM transactions;
BSELECT MIN(amount) AS lowest_transaction, MAX(amount) AS highest_transaction FROM transactions;
CSELECT MIN AND MAX(amount) FROM transactions;
DSELECT RANGE(amount) FROM transactions;
5. An analyst uses COUNT(resolved_date) instead of COUNT(*) on support_tickets. What is the difference?
ANo difference β€” both always return the same number
BCOUNT(resolved_date) excludes rows where resolved_date is NULL; COUNT(*) includes all rows
CCOUNT(resolved_date) is faster because it uses an index
DCOUNT(*) only counts rows where all columns are non-NULL

Part 6 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

Aggregate functions summarize business data by counting records, calculating totals, measuring averages, and identifying minimum and maximum values. Always wrap decimal aggregations in ROUND(…, 2) for clean financial output.

COUNT()SUM()AVG()MIN()MAX()ROUND()Business SummaryRevenueKPIAggregation