×

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

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 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.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 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 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.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 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.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.