Aggregate functions help you summarize data. Learn how and when to use them in SQL.
When you're analyzing data in SQL, you’ll often want to calculate summaries—like how many users signed up, what the average purchase size is, or what the highest revenue day was. That’s where aggregate functions like COUNT
, SUM
, AVG
, MAX
, and MIN
come in.
These built-in SQL functions help you get powerful insights from your data using just a few lines of code. In this guide, we’ll break down what each function does, when to use it, and how it interacts with clauses like GROUP BY
and WHERE
.
Aggregate functions perform calculations on a set of values and return a single result. They’re most commonly used with SELECT
statements and often paired with the GROUP BY
clause to summarize information.
You can try out each example live using the Galaxy SQL Editor—no setup required.
COUNT()
returns the number of rows that match a condition. It's great for tracking volume: total users, signups, orders, etc.
SELECT COUNT(*) FROM users;
This returns the total number of rows in the users
table.
SELECT COUNT(email) FROM users;
This counts only users who have an email (ignores NULLs).
Use COUNT()
often to track system usage or monitor data growth.
SUM()
adds up all numeric values in a column.
SELECT SUM(total_amount) FROM orders;
This calculates the total money made across all orders.
Pair SUM()
with a WHERE
clause to segment results:
SELECT SUM(total_amount) FROM orders WHERE status = 'paid';
For financial summaries and reports, SUM()
is essential.
AVG()
gives you the average value of a numeric column.
SELECT AVG(total_amount) FROM orders;
Want to find the average order size by customer? Combine it with GROUP BY
:
SELECT user_id, AVG(total_amount) FROM orders GROUP BY user_id;
Read more about grouping in ORDER BY vs GROUP BY.
MAX()
returns the largest value in a column.
SELECT MAX(total_amount) FROM orders;
This returns the highest purchase ever made.
Combine it with WHERE
to filter:
SELECT MAX(total_amount) FROM orders WHERE status = 'refunded';
MIN()
returns the smallest value in a column.
SELECT MIN(created_at) FROM users;
This shows the first-ever user signup in your system.
If you want to see these summaries broken down by category (e.g., by customer, region, or date), use GROUP BY
.
SELECT DATE(created_at), COUNT(*) FROM orders GROUP BY DATE(created_at);
This counts how many orders were placed each day.
SELECT region, AVG(total_amount) FROM orders GROUP BY region;
For a deeper dive into this topic, check out our GROUP BY vs ORDER BY article.
When using GROUP BY
, and you want to filter groups based on aggregated values, use HAVING
instead of WHERE
.
SELECT user_id, COUNT(*) as num_orders
FROM orders
GROUP BY user_id
HAVING COUNT(*) >= 5;
This filters out users with fewer than 5 orders.
To avoid common pitfalls when using aggregate functions, see our SQL errors guide.
AS total_orders
) to improve readability.GROUP BY
to create segmented reports.HAVING
(not WHERE
) when filtering aggregated results.COUNT(column)
ignores NULL
, but COUNT(*)
does not.Use SUM()
and COUNT()
to show totals like revenue, signups, or traffic.
Find your top users with MAX()
or AVG()
by usage or purchase frequency.
Use COUNT()
and GROUP BY
on logs to track API failures or user-reported issues.
Explore how teams use these techniques with Galaxy’s product overview.
Mastering aggregate functions is a big step toward writing effective analytical queries in SQL. Whether you're tracking business metrics or cleaning up datasets, COUNT
, SUM
, AVG
, MAX
, and MIN
will be your go-to tools.
Once you understand how to use them with GROUP BY
and HAVING
, you’ll be able to turn raw data into actionable insights.
Test your own queries in the Galaxy SQL Editor or continue learning through our full SQL learning series.