SQL SUM()
The SUM() function returns the sum of numeric column. SQL SUM() ignores Null Values.
Syntax of SQL SUM()
SUM ( [ ALL | DISTINCT ] expression )
Example 1 of SQL SUM()
From the Sal column, below query will sum all the values which are less than 25000
SELECT SUM(sal) as "Total Sal"
FROM emp
WHERE sal < 25000;
Example 2 of SQL SUM()
Below is the test_table:
| ID | Number |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
| 11 | 3 |
| 12 | 3 |
| 13 | 4 |
| 14 | 4 |
| 15 | 4 |
| 16 | 4 |
| 17 | 5 |
| 18 | 5 |
| 19 | 5 |
| 20 | 6 |
| 21 | 6 |
| 22 | 6 |
| 23 | 2 |
| 24 | 2 |
| 25 | 7 |
| 26 | 7 |
| 27 | 7 |
| 28 | 5 |
| 29 | 7 |
| 30 | 5 |
| 31 | 5 |
| 32 | 2 |
| 33 | 5 |
| 34 | 2 |
| 35 | 2 |
Below are the queries run on this above table with their results:
Select Sum(all Number) from test_table
Result:
131
Select Sum(distinct Number) from test_table
Result:
28
No comments:
Post a Comment