Skip to content
BENIBIX Learn

Aggregate Function

Development🗃️ DatabasesDatabase Basics

Also known as SQL Aggregate Function, COUNT, SUM, AVG

A SQL tool that turns lots of rows into one number, like a sum.

Think of it like

It is like a till at the end of the day. It turns a pile of sales into one total.

Example

Emma runs SELECT COUNT(*) FROM orders and sees 1,204. She runs AVG(total) and learns the average order size.

Why it matters

Totals, counts and averages are what most reports show. These functions work them out inside the database, which is fast.

Definition

An aggregate function takes many rows and gives back one value. The common ones are COUNT, SUM, AVG, MIN and MAX. COUNT says how many rows there are, SUM adds them up and AVG gives the average. On its own, it works over every row the query found. With GROUP BY, it gives one answer for each group.

Example code

sql
SELECT COUNT(*), AVG(total), MAX(total)
FROM orders;
How many orders, the average size and the biggest one, in one line.
SUM, on the right, turns each group of rows into one total.

Common mistake

Thinking AVG counts empty values as zero. These functions skip NULL, so the average only uses rows that have a value.

Key takeaways

  1. 1
    Many in, one out. A whole set of rows becomes one value.
  2. 2
    Five common ones. COUNT, SUM, AVG, MIN, MAX.
  3. 3
    Per group with GROUP BY. One answer for each group.