The Wayback Machine - https://web.archive.org/web/20240907071923/https://www.geeksforgeeks.org/sql-count-avg-and-sum/
Open In App

SQL COUNT(), AVG() and SUM() Function

Last Updated : 30 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

NameCitySalaryIDDOJ
AbcDelhi45001346-Aug
DfeNoida65002454-March
DefJaipur54005462-July
MnoNoida78004327-June
JklJaipur54007689-July
LmnDelhi78009878-June
IjkJaipur67006545-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


Previous Article
Next Article

Similar Reads

AVG() Function in SQL Server
AVG() : This function in SQL Server is used to return the average value of the specified expression. Features : This function is used to find the average value of the specified expression.This function comes under Numeric Functions.This function accepts only one parameter, namely expression.This function ignores NULL values. Syntax : AVG(expression
2 min read
PHP-My SQL avg() aggregate function
In this article, we are going to find the average of the column in the SQL database using PHP in Xampp server. We are taking an example of a food database to find the average cost of all the items of food. Let's discuss it one by one. Requirements - Xampp server Introduction :Here, we will see the overview of the MySQL and AVG() function. MySQL -My
4 min read
AVG() Function in SQL
SQL is an RDBMS system in which SQL functions become very essential to provide us with primary data insights. One of the most important functions is called AVG() and is particularly useful for the calculation of averages within datasets. In this, we will learn about the AVG() function, and its syntax by understanding various examples and their outp
4 min read
Abs() and Avg() Function in MS Access
1. Abs() Function : In MS Access, the abs() function returns a positive (absolute) number. In this function, either a positive number or a negative number is passed, and it returns the absolute of that number. It takes a number as a parameter and it returns the positive value of that number. Syntax : Abs(number) Example - SELECT Abs(-240.7) AS AbsN
1 min read
ABS() and AVG() function in SQLite
1. ABS() Function :ABS(X) is a math function used to get the absolute value of a number. Syntax - ABS(X) The ABS(X) function takes a numeric argument X and returns its magnitude. SELECT ABS(-6); Output - 6 If the argument X is a blob or string, the function gives 0.0 as the return value SELECT ABS('gfg'); Output - 0.0 If the argument X is NULL the
2 min read
AVG() Function in MySQL
AVG() function : This function in MySQL is used to return the average value of the specified expression. Features : This function is used to find the average value of the specified expression.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This function ignores NULL values. Syntax : AVG(express
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
COUNT() Function in SQL Server
Pre-requisites:-Aggregate function in SQL A Count() function in SQL Server used to finds the number of indexes returned from the query selected. FeaturesThis function finds the number of indexes as returned from the query selected.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This function ig
4 min read
SUM() Function in SQL Server
SUM() : This function in SQL Server is used to compute the sum of the specified group of values. Features : This function is used to compute the sum of the specified group of values.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This function ignores the null value. Syntax : SUM(expression) Pa
2 min read
Count no. of characters and words in a string in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to count the number of characters and words in the given string. Examples:
2 min read
Count odd and even digits in a number in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.Given a number and task is to find the number of odd and even digits present in the number. Examples: Inpu
2 min read
Sum and average of three numbers in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given three numbers and the task is to find out the sum and average of three numbers. Examples: Input: a
2 min read
Print all odd numbers and their sum from 1 to n in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number N, the task is to display all the odd numbers from 1 to N and their sum. Examples: Input:
1 min read
Sum of the first and last digit of a number in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number n, the task is to find the sum of its first and last digit. Examples: Input: 14598. Output
1 min read
SIN() and COS() Function in SQL Server
1. SIN() Function : The SIN() function returns the sine of a number. Syntax : SIN(number) Parameter : Required. A numeric value. number : It is a numeric value. Returns : It returns the float_expression of the sine of a number. Example-1 : When the argument holds a positive number. SELECT SIN(5); Output : -0.95892427466313845 Example-2 : When the a
1 min read
FLOOR() and CEILING() Function in SQL Server
1. FLOOR() Function : The FLOOR() function returns the largest integer value which is less than or equal to a number. Syntax : FLOOR(number) Parameter : Required. A numeric value. number : It is a numeric value. Returns : It returns the integer value. Example-1 : When the argument holds a positive number. SELECT FLOOR(21.53); Output : 21 Example-2
1 min read
Article Tags :