# SQL SUM() function

## SUM() function

The SQL AGGREGATE SUM() function returns the SUM of all selected column.

**Syntax: **

SUM ([ALL | DISTINCT] expression )

**DBMS Support : SUM() function**

DBMS |
Command |

MySQL | Supported |

PostgreSQL | Supported |

SQL Server | Supported |

Oracle | Supported |

**DB2 and Oracle Syntax :**

SUM ([ALL | DISTINCT] expression ) OVER (window_clause)

**Parameters:**

Name | Description |
---|---|

ALL | Applies to all values. |

DISTINCT | Return the SUM of unique values. |

expression | Expression made up of a single constant, variable, scalar function, or column name. The expression is an expression of the exact numeric or approximate numeric data type category, except for the bit data type. Aggregate functions and subqueries are not permitted. |

**Syntax diagram - SUM() function**

## SQL SUM() on specific column example

To get the total SUM of 'advance_amount' of the 'orders' table, the following SQL statement can be used :

Sample table: orders

**SQL Code:**

```
SELECT SUM(advance_amount)
FROM orders;
```

**Relational Algebra Expression:**

**Relational Algebra Tree:**

Output:

SUM(ADVANCE_AMOUNT) ------------------- 19450

**Pictorial Presentation: **

## SQL SUM() using multiple columns example

To get the sum of 'opening_amt' and 'receive_amt' from the 'customer' table, the following SQL statement can be used:

Sample table: customer

**SQL Code:**

```
SELECT SUM (opening_amt + receive_amt)
FROM customer;
```

**Relational Algebra Expression:**

**Relational Algebra Tree:**

Output:

SUM(OPENING_AMT+RECEIVE_AMT) ---------------------------- 353000

## SQL SUM() with where

In the following example, we have discussed usage of WHERE clause along with the SQL SUM() function to sum one or more columns against one or more conditions.

**Example: **

To get the total SUM of 'advance_amount' of the 'orders' table with the following condition -

1. 'agent_code' must be 'A003',

the following SQL statement can be used :

Sample table: orders

**SQL Code:**

```
SELECT SUM (advance_amount)
FROM orders
WHERE agent_code = 'A003';
```

**Relational Algebra Expression:**

**Relational Algebra Tree:**

Output:

SUM(ADVANCE_AMOUNT) ------------------- 1000

## SQL SUM() with COUNT()

In the following example, we have discussed the usage of SQL SUM() and SQL COUNT() together in a SQL SELECT statement. Regarding this, it should be mentioned that the SQL SUM() and SQL COUNT() both returns a single row.

**Example:**

To get data of 'cust_country',SUM of 'opening_amt' for each 'cust_country' and number of 'cust_country' from the 'customer' table with the following condition -

1. data should be a group on 'cust_country',

the following SQL statement can be used :

Sample table: customer

**SQL Code:**

```
SELECT cust_country, SUM(opening_amt),
COUNT(cust_country)
FROM customer
GROUP BY cust_country;
```

**Relational Algebra Expression:**

**Relational Algebra Tree:**

Output:

CUST_COUNTRY SUM(OPENING_AMT) COUNT(CUST_COUNTRY) -------------------- ---------------- ------------------- USA 18000 4 India 73000 10 Australia 19000 3 Canada 25000 3 UK 26000 5

Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition

Here is a slide presentation of all aggregate functions.

**Practice SQL Exercises**

- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!

**Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.**

**Previous:** COUNT Having and Group by

**Next:** SUM using GROUP BY

## SQL: Tips of the Day

**Difference between primary key and unique key**:

Primary Key:

- There can only be one primary key constraint in a table
- In some DBMS it cannot be NULL - e.g. MySQL adds NOT NULL
- Primary Key is a unique key identifier of the record

Unique Key:

- Can be more than one unique key in one table
- Unique key can have NULL values
- It can be a candidate key
- Unique key can be NULL ; multiple rows can have NULL values and therefore may not be considered "unique"

Ref : https://bit.ly/2XNgBd0

**Weekly Trends**- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises