Aggregate Function

Last Updated : 13 Nov, 2025

SQL aggregate functions are used to perform calculations on a set of values and return a single summarised result. Aggregate functions operate on multiple rows but return a single value per group or entire table.

SQL COUNT() Function

The COUNT() function provides the number of rows that match a specified condition. It is often used to determine:

  • Total records in a table
  • Records that meet a specific filter

Example: First, we will create a demo SQL database and table, on which we will use the COUNT() Function command.

Geekstab
SELECT COUNT(Name)
FROM GeeksTab; 

Output:

Count_name


SQL AVG() Function

The AVG() function provides the average value of a numeric column, helping you determine central tendencies in your data. This is useful for understanding the mean value of a set of numbers, such as salaries, prices, or scores.

Now let's understand this with the help of query:

SELECT AVG(Salary)
FROM GeeksTab; 

Output:

avg-salary

SQL SUM() Function

The SUM() function calculates the total sum of a numeric column. It is ideal for calculating:

  • Total sales
  • Total revenue
  • Any cumulative numeric value

Now let's understand this with the help of query:

SELECT SUM(Salary)
FROM GeeksTab; 

Output:

sum-sal


Note: COUNT(), AVG() and SUM() primarily used for numerical analysis and aggregation.

SQL MIN() Function

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

  • Works with numeric, string, and date data types.
  • Can be used with DISTINCT to find the smallest among unique values.
  • Useful for identifying minimum prices, earliest dates, youngest age, etc.
SELECT MIN(Salary) AS Min_Salary
FROM GeeksTab;

Output:

Min_salary

SQL MAX() Functions 

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

  • Works with numeric, string, and date data types.
  • Can be combined with GROUP BY, HAVING, and subqueries for advanced analysis.
  • Useful for identifying highest salaries, latest dates, oldest age, etc.

Now let's understand this with the help of query:

SELECT MAX(Salary) AS Max_Salary
FROM GeeksTab;

Output:

Max_sal

Using MIN() and MAX() with HAVING Clause

Suppose we want to fetch a person's name with max age as column max_age then we can use the following query but with some conditions like min age at least 22

SELECT Name, MAX(Salary) AS max_salary
FROM GeeksTab
GROUP BY Name
HAVING MIN(Salary) > 5000;

Output:

aggre

Note:

  • Aggregate functions ignore NULL values by default, unless explicitly handled.
  • MIN() and MAX() used to find extremes or boundary values in a dataset.
Comment