Checking your session…
Module 06: Constraints and Database Schema Objects

6.3 Aggregate Functions (MIN, MAX, SUM, AVG, COUNT)

Level: 6 | Version: v1.1 | Author: Meptrasoft

Overview

An aggregate function performs a calculation on multiple rows and returns a single result.

The five core aggregate functions are:

Function Purpose
MIN() Finds the smallest value
MAX() Finds the largest value
SUM() Calculates the total
AVG() Calculates the average
COUNT() Counts rows or non-NULL values

Aggregate functions summarize data and return a single value.

SQL Aggregate Functions: MIN, MAX, SUM, AVG, and COUNT summarizing Salaries into single results
Figure 1: Aggregate functions summarize multiple rows and return a single result.

Practice Tables

This topic reuses the employees and salaries tables from 6.2 Database Objects.

Salaries

emp_id salary
1 45000
2 60000
3 55000
4 50000

MIN() and MAX()

MIN() returns the smallest value, while MAX() returns the largest value.

SQL
SELECT
    MIN(salary) AS minimum_salary,
    MAX(salary) AS maximum_salary
FROM salaries;

Result:

minimum_salary maximum_salary
45000 60000

MIN() → Smallest value
MAX() → Largest value


SUM()

SUM() calculates the total of the selected numeric values.

SQL
SELECT SUM(salary) AS total_salary
FROM salaries;

Result:

total_salary
210000

SUM() → Total


AVG()

AVG() calculates the average of the selected numeric values.

SQL
SELECT AVG(salary) AS average_salary
FROM salaries;

Result:

average_salary
52500

AVG() → Average


COUNT()

COUNT() counts rows or non-NULL values, depending on how it is used.

Count all rows

SQL
SELECT COUNT(*) AS employee_count
FROM employees;

Result:

employee_count
4

Count non-NULL salary values

SQL
SELECT COUNT(salary) AS salary_count
FROM salaries;

COUNT(salary) counts only rows where salary is not NULL.

COUNT(*) → Counts rows
COUNT(column) → Counts non-NULL values


NULL and Aggregate Functions

A common beginner point is that most aggregate functions ignore NULL values.

For example, consider four salary rows:

TEXT
Salary:
45000
60000
NULL
50000

SUM(salary) does not treat NULL as zero; the NULL value is ignored in the aggregate calculation.

Similarly:

TEXT
COUNT(salary) → 3
COUNT(*)      → 4

COUNT(*) counts rows, while COUNT(column) counts non-NULL values in that column.


Simple Comparison

Function Example Question
MIN() What is the lowest salary?
MAX() What is the highest salary?
SUM() What is the total salary?
AVG() What is the average salary?
COUNT() How many employees are there?

Easy Memory Trick

TEXT
MIN   → LOWEST
MAX   → HIGHEST
SUM   → TOTAL
AVG   → AVERAGE
COUNT → HOW MANY

Common Beginner Mistakes

✗ Mistake ✓ Correct Understanding
COUNT(*) and COUNT(column) always return the same result COUNT(column) ignores NULL; COUNT(*) counts rows
AVG() treats NULL as zero NULL values are ignored by AVG()
SUM() works on text values It is normally used with numeric values
Aggregate functions always return multiple rows Without grouping, they normally return one result row
MIN() and MAX() only work with numbers They can also be used with other comparable data types, depending on the database
COUNT() always counts values Its behavior depends on whether *, a column, or an expression is used

Placement Quick Points

TEXT
MIN()   → SMALLEST
MAX()   → LARGEST
SUM()   → TOTAL
AVG()   → AVERAGE
COUNT() → COUNT
  • Aggregate functions summarize multiple rows.
  • MIN() finds the smallest value.
  • MAX() finds the largest value.
  • SUM() calculates a total.
  • AVG() calculates an average.
  • COUNT(*) counts rows.
  • COUNT(column) counts non-NULL values.

Interview Questions

What is an aggregate function?

An aggregate function performs a calculation on multiple rows and returns a summarized result.

What are the five common aggregate functions?
TEXT
MIN()
MAX()
SUM()
AVG()
COUNT()
What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts rows, while COUNT(column) counts only non-NULL values in that column.

What does AVG() do with NULL values?

AVG() ignores NULL values when calculating the average.

Which function finds the highest salary?

MAX(salary).

Which function calculates total salary?

SUM(salary).

Can aggregate functions be used with GROUP BY?

Yes. GROUP BY allows aggregate calculations to be performed separately for each group.

Example:

SQL
SELECT department, AVG(salary)
FROM employees
GROUP BY department;

Practice & Hands-On Exercises

Using the salaries and employees tables:

  1. Find the minimum salary:
SQL
SELECT MIN(salary) FROM salaries;
  1. Find the maximum salary:
SQL
SELECT MAX(salary) FROM salaries;
  1. Calculate the total salary:
SQL
SELECT SUM(salary) FROM salaries;
  1. Calculate the average salary:
SQL
SELECT AVG(salary) FROM salaries;
  1. Count all employees:
SQL
SELECT COUNT(*) FROM employees;
  1. Explain the difference between COUNT(*) and COUNT(salary).
  2. Add a NULL salary and observe how AVG() behaves.
  3. Calculate the average salary for each department using GROUP BY.

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
MIN   → LOWEST
MAX   → HIGHEST
SUM   → TOTAL
AVG   → AVERAGE
COUNT → HOW MANY

Aggregate functions summarize data by calculating values such as minimum, maximum, total, average, and count.

End of Aggregate Functions (MIN, MAX, SUM, AVG, COUNT)