Window Function in SQL
A window function in SQL performs calculations across a set of table rows related to the current row, without reducing the number of rows returned by the query. It operates within a "window" of rows defined by a specific partition or ordering, specified by the OVER
clause. Common window functions include ROW_NUMBER()
, RANK()
, LEAD()
, and LAG()
, which assign row numbers, rankings, and access values from other rows within the window. Window functions enable advanced analytics like running totals, moving averages, and cumulative sums, enhancing query flexibility and data analysis capabilities in SQL.