SQL Aggregate Functions- Explore 5 Types of Functions
SQL Aggregate Functions- Explore 5 Types of Functions
Software Development
Table of Contents
Aggregate functions in SQL perform some calculations on more than one value to
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 1 of 27
:
return a single value. SQL has many aggregate functions, including average, count,
sum, min, and max. All aggregate functions ignore NULL values while calculating,
except the Count function.
An aggregate function in SQL returns one value after calculating multiple column
values. We often use aggregate functions with the SELECT statement's GROUP BY
and HAVING clauses.
Count()
Sum()
Avg()
Min()
Max()
Column References
In SQL, aggregate functions are crucial in calculating a set of values and returning a
single value. These functions are bene\cial when dealing with large datasets. When
using aggregate functions in SQL, it is essential to understand column references. A
column reference is a name containing the data you want to aggregate. To use an
aggregate function with a column reference, specify the column's name in the
function's parentheses.
For example, to \nd the average salary of employees in a table called "employees", you
would use the AVG function with the column reference "salary" like this:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 2 of 27
:
would use the AVG function with the column reference "salary" like this:
FROM employees;
Using column aliases instead of column references is also possible for a more
Explore our curated learning milestones for you!
readable code. However, understanding column references is essential when working
with SQL aggregate functions.
EXPLORE PROGRAM
Knowing the average price of all products stored in our warehouse or the total sales
amount over a period is pretty easy. However, aggregate functions would have to be
replaced by checking every data point on our own, which is time-consuming and full of
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 3 of 27
:
replaced by checking every data point on our own, which is time-consuming and full of
potential failures.
Aggregate functions in SQL are important to anyone working with large amounts of
data and trying to seek valuable insight.
1. COUNT() Function
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Example:
The following SQL statement fetches the number of products in the table.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 4 of 27
:
This will produce the following result.
The below-given command will display those product ids where the unit price is
greater than 4.
Let's look at how we can use GROUP BY and HAVING functions with the COUNT
function.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 5 of 27
:
The SQL command given below will list the number of customers in each city.
Unlock the power of data with our SQL Certi\cation Course! Learn essential SQL
skills and gain a valuable certi\cation to advance your career. Enroll now and take
the \rst step towards becoming a data expert!
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 6 of 27
:
2. SUM() Function
Syntax:
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example:
The following SQL statement \nds the sum of the "unit price" \elds in the "products"
table:
Let’s look at how we can use GROUP BY and HAVING functions with the SUM function.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 7 of 27
:
The SQL command below will list the number of customers in each city, with a sum of
points greater than 3000.
3. AVG() Function
Syntax:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 8 of 27
:
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
4. MIN() Function
The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL
values.
Syntax:
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 9 of 27
:
The above code will give us the minimum quantity in stock in the products table.
5. MAX() Function
The MAX() aggregate function returns the highest value (maximum) in a set of non-
NULL values.
Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
The code depicted below will give us the maximum quantity in stock in the products
table.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 10 of 27
:
This will produce the following result.
EXPLORE PROGRAM
Here are full examples for each aggregate function in SQL, including the SQL program
code:
1. COUNT() Example
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 11 of 27
:
);
Output:
total_customers
---------------
3
2. SUM() Example
Task: Calculate the total sales (sum of price) from the Orders table.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 12 of 27
:
-- Calculate the total sales
SELECT SUM(Price) AS total_sales
FROM Orders;
Output:
total_sales
------------
1800.00
3. AVG() Example
Output:
average_salary
---------------
60000.00
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 13 of 27
:
60000.00
4. MIN() Example
Task: Find the youngest employee (minimum age) from the Employees table.
Output:
youngest_employee
-----------------
28
5. MAX() Example
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 14 of 27
:
CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Salary DECIMAL(10, 2)
);
Output:
highest_salary
--------------
75000.00
Aggregate functions in SQL Server calculate a set of values and return a single value.
These functions are commonly used in SQL queries to summarize data and provide
valuable insights. The syntax for using aggregate functions in SQL Server is
straightforward.
SELECT aggregate_function(column_name)
FROM table_name
[WHERE condition];
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 15 of 27
:
[WHERE condition];
These functions are helpful when working with large data sets, as they can help
simplify and speed up the analysis process. SUM, COUNT, AVG, and MAX are
commonly used aggregate functions.
So, understanding the syntax of SQL Server aggregate functions is essential for
anyone working with databases and looking to analyze data ebciently.
APPROX_COUNT_DISTINCT
While the APPROX_COUNT_DISTINCT function does not provide a fully accurate count
of distinct values, it is usually precise enough for most practical purposes. This
practicality is a key function feature, reinforcing its value in everyday SQL tasks.
Syntax:
APPROX_COUNT_DISTINCT ( expression )
AVG
AVG is an aggregate in SQL that returns an average value over a set of numerical
values in a table or column. This function is very important in analytical tasks that
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 16 of 27
:
values in a table or column. This function is very important in analytical tasks that
require getting the mean value for a given data set. The AVG function can always be
used along with other aggregate functions of SQL, like COUNT, SUM, MAX, and MIN.
One of the most signi\cant pros of using AVG is the ability to trace outliers in the
dataset, which are values much larger or smaller than the average. After these outliers
have been ragged, the data analyst has an idea of how the data is distributed, and he
can make better decisions based on the obtained insights.
So, AVG is a powerful SQL function that can perform a wide range of data analysis
tasks. It is a helpful tool when working with large datasets in a database management
system.
Syntax:
CHECKSUM_AGG
CHECKSUM_AGG: This is an aggregate function in SQL that produces a hash value for
a given dataset. The function takes one column or expression and returns a single
checksum value to represent the data in that column or expression. It produces an
integer, which could be used to compare two sets of data for equality or to detect
changes in the data.
CHECKSUM_AGG is often used in data warehousing and other applications where data
integrity is essential. It is a powerful tool for detecting data changes and ensuring that
the data in a database is accurate and up-to-date.
Syntax:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 17 of 27
:
COUNT_BIG
COUNT_BIG, an SQL function, aggregates the number of rows in a table. It differs from
the COUNT function in that it returns the big integer data type. This function is
particularly useful for counting huge datasets where the number of lines exceeds the
maximum value for an integer data type. COUNT_BIG can be used with other SQL
functions to implement complex queries and analyses. The beauty of COUNT_BIG lies
in its simple syntax, making it easily applicable in SQL statements and ensuring a
smooth user experience.
Syntax:
GROUPING
The GROUP BY function is one of SQL's most commonly used aggregate functions.
The GROUP BY function allows you to group rows of data based on one or more
columns and then perform aggregate calculations for each group. For example, you
could use the GROUP BY function to group sales data by month or region and then
calculate the total sales for each group.
The GROUP BY function is commonly used with other aggregate functions, such as
COUNT, SUM, AVG, and MAX/MIN. It allows you to quickly analyze large data sets and
summarize the results meaningfully.
Syntax:
GROUPING ( <column_expression> )
GROUPING_ID
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 18 of 27
:
The GROUPING_ID function enables SQL aggregates to identify a row's grouping level
in a SELECT statement. Depending on the grouping level, the function returns an
integer that uniquely identi\es each row. The values it returns are based on the
columns used in the GROUP BY clause of the particular SELECT statement. If a row
does not belong to any group, the function returns 0; for rows belonging to any group,
it returns a non-zero value.
The GROUPING_ID function is a tool that clari\es complex aggregations over large
data sets. It simpli\es data grouping in a most useful way for the analysis at hand,
clearly indicating which grouping level each row falls into. This clarity is a signi\cant
bene\t for data analysts and business intelligence professionals, giving them a more
con\dent understanding of their data and analysis.
Syntax:
EXPLORE PROGRAM
GROUPING_ID ( <column_expression>[,...n ] )
STDEV
STDEV (Standard Deviation) is a crucial SQL aggregate function used to measure the
variation or dispersion in a data set. It calculates the square root of the variance and is
a valuable tool for analyzing data trends. It can also help identify outliers in a dataset.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 19 of 27
:
a valuable tool for analyzing data trends. It can also help identify outliers in a dataset.
The STDEV function is commonly used in statistical analysis, data mining, and data
science. By understanding how to use the STDEV function, you can gain valuable
insights into your data and make more informed decisions.
Syntax:
Get noticed by top hiring companies through our JobAssist program. Get
complete job assistance post the Full Stack Web Developer - MERN Stack
Developer Course and unleash the endless possibilities. Enroll TODAY!
STDEVP
This function, STDEVP, is not just a theoretical concept. It's a practical tool extensively
used in \nance, engineering, and scienti\c research applications. However, it's
important to note that STDEVP differs from STDEV, as it computes the population
standard deviation, not the sample standard deviation.
Overall, STDEVP is a useful SQL tool that would be of great importance to analysts and
developers seeking valuable insights into their datasets.
Syntax:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 20 of 27
:
STRING_AGG
Syntax:
VAR
The VAR function is powerful and can be used with other SQL functions to derive
insights from large data sets. You provide it with the column or an expression as an
argument containing the values that you are interested in calculating variance with. A
result of this VAR function is a decimal value representing the variance of a dataset.
Syntax:
VARP
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 21 of 27
:
VARP, or Variance Population, is an aggregate function in SQL that calculates the
variance of provided values. The function becomes useful when you want to discover
the spread of a population. You can use the process with other SQL aggregate
functions like SUM, AVG, and COUNT to build more sophisticated calculations.
VARP is a potent tool in data analysis and often comes up in \nance and statistics
when dealing with vast volumes of data. However, it must be noted that it is pretty
different from VAR, the sample variance. Whereas VARP calculates the variance of the
whole population, VAR only makes computations concerning the sample from the
population.
Syntax:
RANGE
The RANGE function is one of the crucial aggregate functions in SQL. It calculates the
range of a set of values, the difference between the set's most prominent and smallest
values.
For example, if a set of values contains 5, 10, 15, 20, and 25, the range would be 20
(25-5). The RANGE function is commonly used in statistical analysis to measure data
spread.
The RANGE function is a powerful tool in SQL that allows you to analyze your data
statistically. By calculating the range of your data, you can gain insights into its spread
and distribution.
NANMEAN
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 22 of 27
:
excluding the NULL values from the calculation reassures the accuracy of your
analysis. This function is particularly useful in handling big data \les with missing or
incomplete data.
MEDIAN
The median is a statistical measure representing a dataset's median value. In SQL, the
median can be calculated using the median() function, an aggregate function that
returns the median value of a group of values.
SELECT MEDIAN(column_name)
FROM table_name
Here, the column_name represents the column's name that contains the values for
which the median needs to be calculated, and table_name represents the name of the
table that contains the data.
The median() function can be combined with other aggregate functions like COUNT,
SUM, AVG, etc., to perform more complex calculations on the data. Overall, the
median() function in SQL is a powerful tool for data analysis and can provide valuable
insights into data distribution in a dataset.
MODE
One of the most used aggregate functions in SQL is the MODE function. The MODE
function returns the value that comes up the most in a set of values. This is very
helpful, speci\cally when dealing with large data sets that need to \nd the most
common values. The syntax for this function is straightforward. It analyzes only one
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 23 of 27
:
common values. The syntax for this function is straightforward. It analyzes only one
parameter: the column's name with the values.
MODE(column_name).
Overall, the MODE function is a powerful tool for data analysis in SQL and can be used
in various contexts to identify patterns and trends.
EXPLORE PROGRAM
Conclusion
The aggregate function in SQL is very powerful in the database. It serves the same
purpose as its equivalents in MS Excel. In this article, we have seen several examples
of aggregate functions in SQL. We hope you will now be clear about what an aggregate
function is in SQL.
If you wish to learn more about SQL, check out our SQL Certi\cation Course. Our SQL
certi\cation course is led by industry experts who will guide you through the basics
and provide practical training to work with SQL databases. By the end of the course,
you will have a work-ready understanding of SQL, ready to apply in your own
applications.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 24 of 27
:
FAQs
Yes, MAX is an aggregate function in SQL. It returns the maximum value in a set of
values. You can use MAX to return the highest value in a column.
An aggregate function in SQL adds together multiple values and returns a single result.
The aggregate function will carry out this calculation for each group of data when used
with the GROUP BY clause. The GROUP BY statement is commonly used with
aggregate functions ( MAX() , MIN() , SUM() , AVG(), COUNT() )
No, aggregate functions cannot be used in a WHERE clause. They are allowed in the
SELECT list and in the HAVING clause.
The difference between COUNT(*) and COUNT(column_name) in SQL lies in how they
count rows:
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 25 of 27
:
COUNT(*): Counts the total number of rows in a table, including those with NULL
values. It doesn't take into account any speci\c column and gives the count of all
rows.
No, aggregate functions cannot be used directly in the ‘WHERE’ clause. The ‘WHERE’
clause is used to \lter rows before any aggregation takes place. If you want to \lter
based on the result of an aggregate function, you should use the ‘HAVING’ clause,
which is designed for this purpose. The ‘HAVING’ clause \lters groups created by the
‘GROUP BY’ clause based on aggregate function results.
4 Months ₹1,10,000
8 months ₹53,999
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 26 of 27
:
7 months ₹40,000
6 months ₹53,999
Recommended Reads
SOFTWARE DEVELOPMENT
A Guide on How to Become a Site The Ultimate Guide on SQL Basics What Is SQL Injection: How to
Reliability Engineer (SRE) Prevent SQL Injection
46160 90218
28 Oct, 2020 23 Jul, 2024 31 Jan, 2025
Acknowledgement
PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project
Management Institute, Inc.
https://www.simplilearn.com/tutorials/sql-tutorial/sql-aggregate-functions 06/03/25, 11 06 AM
Page 27 of 27
: