T-SQL reference › SUM and AVG

SUM and AVG in SQL Server

SUM ignores NULL. AVG ignores NULL too, and does not count those rows in the divisor. Empty set: SUM is NULL, not zero. Wrap ISNULL if the sheet needs 0.

Finance three

SELECT COUNT(*) AS orders,
       ISNULL(SUM(total_amount), 0) AS revenue,
       AVG(CAST(total_amount AS decimal(12,2))) AS avg_order
FROM Orders
WHERE status = 'Shipped';

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Three numbers for shipped orders, all time: how many, total revenue, average order.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets