Lesson 39 of 60 – Aggregate Functions
65%

Aggregate Functions in SQL

Aggregate functions are SQL functions that perform calculations on multiple rows and return a single result. They are commonly used to calculate totals, averages, counts, minimum values, and maximum values.

Note: Common aggregate functions include COUNT(), SUM(), AVG(), MIN(), and MAX().

1. What are Aggregate Functions?

Aggregate functions perform calculations on a group of rows and return one result.

For example, they can be used to find:

  • Total number of students
  • Total fees
  • Average marks
  • Lowest marks
  • Highest marks

2. Common Aggregate Functions

The most commonly used aggregate functions are:

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

3. COUNT() Function

The COUNT() function counts rows or non-NULL values.

SELECT COUNT(*)
FROM students;

This returns the total number of rows in the students table.

4. COUNT(*)

COUNT(*) counts all rows in the result, including rows containing NULL values in individual columns.

SELECT COUNT(*)
FROM students;

This is commonly used to find the total number of records.

5. COUNT(column_name)

COUNT(column_name) counts non-NULL values in the specified column.

SELECT COUNT(mobile)
FROM students;

Rows where mobile is NULL are not counted.

6. COUNT(DISTINCT)

You can combine COUNT with DISTINCT to count unique values.

SELECT COUNT(DISTINCT course)
FROM students;

This counts the number of different courses.

7. SUM() Function

The SUM() function calculates the total of numeric values.

SELECT SUM(fee)
FROM students;

This returns the total fee value.

8. SUM() with WHERE

SUM() can be combined with WHERE to calculate a total for selected records.

SELECT SUM(fee)
FROM students
WHERE course = 'Python';

This calculates the total fee for Python students.

9. AVG() Function

The AVG() function calculates the average of numeric values.

SELECT AVG(marks)
FROM students;

This returns the average marks.

10. AVG() with WHERE

You can calculate an average for selected records using WHERE.

SELECT AVG(marks)
FROM students
WHERE course = 'SQL';

This calculates the average marks of SQL students.

11. MIN() Function

The MIN() function returns the smallest value in a column.

SELECT MIN(marks)
FROM students;

This returns the lowest marks.

12. MAX() Function

The MAX() function returns the largest value in a column.

SELECT MAX(marks)
FROM students;

This returns the highest marks.

13. MIN() with Salary

MIN() can be used to find the lowest salary.

SELECT MIN(salary)
FROM employees;

This returns the smallest salary value.

14. MAX() with Salary

MAX() can be used to find the highest salary.

SELECT MAX(salary)
FROM employees;

This returns the largest salary value.

15. Using Multiple Aggregate Functions

You can use multiple aggregate functions in one SELECT statement.

SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee,
    AVG(fee) AS average_fee,
    MIN(fee) AS minimum_fee,
    MAX(fee) AS maximum_fee
FROM students;

This returns several summary values in one result.

16. Aggregate Functions with Aliases

Aliases make aggregate results easier to understand.

SELECT
    COUNT(*) AS total_students,
    AVG(marks) AS average_marks
FROM students;

The result columns will have meaningful names.

17. Aggregate Functions and NULL Values

Most aggregate functions ignore NULL values when calculating results.

SELECT AVG(marks)
FROM students;

Rows where marks is NULL are not included in the average calculation.

18. COUNT(*) vs COUNT(column)

These two forms behave differently when NULL values exist.

SELECT COUNT(*)
FROM students;
SELECT COUNT(marks)
FROM students;

COUNT(*) counts rows, while COUNT(marks) counts only non-NULL marks.

19. SUM() and NULL Values

SUM() generally ignores NULL values when calculating the total.

SELECT SUM(fee)
FROM students;

Only non-NULL fee values contribute to the total.

20. AVG() and NULL Values

AVG() generally ignores NULL values when calculating the average.

SELECT AVG(marks)
FROM students;

NULL marks are not treated as zero in the average calculation.

21. Aggregate Functions with WHERE

WHERE can filter rows before the aggregate calculation is performed.

SELECT SUM(fee)
FROM students
WHERE status = 'Active';

This calculates the total fee for active students.

22. Aggregate Functions with BETWEEN

You can combine aggregate functions with BETWEEN.

SELECT AVG(marks)
FROM students
WHERE marks BETWEEN 50 AND 100;

This calculates the average of marks within the specified range.

23. Aggregate Functions with IN

IN can be used to filter records before applying an aggregate function.

SELECT COUNT(*)
FROM students
WHERE course IN ('Python', 'SQL');

This counts students enrolled in Python or SQL.

24. COUNT() for Active Students

COUNT() can be used to calculate the number of students with a particular status.

SELECT COUNT(*) AS active_students
FROM students
WHERE status = 'Active';

This returns the number of active students.

25. Total Fees Collected

SUM() is useful for calculating total fees collected.

SELECT SUM(amount) AS total_collected
FROM payments
WHERE status = 'Paid';

This calculates the total amount from paid payment records.

26. Finding Highest and Lowest Marks

MAX() and MIN() can be used together.

SELECT
    MAX(marks) AS highest_marks,
    MIN(marks) AS lowest_marks
FROM students;

This returns both the highest and lowest marks.

27. Common Aggregate Function Mistake

A common mistake is expecting an aggregate function to return individual rows.

SELECT AVG(marks)
FROM students;

This returns one average value, not the marks of every student.

28. Aggregate Functions and GROUP BY

Aggregate functions become especially useful with GROUP BY. They can calculate separate results for each group.

SELECT course, COUNT(*)
FROM students
GROUP BY course;

This counts students separately for each course.

29. Practical Example

Suppose we have a students table containing marks and fees. We can generate a summary report using aggregate functions.

SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee,
    AVG(marks) AS average_marks,
    MIN(marks) AS lowest_marks,
    MAX(marks) AS highest_marks
FROM students;

This produces a useful summary of the student data.

30. Complete Aggregate Functions Example

Here is a complete example using the main aggregate functions.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100),
    marks INT,
    fee DECIMAL(10,2)
);

SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee,
    AVG(marks) AS average_marks,
    MIN(marks) AS lowest_marks,
    MAX(marks) AS highest_marks
FROM students;

The query generates a summary containing the number of students, total fees, average marks, lowest marks, and highest marks.

📌 Key Points

  • Aggregate functions perform calculations on multiple rows.
  • COUNT() counts rows or non-NULL values.
  • SUM() calculates the total of numeric values.
  • AVG() calculates the average.
  • MIN() returns the smallest value.
  • MAX() returns the largest value.
  • COUNT(*) counts rows, while COUNT(column) ignores NULL values.
  • Most aggregate functions ignore NULL values during calculations.
  • Aggregate functions can be combined with WHERE.
  • Aggregate functions are commonly used with GROUP BY.
  • Aliases can make aggregate results easier to understand.

🧠 Quick Quiz

Question: Which SQL function is used to calculate the total of numeric values?