Reading history

SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX

3 min read 0 views

Learn SQL aggregate functions: COUNT, SUM, AVG, MIN and MAX, COUNT(*) vs COUNT(column), how NULLs are treated, AS aliases and ROUND.

Aggregate functions turn many rows into one summary value: how many students there are, the average CGPA, the highest mark, the total credits. They are the basis of every report and dashboard. This lesson covers COUNT, SUM, AVG, MIN and MAX, how each one treats NULL, and how to name and round the results.

In this lesson you will learn

  • The five core aggregate functions
  • The difference between COUNT(*) and COUNT(column)
  • How NULL values affect the results
  • Naming results with AS and rounding with ROUND
  • Counting unique values with COUNT(DISTINCT ...)

The Core Functions

SELECT COUNT(*) AS total_students,
       COUNT(cgpa) AS with_cgpa,
       ROUND(AVG(cgpa), 2) AS avg_cgpa,
       MAX(cgpa) AS highest,
       MIN(cgpa) AS lowest
FROM students;

SELECT SUM(credits) AS total_credits FROM courses;

SELECT COUNT(DISTINCT city) AS cities FROM students;

Output

+----------------+-----------+----------+---------+--------+
| total_students | with_cgpa | avg_cgpa | highest | lowest |
+----------------+-----------+----------+---------+--------+
| 8              | 7         | 3.5      | 3.95    | 2.98   |
+----------------+-----------+----------+---------+--------+
(1 row)

+---------------+
| total_credits |
+---------------+
| 13            |
+---------------+
(1 row)

+--------+
| cities |
+--------+
| 5      |
+--------+
(1 row)
  • COUNT(*) counts rows: 8 students.
  • COUNT(cgpa) counts only rows where cgpa is not NULL: 7, because Nadia’s is missing.
  • AVG, SUM, MIN and MAX also skip NULLs. The average 3.5 is the total of 7 values divided by 7, not 8.
  • AS gives each result a readable column name, and ROUND(x, 2) keeps two decimal places.
  • COUNT(DISTINCT city) counts unique values: students come from 5 cities.

Aggregates with WHERE

SELECT ROUND(AVG(marks), 1) AS avg_marks, MAX(marks) AS best
FROM enrollments
WHERE course_id = 102;

SELECT COUNT(*) AS cse_students FROM students WHERE dept = 'CSE';

Output

+-----------+------+
| avg_marks | best |
+-----------+------+
| 86.0      | 92   |
+-----------+------+
(1 row)

+--------------+
| cse_students |
+--------------+
| 4            |
+--------------+
(1 row)

WHERE runs first and picks the rows; the aggregate then summarises only those rows. Here that gives the average and best marks in the Databases course (id 102), and the number of CSE students.

Aggregate Functions at a Glance

FunctionReturnsOn NULLs
COUNT(*)Number of rowsCounts them
COUNT(col)Number of non-null valuesSkips them
SUM(col)TotalSkips them
AVG(col)MeanSkips them
MIN(col) / MAX(col)Smallest / largestSkip them

On an empty set, COUNT returns 0 while the others return NULL. MIN and MAX also work on text (alphabetical) and dates.

Common Mistakes

  1. Mixing aggregates with plain columns, such as SELECT name, MAX(cgpa) FROM students. Most databases reject it. SQLite accepts it and, only for MIN and MAX, takes the name from the matching row, but that is not portable. Use ORDER BY ... LIMIT 1 or a subquery to find who has the maximum.
  2. Expecting AVG to count missing values as zero. Use AVG(COALESCE(cgpa, 0)) if that is what you want.
  3. Using an aggregate in WHERE, as in WHERE AVG(cgpa) > 3. Filter groups with HAVING instead.

Practice Exercises

Exercise 1. How many enrollments have marks of 80 or more?

Show answer
SELECT COUNT(*) FROM enrollments WHERE marks >= 80;   -- 6

Exercise 2. Find the name that comes first alphabetically.

Show answer
SELECT MIN(name) FROM students;   -- Arif

Frequently Asked Questions

Is COUNT(1) faster than COUNT(*)?

No. Modern databases treat them the same. COUNT(*) is the clearest way to count rows.

How do I get one summary per department?

Add GROUP BY dept. See GROUP BY and HAVING.

Summary

  • COUNT, SUM, AVG, MIN and MAX summarise many rows into one value.
  • COUNT(*) counts rows; every other aggregate ignores NULL.
  • Use AS for names and ROUND for tidy decimals.
  • WHERE chooses the rows before they are summarised.

Next: SQL GROUP BY and HAVING.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *