SQL Query to Find the Sum of all Values in a Column
Last Updated :
06 Jan, 2025
In SQL, calculating the sum of values in a column is a crucial task for performing data analysis and generating reports. The SUM() function helps to calculate the total sum of numeric values from a column, which is especially useful in scenarios like finding total sales, total employees, or total revenue.
In this article, we will explain the process of creating a table, inserting data, and using the SUM() function to find the sum of all values in a column, along with detailed examples and output explanations.
Examples of Finding the Sum of All Values in a Column
In this section, we will explore various examples of using the SUM() function to calculate the total sum of values in a column. For our demonstration, we are using a department table that stores details about different departments, including the number of employees in each department.
We will use this sample table to demonstrate how to apply the SUM() function to calculate totals across various queries. These examples will help us to understand how to effectively use SUM() for different real-world use cases.
Example 1: Using the SUM() Function to Find the Total Employees
Now, let’s use the SUM() function to calculate the total number of employees in all departments. This query helps in quickly determining the overall workforce size by adding the values in the totalemployees column.
Query:
SELECT SUM(totalemployees) AS TotalEmployees FROM department;
Output
Explanation:
In this query, the SUM() function adds up all the values in the totalemployees column (32 + 56 + 28) and returns the total count of 116 employees across all departments. This is useful for analyzing company-wide employee statistics.
Example 2: Using SUM() Function with WHERE Clause
The WHERE clause can be used with the SUM() function to calculate the sum based on specific conditions. In this example, we will find the total number of employees for departments where the number of employees is greater than 30. This query is helpful for filtering data based on specific criteria.
Query:
SELECT SUM(totalemployees) AS TotalEmployees FROM department
WHERE totalemployees > 30;
Output
Explanation:
In this query, the WHERE totalemployees > 30 condition filters out departments with fewer than 30 employees. The SUM() function then adds the values of the remaining departments, IT (32 employees) and CSE (56 employees), resulting in a total of 88 employees.
Example 3: Using SUM() Function with GROUP BY Clause
The GROUP BY clause allows us to group data based on specific columns and then apply the SUM() function to each group. When combined with the SUM() function, it calculates the total for each group separately. In this example, we will calculate the total number of employees in each department.
Query:
SELECT deptname, SUM(totalemployees) AS TotalEmployees
FROM department
GROUP BY deptname;
Output
deptname | TotalEmployees |
---|
IT | 32 |
CSE | 56 |
ECE | 28 |
Explanation:
In this query, the GROUP BY deptname groups the rows by the department name, and the SUM() function calculates the total number of employees in each group (department). Since there are three departments (IT, CSE, and ECE), the query will return the sum of employees for each department separately.
Example 4: Using SUM() Function with HAVING Clause
The HAVING clause is used in conjunction with the GROUP BY clause to filter groups based on a condition. It is similar to the WHERE clause, but whereas WHERE filters individual rows, HAVING filters groups formed by GROUP BY. In this example, we will find departments with a total employee count greater than 30.
Query:
SELECT deptname, SUM(totalemployees) AS TotalEmployees
FROM department
GROUP BY deptname
HAVING SUM(totalemployees) > 30;
Output
deptname | TotalEmployees |
---|
IT | 32 |
CSE | 56 |
Explanation:
In this query, the GROUP BY deptname groups the rows by department name, and the SUM() function calculates the total number of employees in each department. The HAVING SUM(totalemployees) > 30 condition filters out departments with fewer than 30 employees, so only the departments with more than 30 employees are returned.
Conclusion
The SUM() function in SQL is a powerful tool for calculating the total sum of values in a numeric column. Whether we need to find totals across an entire table or filter results using WHERE, GROUP BY, or HAVING clauses, the SUM() function is a go-to solution. By mastering this function, we can efficiently perform aggregate calculations and derive meaningful insights from our database. This article provided a detailed walkthrough of creating a table, inserting data, and using the SUM() function with various SQL queries and outputs to help you better understand its usage.
Similar Reads
SQL Query to Find the Average Value in a Column In this article, we are going to see how to find the average value in a column in SQL. A column in the SQL table is the vertical catalog structure. In this article, we will be using the Microsoft SQL Server as our database. For the purpose of example, we will be creating a sample table and performin
3 min read
SQL Query to Update All Columns in a Table In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
2 min read
How to Find the Missing Number in SQL Column? Given a column in the table having values from 1 to N, one value will be missed. The task is to find the missing number. So, let's start by creating a database first. Step 1 : Create Database. Query : CREATE DATABASE GFG Step 2 : Use the GFG Database. Query : USE GFG Step 3 : Create a table Create a
1 min read
How To Find the Sum of Digits in a String in SQL Server? Given a string with digits and characters. The task is to find the sum of digits in that string. So, let's start by creating a database first. Step 1: Create a Database. Query : CREATE DATABASE GFG Step 2: Use the GFG Database. Query : USE GFG Step 3 : Â a) Select each character as a row by traversin
1 min read
How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python? MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a w
2 min read
Print all odd numbers and their sum from 1 to n in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number N, the task is to display all the odd nu
1 min read