How to Find Average Marks of Each Subject in SQL?
Last Updated :
28 Nov, 2021
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 below article. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table of STDMARKS inside the database GeeksForGeeks. This table has 4 columns namely ROLL_NO, STUDENT_NAME, SUBJECT, and MARKS containing the roll number and name of the students and the subject name and marks scored by the student in the respective subject.
Query:
CREATE TABLE STDMARKS(
ROLL_NO INT,
STUDENT_NAME VARCHAR(10),
SUBJECT VARCHAR(10),
MARKS INT);
Output:

Step 4: Describe the structure of the table STDMARKS.
Query:
EXEC SP_COLUMNS STDMARKS;
Output:

Step 5: Insert 16 rows into the STDMARKS table.
Query:
INSERT INTO STDMARKS VALUES(1,'RAVI','C++',75);
INSERT INTO STDMARKS VALUES(1,'RAVI','ORACLE',84);
INSERT INTO STDMARKS VALUES(1,'RAVI','JAVA',95);
INSERT INTO STDMARKS VALUES(1,'RAVI','OS',62);
INSERT INTO STDMARKS VALUES(2,'VIVEK','C++',46);
INSERT INTO STDMARKS VALUES(2,'VIVEK','ORACLE',70);
INSERT INTO STDMARKS VALUES(2,'VIVEK','JAVA',55);
INSERT INTO STDMARKS VALUES(2,'VIVEK','OS',56);
INSERT INTO STDMARKS VALUES(3,'RAJ','C++',82);
INSERT INTO STDMARKS VALUES(3,'RAJ','ORACLE',84);
INSERT INTO STDMARKS VALUES(3,'RAJ','JAVA',74);
INSERT INTO STDMARKS VALUES(3,'RAJ','OS',92);
INSERT INTO STDMARKS VALUES(4,'MAHESH','C++',43);
INSERT INTO STDMARKS VALUES(4,'MAHESH','ORACLE',49);
INSERT INTO STDMARKS VALUES(4,'MAHESH','JAVA',68);
INSERT INTO STDMARKS VALUES(4,'MAHESH','OS',35);
Output:

Step 6: Display all the rows of the STDMARKS table.
Query:
SELECT * FROM STDMARKS;
Output:

Step 7: Display the SUBJECT and the average marks scored by the students in that subject from the table STDMARKS. For achieving the aggregate value of a column according to another column, we need to use the GROUP BY clause along with the aggregate function such as AVG. The column mentioned after the GROUP BY clause is the basis for our output. The subject-wise average marks are achieved in this manner.
Syntax:
SELECT COLUMN_NAME1,AVG(COLUMN_NAME2)
AS ALIAS FROM TABLE_NAME GROUP BY COLUMN_NAME1;
Query:
SELECT SUBJECT,AVG(MARKS) AS "AVERAGE MARKS"
FROM STDMARKS GROUP BY SUBJECT;
Output:

Similar Reads
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
Find Maximum Average Marks in Student Pairs
Given an array A[] containing N pairs in the form of (X, Y). Where X and Y denote the student's name and marks respectively. Then your task is to output the maximum average marks of a student. Examples: Input: A[] = {{"Bob", "87"}, {"Mike", "35"}, {"Bob", "52"}, {"Jason", "35"}, {"Mike", "55"}, {"Je
8 min read
How to calculate the average of a set of numbers?
Answer: The formula for calculating the average is: Average=Sum of all values/Number of valuesThe mean or average of the given data distribution is the calculated central value. It is used to determine the average of the given data. It gives a measure of the central tendency of the data. The central
6 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
SQL Query to Find the Average Value in a Column
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 performin
3 min read
Finding Average Salary of Each Department in SQL Server
In SQL, we need to find out the department-wise information from the given table containing information about employees. One such data is the average salary obtained by the employees in each department. We shall use the GROUP BY and AVG clause to achieve this. This is illustrated below. For this art
2 min read
How to Compute a Moving Average in PL/SQL?
A moving average is a technique we use to analyze and determine some specific trends in our provided data. Through moving averages, we can analyze and determine trends in our data along with some other benefits like noise reduction in our data. In this article, we are going to learn about "how to co
5 min read
How to find Arithmetic Mean in Central Tendency?
Answer: The following formula is used to determine the arithmetic mean Central Tendency: X = Σx/N.In layman's terms, statistics refers to the process of obtaining, classifying, examining, interpreting, and finally presenting facts in an accessible manner so that one may make an opinion and, if requi
3 min read
How to find the mean of a dataset?
Answer: Mean of a dataset is calculated using the formula:Mean = Sum of All Values / Total Number of Values Mean is a concept in mathematics that is used to find the average collection of numbers. Mean is also known as the expected value. In general, mean refers to the addition of the largest value
5 min read
How to calculate mean of a CSV file in R?
Mean or average is a method to study central tendency of any given numeric data. It can be found using the formula. [Tex]Mean= (sum of data)/(frequency of data)[/Tex] In this article, we will be discussing two different ways to calculate the mean of a CSV file in R. Data in use: Method 1: Using mean
2 min read