
163 views
MySQL Window Functions
MySQL introduced support for Window Functions in version 8.0. Window Functions are a category of SQL functions that operate on a set of rows related to the current row, allowing you to perform calculations across a “window” of rows. They are particularly useful for tasks like calculating moving averages, cumulative sums, and ranking rows within a result set. Here are some commonly used MySQL Window Functions:
- ROW_NUMBER(): Assigns a unique integer to each row within the result set. Rows receive consecutive integers, starting from 1, based on a specified ordering.
SELECT ROW_NUMBER() OVER (ORDER BY column1) AS row_number, column1, column2
FROM your_table;
- RANK(): Assigns a rank to each row within the result set, allowing for duplicate ranks in case of tied values.
SELECT RANK() OVER (ORDER BY column1) AS rank, column1, column2
FROM your_table;
- DENSE_RANK(): Similar to
RANK()
, but it doesn’t leave gaps in case of tied values. If two rows have the same rank, the next rank is incremented without gaps.
SELECT DENSE_RANK() OVER (ORDER BY column1) AS dense_rank, column1, column2
FROM your_table;
- NTILE(): Divides the result set into the specified number of roughly equal parts and assigns a group number to each row.
SELECT NTILE(4) OVER (ORDER BY column1) AS quartile, column1, column2
FROM your_table;
- LEAD() and LAG(): Retrieve the value of a column from the next row (LEAD) or the previous row (LAG) within the result set, based on a specified order.
SELECT column1, LAG(column1) OVER (ORDER BY column1) AS prev_value, LEAD(column1) OVER (ORDER BY column1) AS next_value
FROM your_table;
- SUM() and AVG() as Window Functions: You can use the SUM() and AVG() functions as window functions to calculate running totals and moving averages, respectively, over a specified window of rows.
SELECT column1, SUM(column1) OVER (ORDER BY column2) AS running_total, AVG(column1) OVER (ORDER BY column2) AS moving_avg
FROM your_table;
MySQL Window Functions offer a powerful way to perform complex analytical tasks without needing subqueries or self-joins. They are particularly helpful in scenarios where you need to perform calculations over subsets of rows within a result set.