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(*)andCOUNT(column) - How
NULLvalues affect the results - Naming results with
ASand rounding withROUND - 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 wherecgpais notNULL: 7, because Nadia’s is missing.AVG,SUM,MINandMAXalso skipNULLs. The average 3.5 is the total of 7 values divided by 7, not 8.ASgives each result a readable column name, andROUND(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
| Function | Returns | On NULLs |
|---|---|---|
COUNT(*) | Number of rows | Counts them |
COUNT(col) | Number of non-null values | Skips them |
SUM(col) | Total | Skips them |
AVG(col) | Mean | Skips them |
MIN(col) / MAX(col) | Smallest / largest | Skip 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
- Mixing aggregates with plain columns, such as
SELECT name, MAX(cgpa) FROM students. Most databases reject it. SQLite accepts it and, only forMINandMAX, takes the name from the matching row, but that is not portable. UseORDER BY ... LIMIT 1or a subquery to find who has the maximum. - Expecting
AVGto count missing values as zero. UseAVG(COALESCE(cgpa, 0))if that is what you want. - Using an aggregate in
WHERE, as inWHERE AVG(cgpa) > 3. Filter groups withHAVINGinstead.
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,MINandMAXsummarise many rows into one value.COUNT(*)counts rows; every other aggregate ignoresNULL.- Use
ASfor names andROUNDfor tidy decimals. WHEREchooses the rows before they are summarised.
Next: SQL GROUP BY and HAVING.
