![]() |
VOOZH | about |
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical and statistical analysis on data. They allow you to:
These functions are commonly used for data analytics and reporting, enabling deeper insights into datasets.
The COUNT() function provides the number of rows that match a specified condition. It is often used to determine:
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;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;The SUM() function calculates the total sum of a numeric column. It is ideal for calculating:
Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;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 |
The following SQL statement finds the number of Names in the "GeeksTab" table.
Query:
SELECT COUNT(Name)
FROM GeeksTab; Output:
7 The following SQL statement finds the average price of salary in the "GeeksTab" table.
Query:
SELECT AVG(Salary)
FROM GeeksTab; Output:
6300 The following SQL statement will find the sum of the Salary in the "GeeksTab" table.
Query:
SELECT SUM(Salary)
FROM GeeksTab; Output:
44100 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:
4. Widely used in data analysis, reporting, and dashboards.
5. Always ensure data type compatibility for AVG() and SUM() (numeric columns only).