Window functions in SQL allow you to perform calculations across a set of rows that are related to the current query row without collapsing the result set. Unlike aggregate functions, which summarize data by grouping rows (and thus reduce the number of rows), window functions retain the original row identities and add new columns with computed values.
Key Points:
Window Functions perform calculations over a range of rows (the "window") related to the current row.
They use the OVER() clause to define the window.
Can include clauses like PARTITION BY (to divide rows into groups) and ORDER BY (to order rows within each partition).
Common window functions include ROW_NUMBER(), RANK(), DENSE_RANK(), SUM(), AVG(), and more.
Why Use Window Functions?
To calculate running totals or cumulative sums.
To assign ranks to rows without aggregating data.
To perform calculations that require reference to other rows while keeping the detail of each individual row.
Example Scenario:
Suppose we have a sales table recording sales transactions:
Compute a running total of all sales amounts ordered by highest sale first.
Using Window Functions:
sql
SELECT
salesperson,
amount,
SUM(amount) OVER (PARTITION BY salesperson) AS total_sales_per_person,
SUM(amount) OVER (ORDER BY amount DESC) AS running_total_sales
FROM
sales
ORDER BY
amount DESC;
Explanation:
SUM(amount) OVER (PARTITION BY salesperson)
Calculates the total sales for each salesperson.
The PARTITION BY clause groups the rows by salesperson but doesn't collapse them, so you still see individual rows.
SUM(amount) OVER (ORDER BY amount DESC)
Calculates a running total of sales amounts, starting from the highest amount.
The ORDER BY amount DESC inside the OVER() clause orders the rows for the cumulative sum.
Result:
salesperson
amount
total_sales_per_person
running_total_sales
Bob
300.00
450.00
300.00
Alice
200.00
450.00
500.00
Alice
150.00
450.00
650.00
Bob
150.00
450.00
800.00
Alice
100.00
450.00
900.00
Charlie
50.00
50.00
950.00
Interpretation:
total_sales_per_person shows each salesperson's total sales, repeated on each of their rows.
running_total_sales shows the cumulative total sales amount, ordered from the highest individual sale downwards.
Benefits:
You retain all detailed transaction rows while adding insightful calculations.
Avoid complex subqueries and joins that are typically required to get similar results.
More efficient and readable SQL code.
Additional Example: Calculating Ranks
Suppose you want to rank salespeople based on their total sales.
sql
SELECT
salesperson,
SUM(amount) AS total_sales,
RANK() OVER (ORDER BY SUM(amount) DESC) AS sales_rank
FROM
sales
GROUP BY
salesperson;
Result:
salesperson
total_sales
sales_rank
Alice
450.00
1
Bob
450.00
1
Charlie
50.00
3
Note: Since both Alice and Bob have the same total sales, they share the same rank.
Summary:
Window functions are powerful for advanced data analysis within SQL queries.
They enhance your ability to perform calculations across rows while preserving individual row data.
Useful in reporting, analytics, and any scenario where you need more than what standard aggregate functions provide.
By understanding and utilizing window functions, you can write more efficient and expressive SQL queries that provide deeper insights into your data.