Back to results for “Window Functions”
SQL Server › Window Functions

What is a window function in SQL Server, and how is it different from a regular aggregate function used with GROUP BY?

Beginner

Answer

A window function performs a calculation across a set of related rows, similar to an aggregate function, but unlike GROUP BY, it does not collapse those rows into a single summarized result, instead returning a value for every individual row while still having access to information from the related rows around it.
SELECT OrderId, CustomerId, Amount,
  SUM(Amount) OVER (PARTITION BY CustomerId) AS CustomerTotal
FROM Orders;
Real-world example A sales report shows every individual order alongside that customer's overall total spending, using a window function to calculate the total without collapsing the individual order details the way a regular GROUP BY would.

Common follow-ups: What does the OVER clause actually control in a window function?;Can window functions be combined with regular aggregate functions in the same query?