SQL COUNT AVG SUM
SQL

SQL COUNT, AVG, SUM — How to Perform Calculations on Data

CHAPTER 14 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL COUNT, AVG, and SUM functions are aggregate functions that perform calculations across multiple rows and return a single result. COUNT() counts rows, AVG() calculates averages, and SUM() adds up values. In this chapter, you will learn how to use all three with practical examples.

SQL COUNT() Function

COUNT() returns the number of rows that match a condition:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

Count all rows:

SQL
SELECT COUNT(*) AS TotalEmployees
FROM Employees;
✓ OUTPUT
+----------------+
| TotalEmployees |
+----------------+
| 6              |
+----------------+

Count employees in IT:

SQL
SELECT COUNT(*) AS ITCount
FROM Employees
WHERE Department = 'IT';
✓ OUTPUT
+---------+
| ITCount |
+---------+
| 2       |
+---------+

💡 COUNT(*) vs COUNT(column)

COUNT(*) counts all rows including those with NULL values. COUNT(column) counts only rows where that column is NOT NULL. Use COUNT(*) for total row counts and COUNT(column) when you need to exclude NULLs.

SQL SUM() Function

SUM() adds up all values in a numeric column:

SQL
SELECT SUM(Salary) AS TotalPayroll
FROM Employees;
✓ OUTPUT
+--------------+
| TotalPayroll |
+--------------+
| 376000       |
+--------------+

Total salaries for Sales department: Use the WHERE clause to filter before summing:

SQL
SELECT SUM(Salary) AS SalesPayroll
FROM Employees
WHERE Department = 'Sales';
✓ OUTPUT
+--------------+
| SalesPayroll |
+--------------+
| 106000       |
+--------------+

SQL AVG() Function

AVG() calculates the average (mean) of all values in a numeric column:

SQL
SELECT AVG(Salary) AS AvgSalary
FROM Employees;
✓ OUTPUT
+------------+
| AvgSalary  |
+------------+
| 62666.6667 |
+------------+

Combining All Three

You can use all aggregate functions together with MIN and MAX (covered in the previous chapter) for a complete data summary:

SQL — Complete Summary
SELECT
  COUNT(*) AS TotalEmployees,
  SUM(Salary) AS TotalPayroll,
  AVG(Salary) AS AvgSalary,
  MIN(Salary) AS MinSalary,
  MAX(Salary) AS MaxSalary
FROM Employees;
✓ OUTPUT
+--------+---------+------------+-----------+-----------+
| Total  | Payroll | AvgSalary  | MinSalary | MaxSalary |
+--------+---------+------------+-----------+-----------+
| 6      | 376000  | 62666.6667 | 51000     | 78000     |
+--------+---------+------------+-----------+-----------+

Aggregate Functions Summary

Function What It Does Ignores NULL?
COUNT(*) Counts all rows No
COUNT(col) Counts non-NULL values in column Yes
SUM(col) Adds up all values Yes
AVG(col) Calculates average of all values Yes
MIN(col) Finds smallest value Yes
MAX(col) Finds largest value Yes

For a complete reference of all aggregate functions, see the official MySQL aggregate functions documentation. These functions become even more powerful when combined with GROUP BY, which you will learn in Chapter 25.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that shows the total number of employees in Mumbai, their total salary, and their average salary.

ANSWER
SELECT
  COUNT(*) AS MumbaiCount,
  SUM(Salary) AS MumbaiTotal,
  AVG(Salary) AS MumbaiAvg
FROM Employees
WHERE City = 'Mumbai';

Expected Output:

+-------------+-------------+-----------+
| MumbaiCount | MumbaiTotal | MumbaiAvg |
+-------------+-------------+-----------+
| 2           | 127000      | 63500     |
+-------------+-------------+-----------+

📝 What You Learned in This Chapter

  • COUNT() counts rows — use COUNT(*) for all rows, COUNT(col) for non-NULL values
  • SUM() adds up all numeric values in a column
  • AVG() calculates the average of numeric values
  • All aggregate functions can be combined with WHERE to filter first
  • SUM, AVG, MIN, and MAX all ignore NULL values automatically
  • Always use aliases with AS for readable output column names

Frequently Asked Questions

What is an aggregate function in SQL?
An aggregate function performs a calculation on a set of values and returns a single result. The most common are COUNT, SUM, AVG, MIN, and MAX. They collapse multiple rows into one summary row.
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts all rows regardless of NULL values. COUNT(column) counts only rows where that specific column has a value (is not NULL). Use COUNT(*) for total row counts and COUNT(column) when NULLs should be excluded.
Does AVG include NULL values?
No. AVG ignores NULL values in both the sum and the count. If salaries are 50000, NULL, 70000, the average is (50000+70000)/2 = 60000, not (50000+0+70000)/3.
Can I use SUM on text columns?
No. SUM and AVG only work on numeric columns. Using SUM on a text column will cause an error. COUNT, MIN, and MAX work on both numeric and text columns.
How do aggregate functions work with GROUP BY?
GROUP BY divides rows into groups, and the aggregate function calculates a result for each group separately. For example, SELECT Department, AVG(Salary) FROM Employees GROUP BY Department shows the average salary per department. GROUP BY is covered in detail in Chapter 25.

📖 Chapter 14 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com