SUM()
The SUM() function returns the sum of rows from a column selected in the query.Let
motivate this with an example.
The query below returns a subset of the products table inlinecode>category, price, cost, stock_quantity
SELECT category, price, cost, stock_quantity
FROM products
LIMIT 20;
SUM()
Suppose we want to compute the potential profit we could make for Laptops. We can do this by taking the
difference between cost and price, multiply it
by stock_quantity and adding the values.
SELECT sum((price - cost) * stock_quantity) As expected_profit
FROM products;
AVG()
The AVG() function returns the numeric average of the number of rows selected in the query. In the example below,
we compute average profit per unit
SELECT avg((price - cost) * stock_quantity) As expected_avg_profit
FROM products;