![]() |
VOOZH | about |
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.
The COUNT() function provides the number of rows that match a specified condition. It is often used to determine:
Example: First, we will create a demo SQL database and table, on which we will use the COUNT() Function command.
SELECT COUNT(Name)
FROM GeeksTab; Output:
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:
The SUM() function calculates the total sum of a numeric column. It is ideal for calculating:
Now let's understand this with the help of query:
SELECT SUM(Salary)
FROM GeeksTab; Output:
Note: COUNT(), AVG() and SUM() primarily used for numerical analysis and aggregation.
The MIN() function returns the smallest value in a specified column.
SELECT MIN(Salary) AS Min_Salary
FROM GeeksTab;Output:
The MAX() function returns the largest value in a specified column.
Now let's understand this with the help of query:
SELECT MAX(Salary) AS Max_Salary
FROM GeeksTab;Output:
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:
Note:
- Aggregate functions ignore NULL values by default, unless explicitly handled.
- MIN() and MAX() used to find extremes or boundary values in a dataset.