SQL COUNT(), AVG() and SUM() Function
Last Updated : 13 Aug, 2025
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical and statistical analysis on data. They allow you to:
- Calculate totals
- Find averages
- Count specific rows
These functions are commonly used for data analytics and reporting, enabling deeper insights into datasets.
SQL COUNT() Function
The COUNT() function provides the number of rows that match a specified condition. It is often used to determine:
- Total records in a table
- Records that meet a specific filter
Syntax:
SELECT COUNT(column_name) FROM table_name WHERE condition;
SQL AVG() Function
The AVG() function provides the average value of a numeric column, helping you determine central tendencies in your data. This is useful for understanding the mean value of a set of numbers, such as salaries, prices, or scores.
Syntax:
SELECT AVG(column_name) FROM table_name WHERE condition;
SQL SUM() Function
The SUM() function calculates the total sum of a numeric column. It is ideal for calculating:
- Total sales
- Total revenue
- Any cumulative numeric value
Syntax:
SELECT SUM(column_name) FROM table_name WHERE condition;
Practical SQL Examples
Let us look at some examples of the COUNT(), AVG() and SUM() Function in SQL to understand them better. To demonstrate this, let us create a table "GeeksTab".
CREATE TABLE GeeksTab ( Name VARCHAR(50), City VARCHAR(50), Salary INT, ID INT, DOJ VARCHAR(50) ); INSERT INTO GeeksTab (Name, City, Salary, ID, DOJ) VALUES ('Abc', 'Delhi', 4500, 134, '6-Aug'), ('Dfe', 'Noida', 6500, 245, '4-March'), ('Def', 'Jaipur', 5400, 546, '2-July'), ('Mno', 'Noida', 7800, 432, '7-June'), ('Jkl', 'Jaipur', 5400, 768, '9-July'), ('Lmn', 'Delhi', 7800, 987, '8-June'), ('Ijk', 'Jaipur', 6700, 654, '5-June'); Table GeeksTab:
| Name | City | Salary | ID | DOJ |
|---|
| Abc | Delhi | 4500 | 134 | 6-Aug |
| Dfe | Noida | 6500 | 245 | 4-March |
| Def | Jaipur | 5400 | 546 | 2-July |
| Mno | Noida | 7800 | 432 | 7-June |
| Jkl | Jaipur | 5400 | 768 | 9-July |
| Lmn | Delhi | 7800 | 987 | 8-June |
| Ijk | Jaipur | 6700 | 654 | 5-June |
Example 1: COUNT() Function
The following SQL statement finds the number of Names in the "GeeksTab" table.
Query:
SELECT COUNT(Name) FROM GeeksTab;
Output:
7
Example 2: AVG() Function
The following SQL statement finds the average price of salary in the "GeeksTab" table.
Query:
SELECT AVG(Salary) FROM GeeksTab;
Output:
6300
Example 3: SUM() Function
The following SQL statement will find the sum of the Salary in the "GeeksTab" table.
Query:
SELECT SUM(Salary) FROM GeeksTab;
Output:
44100
Quick Facts
1. These functions ignore NULL values in calculations.
2. COUNT() counts rows, not values — unless specified with a column name.
3. Can be combined with:
- WHERE – to filter results
- GROUP BY – to group results
- HAVING – to filter aggregated results
4. Widely used in data analysis, reporting, and dashboards.
5. Always ensure data type compatibility for AVG() and SUM() (numeric columns only).
Explore
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security