SQL Sum | Sum Function in SQL - sql - sql tutorial - learn sql




Select SUM Query
  • The SUM function is used to calculate the total for an expression.
  • The SQL SUM function is used to return the sum of an expression in a SELECT statement.
  • SQL SUM function is used to find out the sum of a field in various records.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Syntax

  • The syntax for the SUM function is,
SELECT SUM(<expression>)
FROM "table_name";
  • <expression> can be a column name or an arithmetic operation. An arithmetic operation can include more than one column, such as ("column1" - "column2").
  • It is also possible to have one or more columns in addition to the SUM function in the SELECT statement. In those cases, these columns need to be part of the GROUP BY clause as well:
SELECT "column_name1", "column_name2", ... "column_nameN", SUM("column_nameN+1")
FROM "table_name";
GROUP BY "column_name1", "column_name2", ... "column_nameN";
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Examples

  • We use the following table for our examples.

Table Store_Information

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-08-1999
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Example 1: SUM function on a column

  • To get the sum of all sales from Store_Information, we type in,
SELECT SUM(Sales) FROM Store_Information;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

SUM(Sales)
2750
  • 2750 represents the sum of all Sales entries: 1500 + 250 + 300 + 700.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Example 2: SUM function on an arithmetic operation

  • Assume that sales tax is 10% of the sales amount, we use the following SQL statement to get the total sales tax amount:
SELECT SUM(Sales*0.1) FROM Store_Information;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

SUM(Sales*0.1)
275
  • SQL will first calculate "Sales*0.1" and then apply the SUM function to the result.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Example 3: SUM function with a GROUP BY clause

  • To get the sum of sales for each store, we type in,
SELECT Store_Name, SUM(Sales) FROM Store_Information GROUP BY Store_Name;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

Store_Name SUM(Sales)
Los Angeles 1500
San Diego 250
Boston 700
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

SQL Exercise Aggregate Function SUM

SQL Exercise Aggregate Function SUM

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to SQL Sum | Sum Function in SQL