The SQL SUM()
function is used to calculate the sum of values in a column of a table. It is often used with the GROUP BY
clause to calculate sums for each group of rows. Here's a basic syntax:
SELECT SUM(column_name)
FROM table_name;
This will return the sum of all values in the specified column. If you want to calculate the sum for each group, you can use it with the GROUP BY
clause:
SELECT column_name, SUM(column_name)
FROM table_name
GROUP BY column_name;
This will give you the sum of values for each group of rows based on the specified column.
For example, if you have a table called sales
with columns product
and amount
, and you want to calculate the total sales amount for each product, you can use:
SELECT product, SUM(amount) AS total_sales
FROM sales
GROUP BY product;
This will give you the total sales amount for each product in the sales
table.