sql
  1. sql-select-sum

SQL SELECT Statement SELECT SUM

The SQL SELECT Statement SELECT SUM is used to retrieve the sum of values from a single column in a database table. This statement is useful for finding the total value of a particular data set.

Syntax

The syntax for the SQL SELECT Statement SELECT SUM is as follows:

SELECT SUM(column_name) FROM table_name;
  • column_name: The name of the column to retrieve the sum of values from.
  • table_name: The name of the table from which to retrieve data.

Example

Consider a table named "Sales" with the following data:

Product Quantity Price
Apple 100 1.00
Orange 150 1.50
Banana 75 0.50
Mango 50 2.00

We can use the following SQL SELECT Statement SELECT SUM to find the total value of the sales:

SELECT SUM(Quantity*Price) AS Total_Sales FROM Sales;

Output

Executing the above SQL statement, the following output will be obtained:

Total_Sales
425.00

Explanation

In the above SQL SELECT Statement SELECT SUM, we have used the Quantity and Price columns and multiplied them together to get the total sales value. We have then used the SUM function to get the sum of all the resulting values.

Use

The SQL SELECT Statement SELECT SUM is commonly used to perform calculations on a data set and retrieve the resulting sum. It is frequently used in financial applications, where it is used to calculate the totals of transactions and sales figures.

Important Points

  • The column name used in the SQL SELECT Statement SELECT SUM must be numeric.
  • The resulting value is always of the same data type as the column being summed.

Summary

In conclusion, the SQL SELECT Statement SELECT SUM is used to retrieve the sum of values from a column in a database table. It is a powerful tool for performing calculations on large data sets and retrieving resulting sums.

Published on: