×

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 database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

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

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 RTRIM() function

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

2 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 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 MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

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

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL POW() function

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

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 Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL RADIANS() function

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

2 minutes read.