Beginners Resources

Beginner's Guide to SQL Functions: COUNT, SUM, AVG, MAX, and MIN

Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Aggregate functions help you summarize data. Learn how and when to use them in SQL.

Table of Contents

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.

What Are Aggregate Functions in SQL?

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.

1. COUNT(): Count Rows

COUNT() returns the number of rows that match a condition. It's great for tracking volume: total users, signups, orders, etc.

Example: Count All Rows

SELECT COUNT(*) FROM users;

This returns the total number of rows in the users table.

Example: Count Non-NULL Values

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.

2. SUM(): Total Values

SUM() adds up all numeric values in a column.

Example: Total Revenue

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.

3. AVG(): Calculate Averages

AVG() gives you the average value of a numeric column.

Example: Average Purchase Amount

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.

4. MAX(): Highest Value

MAX() returns the largest value in a column.

Example: Largest Order

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';

5. MIN(): Lowest Value

MIN() returns the smallest value in a column.

Example: Earliest Signup Date

SELECT MIN(created_at) FROM users;

This shows the first-ever user signup in your system.

Using Aggregate Functions with GROUP BY

If you want to see these summaries broken down by category (e.g., by customer, region, or date), use GROUP BY.

Example: Orders Per Day

SELECT DATE(created_at), COUNT(*) FROM orders GROUP BY DATE(created_at);

This counts how many orders were placed each day.

Example: Average Revenue Per Region

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.

Filtering Results with HAVING

When using GROUP BY, and you want to filter groups based on aggregated values, use HAVING instead of WHERE.

Example: Only Active Users with 5+ Orders

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.

Best Practices

  • Use clear aliases (like AS total_orders) to improve readability.
  • Combine aggregate functions with GROUP BY to create segmented reports.
  • Use HAVING (not WHERE) when filtering aggregated results.
  • Remember: COUNT(column) ignores NULL, but COUNT(*) does not.

Real-World Use Cases

1. Business Dashboards

Use SUM() and COUNT() to show totals like revenue, signups, or traffic.

2. Usage Analytics

Find your top users with MAX() or AVG() by usage or purchase frequency.

3. Error Monitoring

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.

Final Thoughts

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.

Check out some other beginners resources