SQL Server Interview Question #91

What is a window function?

Aggregate Functions, GROUP BY & Window Functions Mid-Level Intermediate

Detailed Explanation

A window function performs a calculation across a set of rows related to the current row while normally preserving each individual row in the result.

This differs from GROUP BY, which collapses multiple input rows into one row per group. Window functions use the OVER clause and include ranking functions, aggregate window functions, and analytic functions such as LAG and LEAD.

Code Example

SELECT Id,
       Name,
       Price,
       AVG(Price) OVER () AS OverallAveragePrice
FROM dbo.Products;