SQL Aggregate Sum

This page discusses - SQL Aggregate Sum

SQL Aggregate Sum

SQL Aggregate Sum

     

The SQL Aggregate Sum Function is a part of Aggregate Function that is used to compute sum of the numeric field in a table.

Understand with Example

The Tutorial grasp you with the concept on 'SQL Aggregate Sum'. In this Tutorial, we create a table'Stu_Table'. The create table statement create a table 'Stu_Table' with required table attribute specified in Query.

Create Table Stu_Table

 

 

 

 

create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15), 
Stu_Class  varchar(10),sub_id varchar(2),marks varchar(3));

Insert Data into Stu_Table

The insert into add the records or rows to the table'Stu_Table'.

insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);

Stu_Table

+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1      | Komal    | 10        | 1      | 45    |
| 2      | Ajay     | 10        | 1      | 56    |
| 3      | Rakesh   | 10        | 1      | 67    |
| 1      | Komal    | 10        | 2      | 47    |
| 2      | Ajay     | 10        | 2      | 53    |
| 3      | Rakesh   | 10        | 2      | 57    |
| 1      | Komal    | 10        | 3      | 45    |
| 2      | Ajay     | 10        | 3      | 56    |
| 3      | Rakesh   | 10        | 3      | 67    |
| 1      | Komal    | 10        | 4      | 65    |
| 2      | Ajay     | 10        | 4      | 56    |
| 3      | Rakesh   | 10        | 4      | 37    |
| 1      | Komal    | 10        | 5      | 65    |
| 2      | Ajay     | 10        | 5      | 46    |
| 3      | Rakesh   | 10        | 5      | 63    |
+--------+----------+-----------+--------+-------+

Query

The given below Query display you the records from table 'Stu_Table'. The 'sum(marks)' in the select statement compute the sum of  marks. The   Group By Clause in this query returns you the group of result-set by column name'stu_id'.

select stu_id, stu_name, sum(marks) as 'total marks' 
from stu_table group by stu_id;

Result

+--------+----------+-------------+
| stu_id | stu_name | total marks |
+--------+----------+-------------+
| 1      | Komal    | 267         |
| 2      | Ajay     | 267         |
| 3      | Rakesh   | 291         |
+--------+----------+-------------+