MariaDB is an open-source database that comes under the Relational DataBase Management System(RDBMS). It was bought by Oracle in 2009. MariaDB is highly Compatible with MySQL. It offers exceptional performance and scalability which is optimized for the performance and efficient handling of large data volumes.
MariaDB can run on various Operating Systems like Windows, Linux, and MacOS. In this article, we will learn about the SUM Function in detail along with their examples, and perform various queries using various Functions Like GROUP BY, HAVING, and DISTINCT Clause.
MariaDB SUM Function
In MariaDB, the SUM Function is used when we need to sum the values of an expression. The SUM() function in MariaDB is an Aggregate Function that calculates the sum of values in a specified column. The SUM Function takes a set of values and returns the sum, it ignores NULL values as the AVG() Function does.
Syntax:
SELECT SUM(expression)
FROM TABLE
WHERE [CONDITION];
Examples of MariaDB SUM Function
To understand the SUM function better, we need some tables on which we will perform various queries and operations. Here we will create a table called Employees which consist of EMPID, EMPNAME, EMPSALARY as Columns.
Syntax of Creating a Table:
CREATE TABLE Employees
( EMPID INT ,
EMPNAME varchar(30) NOT NULL,
EMPSALARY INT
);
Let's INSERT Some data into the Employees Table are:
INSERT INTO Employees (EMPID, EMPNAME, EMPSALARY) VALUES
(1, 'PRUDHVI', 1000),
(2, 'RAGHU', 2000),
(3, 'HARSHA', 3000),
(4,'vasanth',5000);
Employees TableExplanation: We have created a table using "CREATE TABLE" statement and named as Employees mentioned the fields we want the data to insert along with its corresponding datatypes.
After creation of table , we have inserted the data into fields of table using INSERT statement and make sure that the datatype of data inserting should be same with datatype mentioned in creation.
Example 1: Using SUM Function
Let's Calculate the total salary of Employees Table.
Query:
SELECT SUM(EMPSALARY)
FROM Employees;
Output:
Output Using SUM FunctionExplanation: SUM Function does arithmetic sum operation , as we have given EMPSALARY field as parameter to it adds all the values present in it and returns as an output.
Example 2: SUM Function Using DISTINCT Clause
Let's Calculate the total sum of distinct employee salaries from the Employees table.
Query:
SELECT SUM(DISTINCT EMPSALARY) as "Total Salary"
FROM Employees;
Output:
Output Using DISTINCT
Explanation: In the above Query we have used the DISTINCT Clause with the SUM Function. Here DISTINCT Clause with SUM allow the unique values to be added only. It won't allowed the duplicate values to be added in the Total_Salary Column. If we clearly saw the Employees table then we observe that all the values in EMPSALARY Column have a Unique values that's why their total sum is 11000.
Example 3: SUM Function Using GROUP BY Clause
In Employees Table which containing employee IDs (EMPID) and their corresponding salaries (EMPSALARY), calculate the total salary earned by each individual employee.
Query:
SELECT SUM(EMPSALARY)
FROM
Employees
GROUP BY EMPID;
Output:
Output for SUM( ) with group by Clause
Explanation: In the Employees Table, Since each employee has only one salary entry, the total salaries match individual salaries. The GROUP BY statement calculates the sum value for each group.i.e EMPID and displays the output. The Output displayed the total sum for each EMPID. Like sum is 1000 for EMPID 1, sum is 2000 for EMPID 2, similarly for all EMPID Fields the sum is displayed.
Example 4: SUM Function Using Having Clause
Let's find out the employees named 'vasanth' whose total salary exceeds 2000.
Query:
SELECT EMPID, SUM(EMPSALARY) AS Employeesalary
FROM Employees
WHERE EMPNAME = 'vasanth'
GROUP BY EMPID
HAVING SUM(EMPSALARY) > 2000;
Output:
Output after using SUM( ) with Having Clause
Explanation: In the above query, we have added HAVING Clause. The SELECT statement selected the EMPID column from Employees table, The SUM Function will add all values in EMPSALARY and returns its sum. The EMPID 1 belongs to an employee named 'vasanth'. and their total salary is 5000, exceeding the 2000, thus the query identifies them as matching both criteria.
Conclusion
Generally we use SUM( ) operator to retrieve addition data from data table. Most of the situations this operator is used in Data Extraction and Data Analysing. It's highly adaptable, used for basic sums, unique value sums (with DISTINCT), grouped sums (with GROUP BY), and conditional sums (with HAVING).
Similar Reads
MariaDB MIN Functions
MariaDB is a relational database language that is similar to SQL. We know that in a relational database language, the data is stored in the form of relations which are nothing but the tables. Similar to SQL which has aggregate functions such as MIN(), MAX(), AVG(), and LEAST() functions. These aggre
5 min read
MariaDB AVG Function
MariaDB is an open-source relational database management system that is a of MySQL and is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL. The MariaDB AVG function is used to calculate the average
3 min read
MariaDB COUNT Functions
MariaDB is an open-source and relational database to operates available data and displays the required value. the count, max, min, and other functions used to get particular information or count of the database data. the mariaDB count() function is used to get a COUNT of the row or available informa
4 min read
SQL SUM() Function
The SUM() function in SQL is one of the most commonly used aggregate functions. It allows us to calculate the total sum of a numeric column, making it essential for reporting and data analysis tasks. Whether we're working with sales data, financial figures, or any other numeric information, the SUM(
5 min read
SQLite SUM() Function
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. The main features of SQLite are that it is a tiny,
6 min read
MariaDB DATEDIFF() Function
Sometimes, the difference between two dates in MariaDB is achieved through the DATEDIFF() function. The DATEDIFF() function offers a straightforward solution for measuring the gap between dates, whether it's for scheduling tasks, tracking project durations, or analyzing trends over time. In this art
3 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
PL/SQL SUM() Function
The SUM() function in PL/SQL is used to calculate the sum of the numeric column. It is an aggregate function that performs calculations on a set of values and returns a single value. In this article, we will explore the syntax, usage, and examples of the PL/SQL SUM() function to help us understand i
5 min read
Python PySpark sum() Function
PySpark, the Python API for Apache Spark, is a powerful tool for big data processing and analytics. One of its essential functions is sum(), which is part of the pyspark.sql.functions module. This function allows us to compute the sum of a column's values in a DataFrame, enabling efficient data anal
3 min read
SUM() Function in SQL Server
The SUM() function in SQL Server is an essential aggregate function used to calculate the total sum of values in a numeric column. It aggregates data by summing up all values in the specified column for the rows that match the criteria of the query. In this article, We will learn about SUM() Functio
3 min read