SQL Query to Find the Average Value in a Column
Last Updated :
25 Mar, 2023
In this article, we are going to see how to find the average value in a column in SQL. A column in the SQL table is the vertical catalog structure. In this article, we will be using the Microsoft SQL Server as our database.
For the purpose of example, we will be creating a sample table and performing the same operations on it.
Table Definition:
We have the following car table in our database :
CREATE TABLE car (
companyid integer ,
companyname varchar(20) ,
totalmodels integer )

The above query creates a car table for us.
Adding Data to Table:
To insert values into the table we need to use the insert statement. Use the below statement to add data to the car table:
INSERT INTO car values(1,'maruti suzuki',10);
INSERT INTO car values(2,'tata',12);
INSERT INTO car values(3,'volkswagen',8);

The above query will add the data to the car table.
Note: We have to insert values according to the table created. For example, we created a car table with companyid as integer, companyname as varchar, and totalmodels as an integer. So, we need to insert an integer, a character, and an integer else we may get an error.
To verify the contents of the table use the below statement:
SELECT * FROM car;
This will show us our created table as shown below:

Average of all values in a column
For this, we need to use avg() function. We have to pass the column name as a parameter. The avg() function has the following syntax:
SELECT AVG( column_name ) FROM table_name;
- The avg() function can be used with the SELECT query for retrieving data from a table.
The below query can be used to find the average of the totalmodels column of our cars table:
SELECT AVG(totalmodels) FROM car;
Output :

Let’s verify the output. The average of 10, 12, and 18 is 10. Hence, we have successfully found out the average of all values n a column of a table.
Finding out the Average without using the AVG() function –
In this approach we will see how we can find out the average value of a column without using the AVG() function. For this purpose we will use two function SUM() and COUNT(). Firstly we will sum all of the values of a certain column and then divide it with the number of elements of that column.
Considering the table is already created and the values are being inserted.
Syntax of SUM()
SELECT SUM(Col_name) FROM Table_Name;
Syntax of COUNT()
SELECT COUNT(Col_Name) FROM Table_Name;
SQL Code –
SELECT SUM(totalmodels) / COUNT (totalmodels) AS Average FROM car;
Output –
Here in this code an alias “Average” has been used to make the output column name “Average”, if the user doesn’t want to use that then they might skip this.
Similar Reads
SQL Query to Find the Sum of all Values in a Column
In SQL, calculating the sum of values in a column is a crucial task for performing data analysis and generating reports. The SUM() function helps to calculate the total sum of numeric values from a column, which is especially useful in scenarios like finding total sales, total employees, or total re
5 min read
How to Find the Missing Number in SQL Column?
Given a column in the table having values from 1 to N, one value will be missed. The task is to find the missing number. So, let's start by creating a database first. Step 1 : Create Database. Query : CREATE DATABASE GFG Step 2 : Use the GFG Database. Query : USE GFG Step 3 : Create a table Create a
1 min read
How to Find Average Marks of Each Student in SQL?
In SQL, sometimes we need to find the average value of a column based on another column of the table such as finding the student-wise average marks scored by him/her in all the subjects. This involves the use of the GROUP BY clause along with the AGGREGATE function like AVG. The same is depicted in
2 min read
How to Find Most Frequent Value in Column in SQL?
To find the most frequent value in a column in SQL, use the COUNT() function to get a count of each unique value, sort the result in descending order, and select the first value in the final results set. In SQL, sometimes we need to find frequent values in a column. Finding the Most Frequent Value i
1 min read
How to Find Average Marks of Each Subject in SQL?
In SQL, sometimes we need to find the average value of a column based on another column of the table such as finding the subject-wise average marks scored by the students of a class. This involves the use of the GROUP BY clause along with the AGGREGATE function like AVG. The same is depicted in the
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
2 min read
How to Find the Maximum of Multiple Columns in SQL
Finding the maximum value of multiple columns is one of the most common analytical tasks essential for making decisions and analyzing data. Using the MAX() function of SQL, users can find the maximum value in a single column. But to find the maximum value in multiple columns, users need to use other
2 min read
SQL Query to find Employees With Higher Salary than Their Department Average ?
Analyzing and understanding salary distributions within departments is a critical aspect of human resource management. In this article, we will explore SQL queries to calculate the average salary for each department and identify employees whose salaries exceed their departmental averages. These quer
4 min read
SQL Query to Find all the Students with Marks Greater than Average Marks
Query in SQL is like a statement that performs a task. Here, we need to write a query to find all the students whose marks are greater than the average marks of students. We will first create a database named âgeeksâ then we will create a table âStudentsâ in that database. Create a Database: We can
2 min read
SQL COUNT(), AVG() and SUM() Function
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical analysis on data. These functions allow you to gather valuable insights from your database, such as calculating totals, and averages, and counting specific rows. In this article, weâll explain
3 min read