Ira SQL ProIra SQL Pro

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

  1. Numeric Columns Only — SUM and AVG work on numeric data types.
  2. NULLs Ignored — Both functions skip NULL values. This affects the average.
  3. Rounding — Use ROUND(AVG(column), 2) to limit decimal places.
  4. With GROUP BY — Segment totals and averages by category.
  5. DISTINCTSUM(DISTINCT column) and AVG(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.

Related Topics