MySQL Aggregate Functions (original) (raw)

Last Updated : 27 Mar, 2026

MySQL Aggregate Functions are used to perform calculations on multiple rows and return a single summarized result, making data analysis easier. They are commonly used with the SELECT statement and often combined with GROUP BY for grouping data.

**Syntax:

SELECT AGGREGATE_FUNCTION(column_name)
FROM table_name
WHERE condition;

First, we will create a demo table on which the aggregate functions will be applied:

Screenshot-2026-03-27-140425

employees Table

1. COUNT()

The COUNT() function returns the number of rows that match a specified condition. It is commonly used to count total records or filtered results.

**Syntax:

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

**Query:

SELECT COUNT(*) AS total_employees
FROM employees;

**Output:

Screenshot-2026-03-27-141016

2. SUM()

The SUM() function calculates the total sum of values in a numeric column. It is useful for finding totals like salary, sales, or marks.

**Syntax:

SELECT SUM(column_name)
FROM table_name
WHERE condition;

**Query:

SELECT SUM(salary) AS total_salary
FROM employees;

**Output:

Screenshot-2026-03-27-142553

3. AVG()

The AVG() function returns the average value of a numeric column. It is commonly used to find mean values.

**Syntax:

SELECT AVG(column_name)
FROM table_name
WHERE condition;

**Query:

SELECT AVG(salary) AS average_salary
FROM employees;

**Output:

Screenshot-2026-03-27-141611

4. MAX()

The MAX() function returns the highest value in a column. It is useful for finding maximum values like highest salary or marks.

**Syntax:

SELECT MAX(column_name)
FROM table_name
WHERE condition;

**Query:

SELECT MAX(salary) AS highest_salary
FROM employees;

**Output:

Screenshot-2026-03-27-141937

5. MIN()

The MIN() function returns the lowest value in a column. It is useful for identifying minimum values like lowest salary or marks.

**Syntax:

SELECT MIN(column_name)
FROM table_name
WHERE condition;

**Query:

SELECT MIN(salary) AS lowest_salary
FROM employees;

**Output:

Screenshot-2026-03-27-142257

Using Aggregate Functions with GROUP BY

The GROUP BY clause is used with aggregate functions to group rows based on one or more columns. It helps perform calculations on each group instead of the entire table.

**Syntax:

SELECT column_name, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column_name;

**Query:

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

**Output:

Screenshot-2026-03-27-143040