Aggregate Functions
COUNT, SUM, AVG, MIN, MAX — summarise your data
✅ What You Will Learn
Aggregate functions perform calculations across multiple rows and return a single value. They are the foundation of all summary reports — total sales, average order value, count of customers, highest and lowest prices.
Without aggregate functions, you can only see individual rows. With them, you can answer "how many?", "how much total?", "what is the average?", "what is the highest/lowest?" — the questions that actually drive business decisions.
📋 The orders table used in examples
| order_id | customer_name | product | amount | city |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | Delhi |
| 1002 | Priya Verma | Mobile Phone | 18000 | Noida |
| 1003 | Amit Kumar | Headphones | 3500 | Gurgaon |
| 1004 | Sneha Kapoor | Laptop | 52000 | Delhi |
| 1005 | Vikram Singh | Tablet | 28000 | Noida |
Syntax
Examples
📌 Key Points to Remember
- ✓Aggregate functions collapse many rows into one summary value
- ✓COUNT(*) counts all rows; COUNT(col) skips NULLs
- ✓SUM and AVG only work on numeric columns
- ✓MIN and MAX work on numbers, text (alphabetical), and dates
- ✓You can combine multiple aggregates in one SELECT
🏢 Real-World Application
Aggregate functions are the backbone of every business report. Monthly revenue reports use SUM(amount). Customer lifetime value uses SUM(amount) per customer. Average order value — a key e-commerce metric — uses AVG(order_total). Headcount reporting uses COUNT(employee_id). Inventory management uses MIN(stock_qty) to find items running low. Data analysts at Meesho, Nykaa, Urban Company, and every other analytics-driven company write aggregate queries dozens of times per day to answer questions like "What was our highest sale today?" (MAX) and "How many new users signed up this week?" (COUNT).
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What are aggregate functions in SQL?
Aggregate functions perform a calculation on a set of rows and return a single value. The five main aggregate functions are COUNT (count rows), SUM (total of values), AVG (average), MIN (smallest value), and MAX (largest value).
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts all rows in the result, including rows with NULL values. COUNT(column_name) counts only rows where that specific column is not NULL. Use COUNT(*) for total rows, COUNT(col) to count non-empty entries.
Can I use multiple aggregate functions in one SELECT?
Yes. SELECT COUNT(*), SUM(amount), AVG(amount), MIN(amount), MAX(amount) FROM orders is a valid query that returns five aggregated values in one result row.
What does AVG() do with NULL values?
AVG() ignores NULL values — it calculates the average of only the non-NULL rows. This is important to understand because it means AVG may not reflect the true mean if NULLs represent zero rather than missing data.
How do I count distinct values in SQL?
Use COUNT(DISTINCT column_name). For example, SELECT COUNT(DISTINCT customer_name) FROM orders counts how many unique customers placed orders, not how many orders were placed.
✏️ Practice Exercise
Write a query to find the total revenue, number of orders, and highest single order amount for orders placed after 2026-01-15.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.