SQL SELECT SUM
The SELECT SUM statement is used to calculate the total sum of a numeric column. It's commonly used for financial and statistical queries, such as calculating the total sales or revenue over a period of time.
The SUM function aggregates values and returns a single sum for a specified column, often used with GROUP BY
to get sums for different categories.
Syntax
The basic syntax for SELECT SUM is:
SELECT SUM(column_name)
FROM table_name;
Example
In this example, we calculate the total amount spent on products from the Orders table.
Code Example
-- Calculate the total amount spent on products
SELECT SUM(Price) FROM Orders;
Output
Explanation
- The query calculates the total sum of the Price column in the Orders table.
- This can be helpful in summarizing total sales, revenue, or any other numeric data.