The Sum function returns the summed value of an table column or expression.
Sum syntax
select sum(expression) from table;
Sum example
COURSE_ID |
NAME |
DESCRIPTION |
DURATION |
PRICE |
1 |
SQL 1 |
SQL course for beginners |
1 week |
10 |
2 |
SQL 2 |
SQL course for advanced |
2 week |
50 |
3 |
HTML5 |
Learn HTML 5 |
1 week |
10 |
4 |
PHP |
PHP course |
4 week |
75 |
5 |
CSS |
Learn CSS |
2 week |
20 |
select sum(price) from course;
Result: 165
select sum(price*2) from course;
Result: 330
select sum(price) from course where price between 20 and 50;
Result: 70
select course_id, name, price,
sum(decode(price, 10, price*3, 20, price*4)) mysum
from course
where price < = 20
group by course_id, name, price
order by course_id;
COURSE_ID |
NAME |
PRICE |
MYSUM |
1 |
SQL 1 |
10 |
30 |
3 |
HTML5 |
10 |
30 |
5 |
CSS |
20 |
80 |