SQLite SUM Function

The SQLite SUM function is used to return the summed value of an expression.

Syntax:

snippet
```SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];```

Syntax when you use SUM function with GROUP BY clause:

snippet
```SELECT expression1, expression2, ... expression_n
SUM(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;```

Example1:

We have a table named "STUDENT", having the following data:

Retrieve total fees of the students where ID is less than or equal to 5:

snippet
```SELECT SUM(FEES) AS "Total Fees"
FROM STUDENT
WHERE ID <= 5;```

Output:

Example2: Using mathematical formula with SUM function

You can use mathematical formula with SUM function.

snippet
```SELECT SUM(FEES / 12) AS "Total Monthly Fees"
FROM STUDENT;```

Output:

Example2: Using GROUP BY clause with SUM function:

Retrieve addresses from the table "STUDENT", find the sum of their corresponding fees and group by the result by address.

snippet
```SELECT ADDRESS, SUM(FEES) AS "Total Salary"
FROM STUDENT
WHERE ID <= 5