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

Aggregate functions are used to perform calculations on multiple rows of a table and return a single value. They are commonly used in reporting, data analysis, and summarizing information.

1. SUM()

The SUM() function adds all the values in a numeric column.

Syntax:
SELECT SUM(column_name) FROM table_name;

Example:
SELECT SUM(salary) FROM employees;
This returns the total salary of all employees.

2. COUNT()

The COUNT() function counts the number of rows that match a specified condition or all rows if no condition is given.

Syntax:
SELECT COUNT(column_name) FROM table_name;
or
SELECT COUNT(*) FROM table_name;

Example:
SELECT COUNT(*) FROM employees;
This returns the total number of employees.

3. AVG()

The AVG() function calculates the average value of a numeric column.

Syntax:
SELECT AVG(column_name) FROM table_name;

Example:
SELECT AVG(salary) FROM employees;
This returns the average salary of all employees.

4. MIN()

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

Syntax:
SELECT MIN(column_name) FROM table_name;

Example:
SELECT MIN(salary) FROM employees;
This returns the lowest salary in the table.

5. MAX()

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

Syntax:
SELECT MAX(column_name) FROM table_name;

Example:
SELECT MAX(salary) FROM employees;
This returns the highest salary in the table.

Key Notes

  • Aggregate functions ignore NULL values, except COUNT(*).
  • They are often used with the GROUP BY clause to calculate values for each group.
  • Can be combined with the WHERE clause to filter rows before performing calculations.
Home » SQL Foundations Program (SQL-101) > Working with Functions > Aggregate Functions (SUM, COUNT, AVG, MIN, MAX)