codesnout.com

SQL SUM()

Google Ads

SQL SUM( ) Function


SQL Sample Code - SQL SUM( ) Function


SELECT SUM(column_name)
FROM table_name;

See the SQL samples section below for an explanation of the SUM function.


What does the SUM() Function do?

The SQL SUM() function calculates the sum of all values in a column.

Note: The SUM() function can only be used on database columns with numeric based data-types. See our data-types section for more information.


Sample 1 – SQL SUM() Function

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this example we want to know the total SUM() for the Sales column in the tblCompany table.

Use the SELECT statement below:

SELECT SUM(Sales) AS Total_Sales FROM tblCompany;

The result will look like this:

Total_Sales
65000

Note: the AS operator is used to name the result column as ‘Total_Sales’. See the Alias section of this website to learn more about renaming database columns and tables.


Sample 2 – SQL SUM() Function using GROUP BY.

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we want to know the total SUM() for the Sales column in the tblCompany table for each Town.

Use the SELECT statement below:

SELECT Town, SUM(Sales) FROM tblCompany
GROUP BY Town;

The result will look like this:

Town SUM(Sales)
Hamburg 30000
Curry 18000
Pisa 17000

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |