SQL Window Functions are powerful analytical tools that perform calculations across a set of rows related to the current row. They provide a way to analyze data within a specific "window" or partition of rows, enabling advanced data analysis and reporting capabilities.
Window functions operate on a window frame, which is a set of rows defined by a partition and an ordering. They allow you to perform calculations like running totals, rankings, and moving averages without the need for complex self-joins or subqueries.
The general syntax for a window function is:
function_name(expression) OVER (
[PARTITION BY column1, column2, ...]
[ORDER BY column3, column4, ...]
[frame_clause]
)
Assigns a unique integer to each row within a partition.
SELECT
employee_name,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
Assign a rank to each row within a partition. RANK() leaves gaps for ties, while DENSE_RANK() doesn't.
SELECT
product_name,
category,
price,
RANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank,
DENSE_RANK() OVER (PARTITION BY category ORDER BY price DESC) AS dense_price_rank
FROM products;
Access data from a previous or subsequent row in the same result set.
SELECT
order_date,
total_sales,
LAG(total_sales) OVER (ORDER BY order_date) AS previous_day_sales,
LEAD(total_sales) OVER (ORDER BY order_date) AS next_day_sales
FROM daily_sales;
You can use SQL Aggregate Functions as window functions to perform calculations over a window of rows.
SELECT
product_name,
category,
sales,
SUM(sales) OVER (PARTITION BY category) AS total_category_sales,
sales / SUM(sales) OVER (PARTITION BY category) AS sales_percentage
FROM product_sales;
SQL Window Functions are invaluable tools for data analysis and reporting. They provide a powerful way to perform complex calculations across related rows without the need for cumbersome self-joins or subqueries. By mastering window functions, you can significantly enhance your SQL skills and tackle advanced analytical tasks with ease.