SQL Server aggregate functions are a way of summarizing your data into more meaningful business insights. Analyzing sales trends, customer interaction, or operations is made all the more effective if you can master these. This blog shall introduce you to some aggregate functions available, both simple and advanced ones using an example of the company, Intellipaat.
Table of Contents
What Are Aggregate Functions in SQL Server?
Aggregate functions compute a value across a set of values and return the result. Probably the most common SQL Server aggregate function is:
- MAX(): Returns the highest number.
- SUM(): It adds up the values in a column.
- AVG(): It returns the average value of any column.
- COUNT(): Returns number of rows from the result set.
- MIN(): It returns the smallest value.
Using Aggregate Functions in SQL Server
Let’s assume Intellipaat has a sales database with the following table structure:
SalesID |
Product |
Amount |
SaleDate |
Region |
1 |
Laptop |
1200 |
2024-01-10 |
North |
2 |
Smartphone |
800 |
2024-01-12 |
South |
3 |
Laptop |
1500 |
2024-01-15 |
North |
4 |
Tablet |
600 |
2024-01-18 |
East |
5 |
Smartphone |
700 |
2024-01-20 |
East |
Total Sales Calculation
To calculate the total sales for all products, you can use the SUM() function:
SELECT SUM(Amount) AS TotalSales
FROM Intellipaat_Sales;
The result will show:
Average Sales Calculation
To find the average sales for all products:
SELECT AVG(Amount) AS AverageSales
FROM Intellipaat_Sales;
The result:
Sales by Region
To calculate total sales by region, you can use the GROUP BY clause:
SELECT Region, SUM(Amount) AS TotalSales
FROM Intellipaat_Sales
GROUP BY Region;
The result:
Region |
TotalSales |
North |
2700 |
South |
800 |
East |
1300 |
Using Aggregate Functions with GROUP BY
GROUPING SETS allows you to specify exactly which groupings you want to include. This provides more control over your summary results.
Region |
Product |
TotalSales |
North |
Laptop |
1200 |
South |
Smartphone |
800 |
East |
Tablet |
600 |
East |
Smartphone |
700 |
North |
NULL |
2700 |
South |
NULL |
800 |
NULL |
Laptop |
2700 |
NULL |
Smartphone |
1500 |
NULL |
NULL |
5800 |
Explanation: GROUPING SETS allows you to specify exactly what combinations of your data you want in a more flexible fashion than ROLLUP and CUBE.
Using Aggregate Functions with ROLLUP: Multilevel-hierarchical
The ROLLUP operator is applied to generate summary rows at several levels of aggregation. It becomes useful when the need is to see how data breaks up at different levels.
Example with ROLLUP:
SELECT Region, Product, SUM(Amount) AS TotalSales
FROM Intellipaat_Sales
GROUP BY ROLLUP(Region, Product);
Output:
Region |
Product |
TotalSales |
North |
Laptop |
1200 |
North |
Laptop |
2700 |
South |
Smartphone |
800 |
South |
NULL |
800 |
East |
Tablet |
600 |
East |
Smartphone |
700 |
East |
NULL |
1300 |
NULL |
NULL |
5800 |
Explanation: The ROLLUP generates summary rows at multiple levels: total sales by Region and Product, and a grand total for all regions.
Using Aggregate Functions with CUBE: Multi-Dimensional Aggregation
CUBE operator is stronger than ROLLUP as it computes sums for all the possible combinations of columns included in grouping. It’s suitable for multi-dimensional analysis.
Example with CUBE:
SELECT Region, Product, SUM(Amount) AS TotalSales
FROM Intellipaat_Sales
GROUP BY CUBE(Region, Product);
Output:
Region |
Product |
TotalSales |
North |
Laptop |
1200 |
South |
Smartphone |
800 |
East |
Tablet |
600 |
East |
Smartphone |
700 |
North |
NULL |
2700 |
South |
NULL |
800 |
NULL |
Laptop |
2700 |
NULL |
Smartphone |
1500 |
NULL |
NULL |
5800 |
Explanation:The CUBE operator will return total sales for each unique combination of Region and Product along with the total of each dimension individually.
Conclusion
Mastering all these basic aggregate and advanced SQL Server aggregate functions would greatly aid the ability of reporting and analytics based on the business data analysis. Whether in terms of quite simple sales reporting or multi-dimensional analysis, powerful aggregate functions with a wide usage base are all over SQL Server to meet diverse requirements.
Understand the effective usage of these functions in order to help you create advanced business insights at Intellipaat that will go in the direction of data-driven decision-making.
These include functions such as ROLLUP, CUBE, GROUP BY, and window functions that take your SQL querying skills to the next level in solving complex business problems and creating advanced reports.
Our SQL Courses Duration and Fees
Cohort starts on 16th Feb 2025
₹15,048
Cohort starts on 23rd Feb 2025
₹15,048