Labs ICT
โญ Pro Login

SUM & AVG

When you need to add up numbers or find averages, SUM and AVG are what you reach for.

SUM โ€” Adding It All Up

-- Total of all student scores
SELECT SUM(score) AS total_score FROM students;

SUM ignores NULL values, which is usually what you want. If a student has no score, they just do not get counted in the total.

AVG โ€” Finding the Average

-- Average score of all students
SELECT AVG(score) AS average_score FROM students;

AVG also ignores NULLs. It sums up all non-NULL values and divides by the count of non-NULL values.

Using Both Together

SELECT 
  COUNT(*) AS student_count,
  SUM(score) AS total_score,
  AVG(score) AS average_score
FROM students;

You can mix aggregate functions in a single query. This is super common when building dashboards or reports.

๐Ÿงช Quick Quiz

What does AVG() return when all values are NULL?