SUM function in SQL
The SUM function in SQL is an aggregate function that calculates the total sum of a numeric column.
SQL Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
FROM table_name
WHERE condition;
Example: Suppose we have a table sales with the following structure:
| sale_id | employee_id | amount |
|---|---|---|
| 1 | 1 | 500 |
| 2 | 2 | 300 |
| 3 | 1 | 700 |
| 4 | 3 | 200 |
| 5 | 2 | 400 |
| 6 | 3 | 600 |
| 7 | 1 | 300 |
To find the total sales amount:
SQL
SELECT SUM(amount) AS total_sales
FROM sales;
FROM sales;
The result of the above query is shown below:
| total_sales |
|---|
| 3000 |