SQL SUM and AVG Functions
beginneraggregation
2 min read
What are SUM and AVG?
SUM adds up all values in a column. AVG calculates the arithmetic mean. Both ignore NULL values.
Syntax
SELECT SUM(column) FROM table_name;
SELECT AVG(column) FROM table_name;When to Use
- Calculating total revenue, hours, or quantity
- Finding average price, age, or score
- Creating summary reports with GROUP BY
Key Points
- Numeric Columns Only — SUM and AVG work on numeric data types.
- NULLs Ignored — Both functions skip NULL values. This affects the average.
- Rounding — Use
ROUND(AVG(column), 2)to limit decimal places. - With GROUP BY — Segment totals and averages by category.
- DISTINCT —
SUM(DISTINCT column)andAVG(DISTINCT column)only consider unique values.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL SUM and AVG Functions Challenge
Write a query that solve this task: calculate the sum of all order totals.
Expected result
A single number representing total revenue across all orders.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What are SUM and AVG?
Initializing database...Each run starts from fresh sample data.
More Examples
Average student age
Find the average age of all students.
Initializing database...Each run starts from fresh sample data.
Average salary by department
Calculate average salary per department.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Does AVG include NULL values?
No. AVG ignores NULLs. If you have values 10, 20, NULL, 30, 40, the average is (10+20+30+40)/4 = 25.
How do I get a rounded average?
Use ROUND: SELECT ROUND(AVG(price), 2) FROM products; rounds to 2 decimal places.