TExcelTAD
Home › SQL › SQL Aggregate Functions

SQL Aggregate Functions

An aggregate function takes many rows and boils them down to a single value: how many orders, the highest price, the total stock. They are the building blocks of every report.

Function Returns
COUNT(*) number of rows
COUNT(col) number of non-NULL values in col
SUM(col) total of the values
AVG(col) average (mean) of the values
MIN(col) smallest value
MAX(col) largest value
GROUP_CONCAT(col, sep) values joined into one string (SQLite/MySQL; STRING_AGG in PostgreSQL/SQL Server)

One summary row

Without GROUP BY, an aggregate turns the whole table (after WHERE) into one row:

sql · try it yourself
SELECT COUNT(*)            AS products,
       MIN(price)          AS cheapest,
       MAX(price)          AS dearest,
       ROUND(AVG(price), 2) AS average_price,
       SUM(stock)          AS units_in_stock
FROM products;
products | cheapest | dearest | average_price | units_in_stock
---------+----------+---------+---------------+---------------
30       | 1.45     | 149     | 22.53         | 1587
(1 row)

Aggregates with WHERE

WHERE is applied first, then the remaining rows are aggregated. Statistics for Electronics only:

sql · try it yourself
SELECT COUNT(*) AS products, MIN(price) AS cheapest, MAX(price) AS dearest
FROM products
WHERE category_id = 6;
products | cheapest | dearest
---------+----------+--------
5        | 9.99     | 149
(1 row)

One row per group

With GROUP BY, you get one summary row per group. Products per category:

sql · try it yourself
SELECT category_id,
       COUNT(*) AS products,
       ROUND(AVG(price), 2) AS avg_price
FROM products
GROUP BY category_id
ORDER BY category_id;
category_id | products | avg_price
------------+----------+----------
1           | 5        | 10.41
2           | 4        | 9.66
3           | 3        | 4.4
4           | 4        | 3.03
5           | 3        | 3.99
6           | 5        | 60.5
7           | 3        | 47.63
8           | 3        | 34.17
(8 rows)

GROUP BY has its own lesson; for now just notice how it splits the rows before aggregating.

Aggregates ignore NULL

All aggregates except COUNT(*) skip NULL values. Of the 40 orders, only those that shipped have a shipped_date:

sql · try it yourself
SELECT COUNT(*)            AS orders,
       COUNT(shipped_date) AS shipped,
       MIN(shipped_date)   AS first_shipment,
       MAX(shipped_date)   AS last_shipment
FROM orders;
orders | shipped | first_shipment | last_shipment
-------+---------+----------------+--------------
40     | 25      | 2025-01-03     | 2025-09-09
(1 row)

MIN and MAX work on text and dates too — ISO dates compare correctly as text.

Joining values into a string

GROUP_CONCAT collects the values of a group into one text value:

sql · try it yourself
SELECT country, GROUP_CONCAT(name, ', ') AS customers
FROM customers
WHERE country IN ('USA', 'UK', 'France')
GROUP BY country
ORDER BY country;
country | customers
--------+------------------------------
France  | Bright Bytes, Kai Moreau
UK      | Nonna's Kitchen, Jonas Garcia
USA     | Night Owl Books, Tech Nest
(3 rows)

Note: The order of names inside GROUP_CONCAT is not guaranteed. Newer SQLite versions accept GROUP_CONCAT(name, ', ' ORDER BY name); PostgreSQL writes STRING_AGG(name, ', ' ORDER BY name).

Rules to remember

  • You cannot nest aggregates directly (MAX(COUNT(*))); use a subquery instead.
  • You cannot use an aggregate in WHERE — filter groups with HAVING.
  • In standard SQL, every non-aggregated column in the SELECT list must appear in GROUP BY. SQLite and MySQL are more forgiving, but relying on that gives unpredictable results.

Test yourself

Which function counts rows including those with NULLs?

  1. COUNT(col)
  2. COUNT(*)
  3. SUM(*)
  4. TOTAL(col)

In which order are WHERE and aggregation applied?

  1. Aggregation first, then WHERE
  2. WHERE first, then aggregation
  3. At the same time
  4. It depends on the database

Without GROUP BY, how many rows does SELECT MAX(price) FROM products return?

  1. One per product
  2. Exactly one
  3. Zero
  4. One per category