SQL SUM() Function - The Coding Shala

Home >> Learn SQL >> SQL SUM() function

SQL SUM() Function

The SQL SUM() function returns the total sum of a numeric column. 
SQL SUM() Function - The Coding Shala

SQL SUM() Syntax

The basic syntax of SQL SUM() is as follows - 

SELECT SUM(column_name)  
 FROM table_name  
 WHERE condition;

The following 'emp' table is used for the examples - 

 emp_id     emp_name     city     dept_no     salary  
 1          Akshay        Pune      101       50000  
 3          Nikhil        Pune      101       51000  
 5          Mohit         Delhi     103       40000  
 6          Shubham       Surat     105       42000  
 7          Akash         Mumbai    106       45000 

SQL SUM() Example

The following SQL query will return the total salary of all the employees using the SQL SUM() Function - 

 SQL >> select sum(salary) as 'Total Salary'   
        from emp;  
Output >>   
 Total Salary  

The following SQL query will return the total salary of employees who are from the same city using the SQL SUM() - 

SQL >> select city, sum(salary) as 'Total Salary'   
        from emp  
        group by city;  

 Output >>   
 city     Total Salary  
 Delhi       40000  
 Mumbai      45000  
 Pune       101000  
 Surat      42000 

Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.


Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala