← SQL and databases

Totals and averages: SUM and AVG

Add up and average a column.

Start this lesson

SUM(column) adds up all the values in a column, and AVG(column) gives their average. Like COUNT, they turn many rows into one answer, and a WHERE clause decides which rows go in. Both skip NULL values. You can sum or average a calculation too, such as SUM(price * stock).

SELECT SUM(copies) AS total_copies,
       AVG(pages) AS avg_pages
FROM books;

One row with two answers: how many copies the library owns in total, and the average book length.

Averages often have long decimals. ROUND(AVG(price), 2) rounds to 2 decimal places.