SQL Aggregate Functions
Aggregate functions perform a calculation on a set of rows and return a single summary value. They are used to answer questions like "How many students are there?", "What is the total fees collected?", or "What is the highest fee paid?"
Think of aggregate functions like calculators that work on an entire column — or on groups of rows — and return one number as the answer.
The Reference Table
| StudentID | StudentName | Age | City | Course | Fees |
|---|---|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi | Science | 45000 |
| 2 | Priya Mehta | 22 | Mumbai | Commerce | 52000 |
| 3 | Arjun Nair | 19 | Chennai | Science | 38000 |
| 4 | Sneha Kapoor | 21 | Delhi | Arts | 42000 |
| 5 | Rohit Das | 23 | Kolkata | Commerce | 60000 |
| 6 | Nisha Verma | 20 | Mumbai | Science | 48000 |
| 7 | Karan Joshi | NULL | Delhi | Science | 47000 |
Note: Karan Joshi's Age is NULL to demonstrate how aggregate functions handle missing values.
The 5 Core Aggregate Functions
| Function | What It Does | Example |
|---|---|---|
COUNT() | Counts the number of rows | COUNT(*) → 7 |
SUM() | Adds up all values in a numeric column | SUM(Fees) → 332000 |
AVG() | Calculates the average of values in a numeric column | AVG(Fees) → 47428.57 |
MIN() | Returns the smallest value in a column | MIN(Fees) → 38000 |
MAX() | Returns the largest value in a column | MAX(Fees) → 60000 |
1. COUNT()
The COUNT() function counts rows. There are two important variations:
COUNT(*) — Counts All Rows (Including NULLs)
SELECT COUNT(*) AS TotalStudents FROM Students;Result: 7 (counts all rows, regardless of NULL values)
COUNT(column) — Counts Non-NULL Values Only
SELECT COUNT(Age) AS StudentsWithAge FROM Students;Result: 6 (Karan's Age is NULL, so he is not counted)
COUNT(DISTINCT column) — Counts Unique Values
SELECT COUNT(DISTINCT City) AS UniqueCities FROM Students;Result: 4 (Delhi, Mumbai, Chennai, Kolkata)
2. SUM()
SUM() adds up all the values in a numeric column. NULL values are ignored.
SELECT SUM(Fees) AS TotalFeesCollected FROM Students;Result:
| TotalFeesCollected |
|---|
| 332000 |
SUM With GROUP BY
SELECT Course, SUM(Fees) AS TotalFees
FROM Students
GROUP BY Course;Result:
| Course | TotalFees |
|---|---|
| Science | 178000 |
| Commerce | 112000 |
| Arts | 42000 |
3. AVG()
AVG() calculates the arithmetic mean (average) of all non-NULL values in a column.
SELECT AVG(Fees) AS AverageFees FROM Students;Result: 47428.57 (total 332000 divided by 7 students)
SELECT AVG(Age) AS AverageAge FROM Students;Result: AVG(Age) uses only 6 rows (Karan's NULL age is ignored): (20+22+19+21+23+20) / 6 = 20.83
4. MIN()
MIN() returns the smallest value in a column. For text columns, it returns the first value alphabetically.
SELECT MIN(Fees) AS LowestFee FROM Students;Result: 38000 (Arjun Nair)
SELECT MIN(StudentName) AS FirstAlphabetically FROM Students;Result: Arjun Nair (A comes first alphabetically)
5. MAX()
MAX() returns the largest value in a column. For text columns, it returns the last value alphabetically.
SELECT MAX(Fees) AS HighestFee FROM Students;Result: 60000 (Rohit Das)
Using Multiple Aggregate Functions Together
SELECT
COUNT(*) AS TotalStudents,
SUM(Fees) AS TotalFees,
AVG(Fees) AS AverageFee,
MIN(Fees) AS LowestFee,
MAX(Fees) AS HighestFee
FROM Students;Result:
| TotalStudents | TotalFees | AverageFee | LowestFee | HighestFee |
|---|---|---|---|---|
| 7 | 332000 | 47428.57 | 38000 | 60000 |
Aggregate Functions With GROUP BY
SELECT City,
COUNT(*) AS Students,
SUM(Fees) AS TotalFees,
AVG(Fees) AS AvgFee,
MIN(Age) AS YoungestAge,
MAX(Age) AS OldestAge
FROM Students
GROUP BY City;Result:
| City | Students | TotalFees | AvgFee | YoungestAge | OldestAge |
|---|---|---|---|---|---|
| Delhi | 3 | 134000 | 44666.67 | 20 | 21 |
| Mumbai | 2 | 100000 | 50000 | 20 | 22 |
| Chennai | 1 | 38000 | 38000 | 19 | 19 |
| Kolkata | 1 | 60000 | 60000 | 23 | 23 |
How Aggregate Functions Handle NULL
| Function | Behavior With NULL Values |
|---|---|
| COUNT(*) | Counts all rows including those with NULLs |
| COUNT(column) | Ignores NULL values |
| SUM(column) | Ignores NULL values |
| AVG(column) | Ignores NULL values (divides only by non-NULL count) |
| MIN(column) | Ignores NULL values |
| MAX(column) | Ignores NULL values |
Key Points to Remember
- Aggregate functions return a single value summarizing multiple rows.
- All aggregate functions except
COUNT(*)ignore NULL values. - They are almost always used with
GROUP BYto get per-group summaries. - They can be used in the
HAVINGclause to filter groups. - Always give aggregated columns a meaningful alias using
ASfor clear output.
Summary
SQL's five core aggregate functions — COUNT(), SUM(), AVG(), MIN(), and MAX() — are essential tools for data analysis and reporting. They convert a group of rows into a single summary value. When combined with GROUP BY, they provide per-group insights. They form the backbone of every analytical SQL query.
