Learn how to use Aggregates Window Functions in SQL to perform advanced data analysis and calculations. Discover how these powerful functions enhance performance and help you analyze complex datasets with ease.
Aggregates in SQL Window Functions
In SQL, aggregate functions are used to perform calculations on multiple rows of data and return a single value, like the total sum, average, or count of a column. However, when used as window functions, these aggregates allow you to compute values over a “window” of rows while keeping the original rows intact. This gives you the flexibility to perform calculations within groups of data without losing individual row information.
Common aggregate functions used as window functions include:
SUM()
: Calculates the total sum of a specified column.AVG()
: Computes the average value of a column.MIN()
: Returns the smallest value in a column.MAX()
: Returns the largest value in a column.COUNT()
: Counts the number of rows or non-NULL
values in a column.
Basic Syntax for Window Aggregate Functions
sqlCopy codeSELECT
column1,
column2,
SUM(column2) OVER (PARTITION BY column1 ORDER BY column2) AS SumResult,
AVG(column2) OVER (PARTITION BY column1 ORDER BY column2) AS AvgResult,
MIN(column2) OVER (PARTITION BY column1 ORDER BY column2) AS MinResult,
MAX(column2) OVER (PARTITION BY column1 ORDER BY column2) AS MaxResult,
COUNT(column2) OVER (PARTITION BY column1 ORDER BY column2) AS CountResult
FROM table_name;
Here:
PARTITION BY
divides the result set into groups.ORDER BY
orders the data within those groups.
Examples of Aggregate Window Functions
Let’s assume we have a table of sales data:
SaleDate | SalesAmount | Department |
---|---|---|
2024-01-01 | 500 | Electronics |
2024-01-02 | 200 | Electronics |
2024-01-03 | 300 | Furniture |
2024-01-04 | 700 | Furniture |
2024-01-05 | 600 | Electronics |
2024-01-06 | 400 | Furniture |
1. SUM()
Example (Cumulative Sales)
Let’s calculate the total sales for each department:
SELECT
SaleDate,
SalesAmount,
Department,
SUM(SalesAmount) OVER (PARTITION BY Department ORDER BY SaleDate) AS CumulativeSales
FROM Sales;
SaleDate | SalesAmount | Department | CumulativeSales |
---|---|---|---|
2024-01-01 | 500 | Electronics | 500 |
2024-01-02 | 200 | Electronics | 700 |
2024-01-05 | 600 | Electronics | 1300 |
2024-01-03 | 300 | Furniture | 300 |
2024-01-04 | 700 | Furniture | 1000 |
2024-01-06 | 400 | Furniture | 1400 |
In this example, the SUM(SalesAmount)
is calculated for each department, and the cumulative sales amount increases with each sale within that department.
2. AVG()
Example (Average Sales per Department)
Let’s calculate the average sales amount for each department:
SELECT
SaleDate,
SalesAmount,
Department,
AVG(SalesAmount) OVER (PARTITION BY Department ORDER BY SaleDate) AS AvgSales
FROM Sales;
SaleDate | SalesAmount | Department | AvgSales |
---|---|---|---|
2024-01-01 | 500 | Electronics | 500 |
2024-01-02 | 200 | Electronics | 350 |
2024-01-05 | 600 | Electronics | 466.67 |
2024-01-03 | 300 | Furniture | 300 |
2024-01-04 | 700 | Furniture | 500 |
2024-01-06 | 400 | Furniture | 466.67 |
The AVG(SalesAmount)
computes the average sales within each department up to each row’s position.
3. MIN()
and MAX()
Example (Min and Max Sales in Each Department)
Let’s find the minimum and maximum sales amounts within each department:
SELECT
SaleDate,
SalesAmount,
Department,
MIN(SalesAmount) OVER (PARTITION BY Department) AS MinSales,
MAX(SalesAmount) OVER (PARTITION BY Department) AS MaxSales
FROM Sales;
SaleDate | SalesAmount | Department | MinSales | MaxSales |
---|---|---|---|---|
2024-01-01 | 500 | Electronics | 200 | 600 |
2024-01-02 | 200 | Electronics | 200 | 600 |
2024-01-05 | 600 | Electronics | 200 | 600 |
2024-01-03 | 300 | Furniture | 300 | 700 |
2024-01-04 | 700 | Furniture | 300 | 700 |
2024-01-06 | 400 | Furniture | 300 | 700 |
MIN(SalesAmount)
and MAX(SalesAmount)
show the lowest and highest sales amounts for each department across all rows.
4. COUNT()
Example (Number of Sales Per Department)
Let’s count how many sales transactions occurred within each department:
SELECT
SaleDate,
SalesAmount,
Department,
COUNT(SalesAmount) OVER (PARTITION BY Department) AS NumSales
FROM Sales;
SaleDate | SalesAmount | Department | NumSales |
---|---|---|---|
2024-01-01 | 500 | Electronics | 3 |
2024-01-02 | 200 | Electronics | 3 |
2024-01-05 | 600 | Electronics | 3 |
2024-01-03 | 300 | Furniture | 3 |
2024-01-04 | 700 | Furniture | 3 |
2024-01-06 | 400 | Furniture | 3 |
COUNT(SalesAmount)
counts how many sales entries exist for each department.
Common Interview Questions Based on Aggregates
- What is the difference between
GROUP BY
andPARTITION BY
in SQL?- Answer:
GROUP BY
groups rows together and collapses them into a single summary row per group, whereasPARTITION BY
divides the rows into groups but allows for calculations to be done on each row within those groups (without collapsing them).
- Answer:
- How do you calculate the average sales per month using a window function?
- Answer: Partition by month (and year if necessary) and then calculate the average.
- How would you calculate the running total of sales for each department?
- Answer: Use the
SUM()
window function withPARTITION BY Department
andORDER BY SaleDate
to calculate the cumulative sum.
- Answer: Use the
- What is the use of the
COUNT()
window function in SQL?- Answer:
COUNT()
in a window function counts the number of rows within each partition, providing insights such as how many entries are in each group (e.g., how many sales transactions occurred within each department).
- Answer:
- Can you use window functions with aggregate functions like
SUM()
orAVG()
in SQL?- Answer: Yes, window functions allow you to use aggregate functions like
SUM()
,AVG()
,MIN()
,MAX()
, andCOUNT()
to compute values over a specified window or group of rows, without collapsing the rows into a single value.
- Answer: Yes, window functions allow you to use aggregate functions like