MySQL Tutorial
The SUM() function returns the total sum of a numeric column
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | JAVA |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | JAVA |
Now lets calculate the sum of record from Students table.
SELECT SUM(ROLL_NO)
FROM Students;
The above SELECT statement calculate the sum of record of ROLL_NO.
SUM(ROLL_NO) |
---|
21 |
We can calculate the sum of record with where condition.
SELECT SUM(ROLL_NO)
FROM Students;
WHERE SUBJECT='JAVA'
The above SELECT statement calculate the sum of record SUBJECT is 'JAVA'.
SUM(ROLL_NO) |
---|
11 |