SQL SUM is one of SQL’s aggregate functions. It enables you to add values from selected tables and columns, which can help you perform important functions and reduce the work involved in handling complex data sets.

What is SQL SUM?

Per­form­ing cal­cu­la­tions with records is one of the most important tasks for database admins who work with SQL. That makes SQL SUM an in­dis­pens­able tool for data ag­greg­a­tions like cal­cu­lat­ing sales, de­term­in­ing trends and data patterns, and creating reports and analyses.

SQL SUM cal­cu­lates the sum of records and returns it in a result table. When combined with SQL operators, SQL commands and other aggregate functions like MIN, MAX and AVG, it can be used to carry out complex cal­cu­la­tions.

Tip

You don’t need a lot of back­ground knowledge to get started with SQL! Our SQL in­tro­duc­tion with examples helps you get a handle on the basics.

What is the syntax of SQL SUM?

The basic syntax of SQL SUM looks as follows:

SELECT  SUM(ColumnA)
FROM  TableA;
sql

It uses the following para­met­ers:

  • SELECT: SQL SELECT specifies the columns whose values you want to add up.
  • SUM: Directly follows SELECT and specifies that you want to take the sum of the indicated column. If you combine SUM(ColumnA) with AS you can name the column for the added up values in the results table.
  • WHERE: If you add an SQL WHERE clause, you can specify con­di­tions for which values will be added. SQL HAVING, SQL SELECT DISTINCT and SQL CASE can also be used to limit which values are added.

What is SUM used for?

SQL SUM is an in­dis­pens­able aggregate function in SQL. It’s used wherever values, sets and other in­form­a­tion needs to be analysed or cal­cu­lated. Some common use cases for it include:

  • Cal­cu­lat­ing and analysing sales or the per­form­ance of a company, project or campaign
  • Cal­cu­lat­ing the average value of shopping baskets, ratings, sales or con­ver­sions
  • Eval­u­at­ing trans­ac­tions, accounts or bank transfer amounts
  • Cal­cu­lat­ing pro­duc­tion volume, inventory and delivery quant­it­ies

Examples of SQL SUM

Below we’ll look at three examples that show how SQL SUM works.

Cal­cu­lat­ing inventory

In this example we’ll determine how many products are currently in stock using the columns ‘Quantity’ and ‘Pro­duct­Names’ from a table called ‘Products’. We’ll use the keyword AS to show the inventory as its own column in the target table. We’ll use a WHERE clause to specify which product you want to determine the inventory for.

The code looks as follows:

SELECT  SUM(Quantity)  AS  Inventory
FROM  Products
WHERE  ProductName  =  'ProductName1';
sql

Sales by region

In this example, we’ll calculate and group sales by city using the columns ‘Price’ and ‘City’ in a table called ‘Orders’.

The code looks as follows:

SELECT   City,  SUM(Price)  AS Sales
FROM  Orders
GROUP BY  City;
sql

De­term­in­ing total salaries by de­part­ment

In this example, we want to calculate and group the total salaries per de­part­ment. We’ll use the columns ‘Name’, ‘Salary’ and ‘De­part­ment’ from a table called ‘Employees’. The code looks as follows:

SELECT   Department,  SUM(Salary)  AS Total_salary
FROM  Employees
GROUP BY  Department;
sql

What are the al­tern­at­ives to SQL SUM?

In addition to SQL SUM, there are a few other functions for per­form­ing cal­cu­la­tions of records and numerical values:

  • AVG: SQL AVG allows you to calculate the average value of columns.
  • COUNT: SQL COUNT counts the number of records or rows in a table or column.
  • MIN und MAX: SQL MIN AND MAX determine the smallest and largest values in the selected records.
Go to Main Menu