SQL SUM, AVG, MIN and MAX
Aggregate functions turn many rows into one number. SUM adds, AVG averages, MIN and MAX return the lowest and highest value. All of them ignore NULLs.
How do you use them together?
SELECT COUNT(*) AS orders, SUM(amount) AS revenue, ROUND(AVG(amount), 2) AS avg_order, MIN(order_date) AS first_order, MAX(order_date) AS last_orderFROM orders;
MIN and MAX also work on dates and text.
Why is my average a whole number?
In some databases, such as SQL Server and PostgreSQL, dividing two integers gives an integer: SUM(x) / COUNT(*) can drop the decimals. Use AVG, or multiply by 1.0 first. SQLite's AVG always returns a decimal number.
How do you find the row with the maximum?
MAX(amount) gives the value, not the order. To get the whole row:
SELECT * FROM ordersWHERE amount = (SELECT MAX(amount) FROM orders);
Why don't the totals match my spreadsheet?
Usually NULLs (ignored), duplicates from a join (counted twice), or a filter in WHERE that you forgot about.
Practise AVG and SUM Practise MIN and MAX
Updated November 2026.