Summation Function in SQL: SUM

The SUM function in the SQL language, despite its simplicity, is used quite often when working with a database. With its help it is convenient to get some intermediate or final results without resorting to the help of auxiliary DBMS tools.

Function Syntax

In most SQL languages, the syntax sum is the same - as an argument, only the field name or some arithmetic action of several of them is used, according to which summation is required.

sql sum




In exceptional cases, it is possible to convey a specific value in the form of a number or a variable, but such "schemes" are practically not used, since they do not carry great value. The following is the syntax of a function in SQL:

sum (a) - here as a parameter a certain numerical value or expression is used

, , , DISTINCT ALL, , .

SUM SQL

. SQL SUM , , , .

, , , . :

SELECT , sum() FROM GroupBy ;

.

sql sum examples




For the second example, you need to get a list of goods, the sales amount for which exceeded a certain value, for example, 100. You can get the result for this task in several ways, the most optimal of which is the execution of one request:

SELECT Product FROM (SELECT Product, sum (Sum of Purchases) as Total FROM Sales) WHERE Total> 100.




All Articles