MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.