SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL Math Functions

SQL SUM Command

Let us learn about the SUM sql command and how to use it in our tables. SUM command can be applied to numeric field and the total of the value is returned. Now let us apply this SUM command to this table and find out the total mark obtain by all the students. The SUM command will add all the values of the mark field and return to us. This is our table
id name class mark
1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55
We will apply the SUM command here like this to the field  mark

SELECT sum( mark ) FROM `student`

sum(mark)
390
The command added all the values of the mark field and displayed. We can define some header  like this also.

SELECT SUM(mark) as total_mark FROM `student`

total_mark
390

Now let us find out what is the total mark in each class. Here we can use the Group By command to find out the total mark obtained by each class

SELECT class, sum( mark ) as total_mark FROM`student` GROUP BY class

classtotal_mark
Four250
Three140

Related Tutorial
• Sum record wise
• MySQL Max
• MySQL Min
• MySQL Avg
You can see above that total mark of each class is displayed. Since we have two class in our table so the sql command has returned two class with total marks in each of them. We have to use Group By clause if we ask for the query to return any other field name other than the sum. Otherwise system will generate error.

We can add condition to the sql command to get our desired result. We can add one Where clause to the query to consider records for which mark is more than some value ( say 55 )

SELECT sum( mark ) as total_mark, class FROM student where mark > 55 GROUP BY class

total_mark class
195 Four
85 Three

Other sql commands like between can be used along with this sum command to find out required results.

SQL sum in multiple columns

Number of User Comments : 7


Google+

zin el ouni16-09-2010
how Ican execute Querie union (2 table with many field and one Field numΓ©ric), with this field i want make an Operator sum
JTM11-08-2011
Hi! can someone tell me how to sum two or more rows from one table but different categories?I have a table that summarises my sales per category but i want to generate a report that summarizes all the items sold per category and return it in one single row.Please help
ketan15-01-2012
how to lisi a record from second to fifth from mysql db in php???
Rajesh13-02-2012
Excellent Tutorial.
Habib Ullah27-08-2013
Best Tutorial
julius pogi24-09-2013
SELECT ACCOUNTTITLE,sum(debit),sum(credit) FROM `FIXEDJOURNALDTL` GROUP BY ACCOUNTTITLE
amar28-09-2013
after getting the sum of column value in grid view .i want to display in text box.pls guard me...
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer