Learn SQL
-- aggregations.avg_sum

AVG & SUM in SQL

Average and total of a numeric column

--concept

SUM adds a numeric column across matching rows; AVG is SUM divided by COUNT of non-null values. Both ignore NULLs entirely — which is usually what you want mathematically but occasionally surprises people expecting zeros to count. Round results explicitly (ROUND(AVG(salary), 2)) for clean reports.

--how it works
1

SUM totals a numeric column

SUM(salary) adds the values of every matching row — payroll in one number.

2

AVG is the mean

AVG(salary) = SUM ÷ COUNT over non-NULL values; NULLs are skipped, not zero-filled.

3

Combine them freely

One SELECT can output SUM, AVG, MIN, MAX, COUNT together.

4

Round for humans

ROUND(AVG(salary), 2) — floating averages print ugly decimals otherwise.

5

Scope before summing

WHERE department = 'Sales' first; aggregates then describe just that slice.

--visualization
Alice85,000Bob52,000Cynthia91,000David47,000Elena58,000Farid73,000SUM = 0 · AVG ≈ —NULLs are skipped by both functions — they are not zeros
Stage 1 / 8
AVG(salary) and SUM(salary) scan the same six values — watch both build up.
--example
query.sql
1SELECT SUM(salary) AS total_payroll,
2 ROUND(AVG(salary), 0) AS avg_salary
3FROM employees;
Result
total_payrollavg_salary
40600067667

85k + 52k + 91k + 47k + 58k + 73k = 406,000 total; divided by 6 ≈ 67,667 average. The visualization accumulates a running bar as each row is added.

--variants

SUM

The total of a column.

query.sql
1SELECT SUM(salary) AS total_payroll FROM employees;
Result
total_payroll
406000
185000+52000+91000+47000+58000+73000.
2One number comes back — six rows in, one row out.
3NULL salaries would be ignored, not treated as 0.

AVG

The mean, NULLs excluded.

query.sql
1SELECT ROUND(AVG(salary), 0) AS avg_salary FROM employees;
Result
avg_salary
67667
1406000 ÷ 6 = 67666.67 → rounds to 67667.
2Had two salaries been NULL, the divisor would be 4, not 6.
3NULL-skipping is usually the math you want — but know it.

Both at once

Aggregates compose in one SELECT.

query.sql
1SELECT SUM(salary) AS total, ROUND(AVG(salary), 0) AS average
2FROM employees;
Result
totalaverage
40600067667
1Two aggregate expressions scan the same rows once.
2Each gets its own result column and alias.
3GROUP BY would repeat this per department.

Scoped average

Average of a filtered slice.

query.sql
1SELECT ROUND(AVG(salary), 0) AS sales_avg
2FROM employees
3WHERE department = 'Sales';
Result
sales_avg
55000
1WHERE keeps Bob (52000) and Elena (58000).
2Average = 110000 ÷ 2 = 55000 exactly.
3Marketing alone would average 47000 — one row, itself.
--key facts
NULL ruleboth functions skip NULLs — AVG ignores them, does not treat as 0
Integer divisionsome engines return integer averages — cast or ROUND
PairingSUM/AVG/MIN/MAX are siblings usable together in one query
--use cases
Payroll, revenue and inventory totals
KPI cards: average order value, mean response time
Comparing segments: average salary per department
Anomaly detection: today's SUM against the trailing average
--under the hood
SUM/AVG over an indexed column can scan just the index — smaller I/O than the table.
Watch integer division in some dialects: AVG over integers may truncate unless cast; ROUND, then verify.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.