Aggregate Functions: COUNT, SUM, AVG, MIN & MAX ๐
So far, you've learned how to retrieve, filter, and sort individual rows.
Now we're moving to one of the most important skills for a Data Analyst:
Turning thousands of rows into meaningful business metrics.
For example:
How many customers do we have?
What is our total revenue?
What is the average order value?
What is the highest salary?
What is the lowest product price?
That's exactly what aggregate functions are designed for.
1๏ธโฃ What Are Aggregate Functions?
Aggregate functions perform a calculation across multiple rows and return a summarized result.
The five essential functions are:
โข
COUNT() Counts rows/valuesโข
SUM() Calculates totalโข
AVG() Calculates averageโข
MIN() Finds minimumโข
MAX() Finds maximum2๏ธโฃ COUNT()
COUNT() is used to count records or non-NULL values.Count all rows:
SELECT COUNT(*) AS total_customers
FROM customers;
If there are 5,000 customers: total_customers = 5000
3๏ธโฃ COUNT(*) vs COUNT(column)
This distinction is extremely important.
COUNT(*) Counts rows.SELECT COUNT(*)
FROM employees;
COUNT(column) Counts non-NULL values in that column.SELECT COUNT(manager_id)
FROM employees;
Suppose: employee A | 101, B | 102, C | NULL, D | 103
Then:
COUNT(*) = 4, COUNT(manager_id) = 3Because one manager_id is NULL.
Interview Tip:
COUNT(*)counts rows;COUNT(column)counts non-NULL values in that column.
4๏ธโฃ COUNT(DISTINCT)
Use
COUNT(DISTINCT ...) when you want to count unique values.Example:
SELECT
COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;
Suppose: customer_id 101, 101, 102, 103, 103, 103
Then:
COUNT(*) = 6, COUNT(DISTINCT customer_id) = 3This is extremely common in analytics.
5๏ธโฃ Real-World Example: Active Customers
Suppose your orders table contains thousands of orders.
The business asks:
How many unique customers placed an order?
SELECT
COUNT(DISTINCT customer_id) AS active_customers
FROM orders;
Notice that we're counting customers, not orders. One customer may have placed 20 orders, but should still count as one unique customer.
6๏ธโฃ SUM()
SUM() calculates the total of a numeric column.Example:
SELECT
SUM(amount) AS total_revenue
FROM orders;
If the amounts are: 1000, 2000, 1500, 3000 then: SUM = 7500
7๏ธโฃ SUM With a Condition
You can combine
SUM() with WHERE.Example:
Calculate revenue from completed orders only.
SELECT
SUM(amount) AS completed_revenue
FROM orders
WHERE order_status = 'Completed';
This is a very common business query.
8๏ธโฃ AVG()
AVG() calculates the average of non-NULL numeric values.Example:
SELECT
AVG(salary) AS average_salary
FROM employees;
If salaries are: 50000, 60000, 70000 then: Average = 60000
9๏ธโฃ AVG and NULL Values
AVG() generally ignores NULL values.Suppose: salary 50000, 60000, NULL, 70000
The average is: (50000 + 60000 + 70000) / 3 = 60000
It doesn't divide by 4. This is important when working with incomplete real-world data.
๐ MIN()
MIN() finds the smallest value.Example:
SELECT
MIN(salary) AS lowest_salary
FROM employees;
For products: