Aggregate Functions — COUNT, SUM, AVG, MAX, MIN
Collapse many rows into a single summary number — the foundation of every report and dashboard.
Aggregate functions turn a list of rows into one answer. COUNT rows, SUM values, find the AVG, MAX, and MIN.
EXERCISE
1COUNT, SUM & AVG
Aggregate functions collapse many rows into a single summary number. COUNT, SUM, and AVG are the three you will use in almost every analytical query.
COUNT — how many rows:
-- Count all rows in the orders table:
SELECT COUNT(*) AS total_orders FROM orders;
-- Count only rows where a column is NOT NULL:
SELECT COUNT(phone) AS customers_with_phone FROM customers;
-- Skips NULL values — only counts rows where phone has a value
-- Count unique values:
SELECT COUNT(DISTINCT city) AS unique_cities FROM customers;
SUM — add up values:
-- Total revenue from all orders:
SELECT SUM(total) AS total_revenue FROM orders;
-- Total stock value:
SELECT SUM(price * stock_qty) AS total_stock_value FROM products;
-- Sum with a condition:
SELECT SUM(total) AS completed_revenue
FROM orders
WHERE status = 'completed';
AVG — the average:
-- Average order value:
SELECT AVG(total) AS avg_order_value FROM orders;
-- Average salary by department (combined with GROUP BY):
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
-- Round to 2 decimal places:
SELECT ROUND(AVG(total), 2) AS avg_order_value FROM orders;
Pro Tip:
COUNT(*)counts all rows including NULLs.COUNT(column_name)skips NULLs. This matters when your column has missing values — the two can give very different numbers.
EXERCISE
2MIN, MAX & Combining Aggregates
MIN and MAX find the smallest and largest values. You can combine multiple aggregate functions in a single query to build a complete summary in one shot.
MIN and MAX:
-- Cheapest and most expensive products:
SELECT MIN(price) AS cheapest, MAX(price) AS most_expensive
FROM products;
-- Earliest and latest orders:
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;
-- Works on text too (alphabetical min/max):
SELECT MIN(name) AS first_alphabetically, MAX(name) AS last_alphabetically
FROM customers;
Combining all aggregates in one query:
-- Full summary of orders table:
SELECT
COUNT(*) AS total_orders,
COUNT(DISTINCT customer_id) AS unique_customers,
SUM(total) AS total_revenue,
AVG(total) AS avg_order_value,
MIN(total) AS smallest_order,
MAX(total) AS largest_order,
MIN(order_date) AS first_order_date,
MAX(order_date) AS last_order_date
FROM orders
WHERE status = 'completed';
ROUND — controlling decimal places:
SELECT
ROUND(AVG(salary), 0) AS avg_salary_rounded, -- 0 decimals
ROUND(AVG(price), 2) AS avg_price_2dp, -- 2 decimal places
FLOOR(AVG(total)) AS avg_order_floor, -- round down
CEIL(AVG(total)) AS avg_order_ceil -- round up
FROM orders;
Pro Tip: All aggregate functions except COUNT(*) ignore NULL values automatically. If a salary column has 10 rows but 2 are NULL, AVG(salary) divides by 8, not 10. This can silently skew your results.
EXERCISE
3Aggregate Functions with WHERE
You can filter rows before aggregating with WHERE. But you cannot filter on aggregate results with WHERE — that is what HAVING is for.
WHERE filters before aggregation:
-- Total revenue from completed orders only:
SELECT SUM(total) AS completed_revenue
FROM orders
WHERE status = 'completed'; -- WHERE runs BEFORE SUM
-- Average salary of Engineering department:
SELECT AVG(salary) AS eng_avg_salary
FROM employees
WHERE department = 'Engineering';
The execution order matters:
-- SQL logical order of execution:
-- 1. FROM (which table)
-- 2. WHERE (filter rows)
-- 3. GROUP BY (group the remaining rows)
-- 4. HAVING (filter groups)
-- 5. SELECT (calculate output)
-- 6. ORDER BY (sort)
-- 7. LIMIT (cap rows)
What you CANNOT do with WHERE:
-- This will ERROR — you can't use aggregate functions in WHERE:
SELECT department, AVG(salary)
FROM employees
WHERE AVG(salary) > 60000; -- ERROR: Invalid use of group function
-- Correct way — use HAVING (covered next lesson):
SELECT department, AVG(salary) AS avg_sal
FROM employees
GROUP BY department
HAVING AVG(salary) > 60000;
Multiple aggregates with the same filter:
SELECT
COUNT(*) AS orders_today,
SUM(total) AS revenue_today,
AVG(total) AS avg_order_today
FROM orders
WHERE DATE(created_at) = CURDATE();
Pro Tip: Knowing SQL''s logical execution order is one of the most important mental models in SQL. WHERE always runs before aggregation, which is why it can''t reference aggregate results.
