SQL SUM Query

SQL SUM is used to do the addition of a column’s data.

SQL query structure with SUM

SELECT SUM(column) FROM table;

For example:

Database Name = jai_school

CREATE DATABASE jai_school;

Table Name = students

CREATE TABLE students(
name VARCHAR(25),
class INT(10),
roll_no INT(15),
due_fee INT(20)
);

Insert data in the table

INSERT INTO students(name,class,roll_no,due_fee)VALUES("Ram",8,1,8000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Rahim",8,2,16000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Shyam",8,3,7000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Anita",8,4,9000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Jai",8,5,7000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Ravina",6,1,13000);
INSERT INTO students(name,class,roll_no,due_fee)VALUES("Aman",6,2,3000);

Table Image:-

table for sum query

SQL SUM query

Now, I want to calculate the total due fee of the students from all classes, so my query will be:

SELECT SUM(due_fee) FROM students;

Query result:-

sum result

SQL SUM with WHERE

The total due fee of students who are in 8 class.

SELECT SUM(due_fee) FROM students WHERE class="8";

Query result:-

sum_with_where_result

Leave a Comment