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

COUNT, SUM, AVG, MIN and MAX

One Dataset, Exact Results

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,
       AVG(marks) AS average,
       MIN(marks) AS minimum,
       MAX(marks) AS maximum
FROM students;
4 | 340.00 | 85.000000 | 74.00 | 91.00

The displayed scale of AVG may vary with client and type; the mathematical value is 85.

COUNT Variants

SELECT COUNT(*) AS all_rows,
       COUNT(marks) AS known_marks,
       COUNT(DISTINCT class_name) AS classes
FROM students;
all_rows = 4 known_marks = 4 classes = 2

COUNT(1) also counts rows, but COUNT(*) states intent clearly and MySQL optimizes it. COUNT(DISTINCT expression) counts distinct non-NULL values.

SUM and AVG

SELECT SUM(marks) AS active_total,
       ROUND(AVG(marks), 2) AS active_average
FROM students
WHERE status = 'Active';
active_total = 266.00 active_average = 88.67

AVG is SUM of non-NULL inputs divided by their non-NULL count. Do not round individual marks before aggregation unless that is the stated business rule.

MIN and MAX

SELECT MIN(marks) AS lowest,
       MAX(marks) AS highest,
       MIN(full_name) AS first_name,
       MAX(full_name) AS last_name
FROM students;
lowest = 74.00 highest = 91.00 first_name = Aarav last_name = Sana

MIN/MAX work on comparable values; text results follow collation. To retrieve the student row holding the maximum, use ORDER BY marks DESC with LIMIT and a tie policy, or join to a subquery—MAX alone returns only the value.

Conditional Aggregation

SELECT
  COUNT(*) AS total,
  SUM(CASE WHEN status = 'Active' THEN 1 ELSE 0 END) AS active,
  SUM(CASE WHEN marks >= 85 THEN 1 ELSE 0 END) AS marks_85_plus
FROM students;
total = 4 active = 3 marks_85_plus = 3

Conditional aggregation creates several metrics in one scan and generalizes cleanly inside GROUP BY reports.

Mistakes and Practice

  • Using COUNT(column) when NULL rows must count.
  • Assuming MAX returns the whole winning row.
  • Rounding each input too early.
  • Converting text to numbers implicitly.
  • Dividing SUM by COUNT(*) when NULL inputs should be excluded.
  1. Calculate inactive count and average.
  2. Count distinct statuses.
  3. Return min/max per class.
  4. Count marks at least 90 conditionally.

Quick Summary

  • COUNT has row, non-NULL and distinct forms.
  • SUM totals; AVG uses known values; MIN/MAX return extreme values.
  • Conditional aggregation builds multiple report metrics.
  • NULL, precision, collation and tie handling matter.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts rows. COUNT(column) counts only rows where that expression is not NULL.
Can SUM and AVG be used on text columns?
They are numeric aggregates. Relying on implicit text-to-number conversion is unsafe; store numeric data in numeric columns.
Does AVG round automatically?
AVG returns a numeric result according to input type and MySQL precision rules. Use ROUND only for the required display scale.
How do I count unique classes?
Use COUNT(DISTINCT class_name). NULL is not included in that count.
How can I count rows matching a condition?
Use SUM(condition) in MySQL for TRUE/FALSE expressions or the portable SUM(CASE WHEN condition THEN 1 ELSE 0 END).
🔗

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.