SQL Server Interview Question #83

What does SUM() do?

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

Detailed Explanation

SUM calculates the total of a numeric expression over qualifying rows. It can be used over an entire result set or per group.

SUM ignores NULL input values. The returned data type depends on the input expression, so developers should pay attention to numeric range and precision when aggregating large financial or transactional datasets.

Code Example

SELECT CustomerId,
       SUM(TotalAmount) AS TotalPurchased
FROM dbo.Orders
GROUP BY CustomerId;