Window Function
A SQL function that performs calculations across a set of rows related to the current row without collapsing them.
In Depth
Window functions (also called analytic functions) perform calculations across a set of table rows that are somehow related to the current row—unlike aggregate functions which collapse rows into a single result, window functions retain all rows in the output. Common window functions include ROW_NUMBER() (assigns sequential numbers), RANK() and DENSE_RANK() (ranking with and without gaps), LAG() and LEAD() (access previous/next row values), SUM() OVER (running totals), and AVG() OVER (moving averages). Window functions use the OVER clause to define the window (PARTITION BY for grouping, ORDER BY for ordering, and ROWS/RANGE for frame boundaries). They are essential for time-series analysis, leaderboards, percentile calculations, and year-over-year comparisons.
How AI for Database Helps
AI for Database generates complex window functions automatically when you ask for rankings, running totals, or period-over-period comparisons.
Ready to try AI for Database?
Query your database in plain English. No SQL required. Start free today.