SQL COUNT(), AVG() and SUM() Function
Last Updated :
30 Jun, 2024
The SQL COUNT(), AVG() and SUM() functions are the aggregate functions in SQL. They are used for mathematical analysis of the data and provide insights about the data.
Here, we cover the COUNT(), AVG(), and SUM() Functions in SQL with examples and understand their working in SQL.
SQL COUNT() Function
The COUNT() function provides the number of rows that match a specified condition. This function is particularly useful for understanding the volume of data entries and identifying trends based on countable metrics.
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 provides the total sum of a numeric column. This function is ideal for calculating totals such as sales, revenue, or any other cumulative numeric value.
Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Demo SQL Database
Let’s 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“.
MySQL
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 |
COUNT() Function Example
The following SQL statement finds the number of Names in the “GeeksTab” table.
Query:
SELECT COUNT(Name)
FROM GeeksTab;
Output:
7
AVG() Function Example
The following SQL statement finds the average price of salary in the “GeeksTab” table.
Query:
SELECT AVG(Salary)
FROM GeeksTab;
Output:
6300
SUM() Function Example
The following SQL statement will find the sum of the Salary in the “GeeksTab” table.
Query:
SELECT SUM(Salary)
FROM GeeksTab;
Output:
44100
Important Points About SQL COUNT(), AVG() and SUM() Functions
1. The SQL COUNT(), AVG(), and SUM() functions are essential aggregate functions used in SQL for data analysis and reporting.
2. The COUNT() function provides the number of rows that match a specified condition, while the AVG() function calculates the average value of a numeric column, and the SUM() function returns the total sum of a numeric column.
3. These functions are commonly used in SQL queries to perform calculations on data sets, enabling users to count rows, calculate averages, and sum values efficiently.
4. These functions can be used in combination with other SQL clauses, such as WHERE, GROUP BY, and HAVING, to perform more complex data analysis tasks
5. It’s important to consider how these functions handle NULL values, as they can affect the final results
Please Login to comment...