Window Functions in SQL: See the Whole Picture Without Losing the Details
- Shunmuga Priya
- Sep 3
- 4 min read
Window Functions
A window function in SQL performs a calculation across a set of rows related to the current row, without changing the number of rows in the result set.
We can get data without losing the level of detail of the row.
Aggregate functions are if you are giving a N number of rows as input it is able to give you One Output.
Window functions are if you are giving N number of rows as input it is able to give you N number of outputs.
Whenever using window functions for sure you need to use a clause called OVER.
The purpose of OVER is to apply for every particular row.

Syntax
SELECT
column_name,
window_function(column_name) OVER (
PARTITION BY column_name
ORDER BY column_name
) AS result_column
FROM table_name;
function_name: The aggregate or ranking function (e.g., SUM, AVG, ROW_NUMBER, etc.).
PARTITION BY: Divides the result set into partitions to apply the function separately to each partition.
ORDER BY: Specifies the order of rows within each partition.
frame_clause: Defines a subset of rows for the calculation (e.g., ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).
Aggregate Functions
Aggregate window functions calculate aggregates over a window of rows while retaining individual rows. Common aggregate functions include:
SUM(): Sums values within a window.
AVG(): Calculates the average value within a window.
COUNT(): Counts the rows within a window.
MAX(): Returns the maximum value in the window.
MIN(): Returns the minimum value in the window.
Example: Sales Data Analysis
Imagine a table sales with the following structure
Query: Calculate Total and Average Sales per Region
SELECT
region,
salesperson,
amount,
SUM(amount) OVER (PARTITION BY region) AS total_sales_per_region,
AVG(amount) OVER (PARTITION BY region) AS avg_sales_per_region
FROM sales;
Result
Explanation
SUM(amount) OVER (PARTITION BY region)
Calculates the total sales for each region (East and West) without grouping the rows.
AVG(amount) OVER (PARTITION BY region):
Calculates the average sales for each region.
1. ROW_NUMBER()
Assigns a unique sequential number to each row, starting from 1, without skipping numbers, even if there are ties.
Example:
SELECT Name, Score, ROW_NUMBER() OVER (ORDER BY Score DESC) AS RowNumber
FROM Students;
Input
Output
2.RANK()
Assigns a rank to rows, but skips numbers if there are ties.
Example:
SELECT Name, Score, RANK() OVER (ORDER BY Score DESC) AS Rank
FROM Students;
Output
3. DENSE_RANK()
Similar to RANK(), but does not skip numbers when there are ties.
Example:
SELECT Name, Score, DENSE_RANK() OVER (ORDER BY Score DESC) AS DenseRank
FROM Students;
Output
4.PERCENT_RANK()
1.Calculates the relative rank of a row as a percentage of the total rows, ranging from 0 to
Example
SELECT Name, Score, PERCENT_RANK() OVER (ORDER BY Score DESC) AS PercentRank
FROM Students;
Output
5.NTILE(n)
Divides rows into n equal groups and assigns a group number to each row.
Example:
SELECT Name, Score, NTILE(2) OVER (ORDER BY Score DESC) AS NTile
FROM Students;
Output
1. LAG()
The LAG() function retrieves the value of a column from a previous row within the same result set.
Example
SELECT
employee_id,
salary,
LAG(salary) OVER (ORDER BY employee_id) AS previous_salary
FROM employees;
Output
2. LEAD()
The LEAD() function retrieves the value of a column from the next row within the same result set.
Example:
SELECT
employee_id,
salary,
LEAD(salary) OVER (ORDER BY employee_id) AS next_salary
FROM employees;
Output
3. FIRST_VALUE()
The FIRST_VALUE() function retrieves the first value in a window or partition.
Example
SELECT
department_id,
employee_id,
salary,
FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary) AS lowest_salary
FROM employees;
Output
4. LAST_VALUE()
The LAST_VALUE() function retrieves the last value in a window or partition.
Example:
SELECT
department_id,
employee_id,
salary,
LAST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS highest_salary
FROM employees;
Output
Conclusion


