Totals and averages with SUM and AVG
SUM adds up a column and AVG works out its average (the mean):
SELECT SUM(budget) AS total_budget, AVG(budget) AS average_budget
FROM marketing_campaignsBoth ignore NULLs. AVG divides by the number of values that aren't NULL, not by the number of
rows. Usually that's what you want, but it's worth knowing.
You can aggregate a calculation, too. This is the cost of all the stock on the shelves:
SELECT SUM(stock_qty * unit_cost) AS stock_value
FROM productsAverages often have long decimals: wrap them in ROUND(…, 2) for a tidy result.