×

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 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 Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 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 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 MONTHNAME() function

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

3 minutes read.

MySQL LOCATE() function

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

3 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 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 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 FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.