×

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression.

Syntax:

SELECT SUM(columnname) FROM table_name WHERE conditions; 

Consider the existing tables which have the following records:

Table: Employees

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Example 1: Write a query that sums the total salary of employees from the employees' table.

SELECT SUM (SALARY) AS 'SALARY' FROM EMPLOYEES;

The Sum expression will display the sum of the total salary. s

Output:

SALARY
742000
SQL SELECT SUM

Example 2: Write a query to sum the salary of employees whose city is Pune from the employees' table.

SELECT CITY, SUM(SALARY) AS 'SALARY'  FROM EMPLOYEES WHERE CITY = 'PUNE';

This query will make a sum of the salary of the employees whose city is Pune.

Output:

CITYSALARY
PUNE186500
SQL SELECT SUM

Example 3: Write a query to sum the employees' salary from the employees' table of employees whose department is Oracle.

SELECT DEPARTMENT, SUM(SALARY) AS 'SALARY'  FROM EMPLOYEES WHERE DEPARTMENT = 'ORACLE';

Output:

DEPARTMENTSALARY
ORACLE181000
SQL SELECT SUM

Example 4: Write a query to sum salary of employees from the employees' table whose department includes oracle and FMW.

SELECT SUM(SALARY) AS 'SALARY'  FROM EMPLOYEES WHERE DEPARTMENT IN ('ORACLE', 'FMW');

Output:

SALARY
347500
SQL SELECT SUM

Example 4: Write a query to summed salary of employees from employees’ table whose salary is greater than 50000 and city includes Pune, and Mumbai.

SELECT CITY, SUM(SALARY) AS EMPLOYEE_SALARY FROM EMPLOYEES WHERE SALARY > 50000 AND CITY IN ('PUNE', 'MUMBAI') GROUP BY CITY;

Output:

CITYEMPLOYEE_SALARY
PUNE119000
MUMBAI186500
SQL SELECT SUM

Example 5: Write a query to sum salary of employees from employees' table whose salary is greater than 50000 or city includes Oracle, FMW and group by the department.

SELECT DEPARTMENT, SUM(SALARY) AS EMPLOYEE_SALARY FROM EMPLOYEES WHERE SALARY > 50000 OR DEPARTMENT IN ('ORACLE', 'FMW') GROUP BY DEPARTMENT;

Output:

DEPARTMENTEMPLOYEE_SALARY
C#60000
FMW166500
JAVA168500
ORACLE181000
TESTING116000
SQL SELECT SUM

Example 6:  Write a query to sum the salary of employees from employee's table using unique cities of employees and group by city.

SELECT CITY, SUM(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY CITY;

Output:

CITYEMPLOYEE_SALARY
CHANDIGARH55500
DELHI115500
JAIPUR50500
MUMBAI169000
NOIDA114500
PUNE121000
SQL SELECT SUM

Example 7:  Write a query to sum the salary of employees from employee's table which have unique department and group by the department.

SELECT DEPARTMENT, SUM(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY DEPARTMENT;

Output:

DEPARTMENTEMPLOYEE_SALARY
C#110000
FMW116000
JAVA168500
ORACLE181000
TESTING116000
SQL SELECT SUM

Example 8:  Write a query to sum the salary of employees from employee's table and group by city, department.

SELECT CITY, DEPARTMENT, SUM(SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY CITY, DEPARTMENT;

Output:

CITYDEPARTMENTEMPLOYEE_SALARY
CHANDIGARHORACLE55500
DELHIORACLE60000
DELHITESTING55500
JAIPURFMW101000
MUMBAIC#50000
MUMBAIJAVA58500
MUMBAITESTING60500
NOIDAC#60000
NOIDAJAVA54500
PUNEFMW65500
PUNEJAVA55500
PUNEORACLE65500
SQL SELECT SUM

Example 9: Write a query to sum the salary of the employees from employee's table group by the city where aggregate salary is greater than 75000.

SELECT CITY, SUM(SALARY) AS SALARY FROM EMPLOYEES GROUP BY CITY HAVING SUM(SALARY) > 75000;

Output:

CITYSALARY
DELHI115500
JAIPUR101000
MUMBAI169000
NOIDA114500
PUNE186500
SQL SELECT SUM

As we can see, it has only cities that aggregate salary is greater than 75000.

Example 10: Write a query to sum an employee's salary with a laptop and group by the department.

SELECT DEPARTMENT, SUM(SALARY) AS SALARY FROM EMPLOYEES WHERE EMPLOYEEID IN (SELECT EMPLOYEEID FROM LAPTOP) GROUP BY DEPARTMENT;

Output:

DEPARTMENTSALARY
C#60000
JAVA113000
ORACLE60000
TESTING55500
SQL SELECT SUM

Example 11: Write a query to sum employees' salary with laptop and  group by the department where aggregate salary is greater than 58000.

SELECT DEPARTMENT, SUM(SALARY) AS SALARY FROM EMPLOYEES WHERE EMPLOYEEID IN (SELECT EMPLOYEEID FROM LAPTOP) GROUP BY DEPARTMENT HAVING SUM(SALARY) > 58000;

Output:

DEPARTMENTSALARY
C#60000
JAVA113000
ORACLE60000
SQL SELECT SUM

Related Topics

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.