Window functions in SQL are an essential tool for performing advanced calculations across a subset of rows in a query result. Understanding their syntax in depth is crucial to leverage their full power.
Window functions are a type of SQL function that operate on a window of rows. Unlike aggregate functions, they do not collapse rows into a single result. Instead, they compute values for each row while retaining the row-level details.
The proper use of syntax ensures that the calculations are accurate and efficient. A clear understanding of each part of the syntax enables developers to write powerful queries for complex scenarios.
The general syntax for a window function is as follows:
function_name(expression)
OVER (
[PARTITION BY column_name]
[ORDER BY column_name]
[ROWS|RANGE BETWEEN frame_start AND frame_end]
)
function_name(expression)
: The window function to apply, such as SUM
, AVG
, RANK
, etc.OVER
: Indicates that this is a window function.PARTITION BY
: Divides the dataset into partitions.ORDER BY
: Specifies the order of rows within each partition.ROWS|RANGE BETWEEN
: Defines the subset of rows in the window frame.The function to be applied within the window. Common functions include:
SUM
, AVG
, COUNT
, MAX
, MIN
RANK
, DENSE_RANK
, ROW_NUMBER
LAG
, LEAD
, FIRST_VALUE
, LAST_VALUE
SELECT EmployeeID,
SUM(Salary) OVER () AS TotalSalary
FROM Employees;
The OVER
clause defines the window of rows for the function. It may contain PARTITION BY
, ORDER BY
, or both.
SELECT Department, EmployeeID,
RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS Rank
FROM Employees;
Department | EmployeeID | Rank |
---|---|---|
HR | 101 | 1 |
HR | 102 | 2 |
Sales | 201 | 1 |
Sales | 202 | 2 |
Used to divide the dataset into partitions. Each partition is processed independently.
SELECT Department,
EmployeeID,
AVG(Salary) OVER (PARTITION BY Department) AS AvgSalaryByDept
FROM Employees;
Specifies the order of rows within each partition, which is critical for ranking and cumulative calculations.
SELECT EmployeeID,
SUM(Salary) OVER (ORDER BY HireDate) AS RunningTotal
FROM Employees;
The ROWS
or RANGE
clause defines the subset of rows to include in the calculation.
SELECT EmployeeID,
SUM(Salary) OVER (ORDER BY HireDate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CumulativeSalary
FROM Employees;
EmployeeID | CumulativeSalary |
---|---|
101 | 5000 |
102 | 12000 |
103 | 18000 |
PARTITION BY
and ORDER BY
If omitted, the function operates on the entire dataset as a single partition.
SELECT EmployeeID,
COUNT(*) OVER () AS TotalEmployees
FROM Employees;
You can use multiple window functions in the same query.
SELECT EmployeeID,
SUM(Salary) OVER () AS TotalSalary,
RANK() OVER (ORDER BY Salary DESC) AS SalaryRank
FROM Employees;
SELECT EmployeeID,
SUM(Salary) OVER (PARTITION BY Department) AS TotalDeptSalary
FROM Employees;
SELECT EmployeeID,
RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DeptRank
FROM Employees;
SELECT EmployeeID,
AVG(Salary) OVER (ORDER BY HireDate ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS MovingAvg
FROM Employees;
The syntax of window functions in SQL is both flexible and powerful, enabling advanced analytics without collapsing data. By understanding the syntax and components deeply, you can leverage window functions for a wide range of scenarios, from simple aggregations to complex trend analyses. Happy Coding!❤️