×

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 Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

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

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

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

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

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

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

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

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

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 SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

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

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute 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 INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

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

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.