×

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the sum function shows the summation of all values in the table. If the given table shows empty or null, then the function returns a null value. This function works on numerical data type values of the table.

Syntax

The SUM () function basic syntax shows below.

SUM(function_expression);

MySQL SUM () function syntax with table shows below.

SELECT SUM(function_expression) FROM Table_Name WHERE condition;

Description

  • The SUM() function requires the column data for addition.
  • The "function_expression" represents the table column for the SUM() function.
  • The "WHERE" clause applies conditions on the sum() function.

Prerequisite for the SUM function

  • Create a table with the required column name and its other constraints.

mysql> create table marksheet (  

    id int NOT NULL,

    name varchar(100) NOT NULL,    

    marks int,  

    birth_date date,

    phone_number bigint  

    );  

  • Insert the value in the table as per column and its data type.

mysql> insert into 

marksheet(id, name, marks, birth_date, phone_number) 

VALUES    

(1, 'Sunny', 78, '2000-07-01', 9123011122),  

(2, 'Pummy', 74, '2000-09-25', 9823013126),  

(3, 'merry', 71, '2000-04-11', 9183032125),  

(4, 'merry', 85, '2000-10-08', 7113019962),

(5, 'merry', 71, '2000-08-11', 8745232100),

(6, 'jenny', 79, '2000-01-17', 772310032);

  • You can refer to the table structure and information using the output query.
mysql> SELECT * FROM marksheet; 
MySQL SUM() function

Here, you see the five columns and six rows in the table. This table contains int, bigint, date, and varchar data type's value.

Examples of the SUM function

1) Example: The "SUM" function query uses for the summation of the single column. Here, the "SUM" function adds all data of the row and returns the output.  

mysql> SELECT SUM(marks) AS Mark  FROM marksheet;

 Output

MySQL SUM() function

This output image displays the addition of the "marks" column. The output image displays a single column and row with value.

2) Example: This example will use the "SUM" function on the multiple columns of the table. This aggregate function returns the addition of the values at the respective column.

mysql> SELECT SUM(marks) AS Mark, SUM(id) AS ID, SUM(birth_date) AS date  FROM marksheet;

Output

MySQL SUM() function

The above image shows the addition of the marks, id, and date columns. Here, the SUM function operates on the entire rows means six rows of the columns.

3) Example: Execute the sum function to show the summation of the required column using the WHERE clause. Here, the aggregation function uses marks, id, and birth_date columns. This function uses the "WHERE" clause with the "less than" condition.

mysql> SELECT SUM(marks) AS Mark, SUM(id) AS ID, SUM(birth_date) AS date  FROM marksheet WHERE id < 4;

Output

MySQL SUM() function

The above output image shows the summation of the numerical and date columns. The table uses the "WHERE" clause to operate less than the "4" value. You can show the addition of the conditional row data of the columns.

4) Example: This example explains the "SUM" function with the "GROUP BY" clause. Here, this aggregation function operates multiple columns for the addition operation. The "SUM" function uses the "GROUP BY" clause on the "marks" column.

mysql> SELECT SUM(marks) AS Mark, SUM(id) AS ID, SUM(birth_date) AS date  FROM marksheet  GROUP BY marks;

Output

MySQL SUM() function

The above output image shows the summation of the numerical columns. The table use addition operation on similar value like 71 + 71 = 142 value.

5) Example: The SUM function with the "HAVING" clause example shows below.

Execute the "SUM" function to perform the addition of the multiple columns. Here, the SUM function uses the "GROUP BY" and "HAVING" clauses. This clause works on the "marks" column of the table.

mysql> SELECT SUM(marks) AS Mark, SUM(id) AS ID, SUM(birth_date) AS date  FROM marksheet  GROUP BY marks 
HAVING SUM(marks) < 78;

Output

MySQL SUM() function

The sum function returns three columns and one row in the above output table. The marks, id, and date show numerical values. The above table shows values less than the "78" marks column.

6) Example: The SUM function with the "DISTINCT" clause example shows below. The DISTINCT clause is used to remove duplicate data.

mysql> SELECT SUM( DISTINCT marks) AS Mark, SUM( DISTINCT id) AS ID, SUM( DISTINCT birth_date) AS date  FROM marksheet  GROUP BY marks 
HAVING SUM(marks) < 75;

Output

MySQL SUM() function

The sum function returns the above output. This output shows three columns and one row. The date shows in the numerical format.

7) Example: The SUM functions with multiple clauses example shows below.

This example uses the SUM function with the DISTINCT clause. The "DISTINCT" clause works with the marks, id, and birth_date column. The "WHERE", "GROUP BY," and "HAVING" clauses works on the "marks" column.

mysql> SELECT SUM( DISTINCT marks) AS Mark, SUM( DISTINCT id) AS ID, SUM( DISTINCT birth_date) AS date  FROM marksheet  
WHERE id < 5
GROUP BY marks 
HAVING SUM(marks) < 75;

Output

MySQL SUM() function

The above output returns the unique value of the table column. This output shows the marks, id, and date column with the sum function's data. This table shows two rows and three columns of the marksheet table.

8) Example: The SUM functions with the condition example shows below.

The SUM function uses the "marks", "id," and "birth_date" columns of the table.

mysql> SELECT SUM(marks) AS Mark, SUM(id) AS ID, SUM(birth_date) AS date  FROM marksheet  
WHERE id < 5
GROUP BY marks 
HAVING SUM(marks) < 75;

Output

MySQL SUM() function

The output image shows the value of the column data. This output does not show the summation of the column data. This output shows two rows of data because of the "HAVING" condition.


Related Topics

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Character Length Function

In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples. Introduction of MySQL CHARACTER_LENGTH() function CHARACTER_LENGTH() function in MySQL is used to...

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.