What are window functions in SQL?

Answer

Window functions perform calculations across a set of rows related to the current row (the "window"), without collapsing them into groups like GROUP BY. Syntax: function() OVER (PARTITION BY col ORDER BY col ROWS/RANGE ...). Common functions: ROW_NUMBER() — sequential row number in partition; RANK() / DENSE_RANK() — rank with/without gaps for ties; LAG(col, n) / LEAD(col, n) — access previous/next row values; SUM() OVER () — running total; NTILE(n) — divide rows into n buckets. Window functions are invaluable for analytics: calculating running totals, percentile rankings, and comparing rows to their peers.