Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 56

Aggregate Functions in MySQL

What Aggregation Does

Aggregate functions answer summary questions: How many students? What is the class average? Which mark is highest? Without GROUP BY, the filtered input is one group and the query normally returns one summary row.

Verified dataset: Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active). Total marks = 340.00 and overall average = 85.00.
SELECT COUNT(*) AS students,
       SUM(marks) AS total_marks,
       ROUND(AVG(marks), 2) AS average_marks,
       MIN(marks) AS lowest,
       MAX(marks) AS highest
FROM students;
students | total_marks | average_marks | lowest | highest 4 | 340.00 | 85.00 | 74.00 | 91.00

The Five Core Aggregates

FunctionQuestion answered
COUNT(*)How many rows?
COUNT(column)How many non-NULL values?
SUM(column)What is the total?
AVG(column)What is the arithmetic mean of known values?
MIN / MAXWhat are the smallest and largest comparable values?

NULL, Empty Inputs and Result Types

Most aggregates ignore NULL, which means AVG does not automatically treat missing marks as zero. COUNT(*) still counts the row. If a filter produces no input rows, COUNT returns 0 while SUM/AVG/MIN/MAX usually return NULL.

SELECT COUNT(*) AS rows_found,
       COUNT(marks) AS known_marks,
       COALESCE(SUM(marks), 0) AS safe_total
FROM students
WHERE class_name = 'XII-Z';

Use COALESCE only where a zero total is semantically correct. Exact result precision depends on input types; use DECIMAL for exact marks and money.

Filter Rows Before, Groups After

SELECT COUNT(*) AS active_students,
       ROUND(AVG(marks), 2) AS active_average
FROM students
WHERE status = 'Active';
active_students | active_average 3 | 88.67

WHERE removes inactive rows before aggregation. HAVING filters completed groups and is explained in the next lesson.

Whole Table vs Grouped Report

SELECT class_name,
       COUNT(*) AS students,
       ROUND(AVG(marks), 2) AS average_marks
FROM students
GROUP BY class_name
ORDER BY class_name;
X-A | 2 | 88.75 X-B | 2 | 81.25

Every selected nonaggregate column must be valid for the group. With ONLY_FULL_GROUP_BY, MySQL rejects ambiguous selected columns that are neither aggregated nor functionally dependent on GROUP BY columns.

Mistakes and Practice

  • Confusing COUNT(*) with COUNT(nullable_column).
  • Using aggregate conditions in WHERE.
  • Replacing NULL with zero without a business rule.
  • Selecting unrelated columns beside aggregates.
  • Rounding too early before totals or averages are complete.
  1. Calculate active total and average.
  2. Count known marks separately from rows.
  3. Return one summary per status.
  4. Predict results after inserting a NULL mark.

Quick Summary

  • Aggregates summarize rows into one result per group.
  • COUNT, SUM, AVG, MIN and MAX have distinct NULL behavior.
  • WHERE filters input rows; GROUP BY creates groups; HAVING filters groups.
  • Choose correct numeric types and round only for presentation.

Official References

References reviewed 14 August 2026. Outputs use the stated four-row dataset.

Frequently Asked Questions

What is an aggregate function?
It summarizes a set of input rows into one value per group, such as a count, total, average, minimum or maximum.
Do aggregate functions ignore NULL?
COUNT(expression), SUM, AVG, MIN and MAX ignore NULL inputs. COUNT(*) counts rows. If no non-NULL value exists, most numeric aggregates return NULL.
Can aggregate functions be used in WHERE?
Not directly for the same query level, because WHERE filters rows before groups are calculated. Use HAVING for aggregate conditions or an outer query.
What is the difference between aggregate and scalar functions?
An aggregate consumes a set of rows and returns one result per group. A scalar function normally returns one result for each input row.
Does AVG include missing marks as zero?
No. AVG ignores NULL. If the business rule treats missing marks as zero, express that explicitly with COALESCE—but verify that the meaning is correct.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.