JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Get the sum of multiple columns in MySQL

Author:JIYIK Last Updated:2025/04/23 Views:

In this article, we will learn how to sum multiple columns in MySQL.


Sum multiple columns in MySQL

You can use aggregate functions SUM()to calculate the total value in a collection. SUM()The function calculation does not consider NULL values.

The SUM() aggregate function has the following syntax:

SELECT SUM(aggregate_expression)
FROM table_name
WHERE conditions;

The aggregate_expression parameter specifies the column or expression used to calculate the aggregate.

The table from which we wish to obtain records is specified by table_name. The FROM clause must list at least one table.

The WHERE condition is optional. To identify a record, certain requirements must be met, as described in the WHERE clause.

You must use CASE statement in MySQL query to select multiple sum columns and display them in different columns. The CASE() function in MySQL determines a value by passing the condition when any condition satisfies the provided statement; otherwise, it returns the statement in the else part.

When the condition is met, it stops reading and returns the output. Following is the syntax:

SELECT
    SUM( CASE WHEN column_name1=value_1 THEN column_name2 END ) AS alias_column_1,
    SUM( CASE WHEN column_name1=value_2 THEN column_name2 END ) AS alias_column_2,
FROM yourTableName;

Consider the following example to better understand the preceding concepts.

-- Total Bonus
SELECT
    SUM(bonus) as TotalBonus
FROM
    EmployeeBonus;
-- Total Marks
SELECT
    SUM(maths+physics+chemistry) as TotalMarks, studentId
FROM
    StudentMarks
GROUP BY
    studentId;

SELECT
    SUM(CASE WHEN subject='Maths' THEN marks END) AS 'Maths TOTAL SCORE',
    SUM(CASE WHEN subject='Physics' THEN marks END) AS 'Physics TOTAL SCORE',
    SUM(CASE WHEN subject='Chemistry' THEN marks END) AS 'Chemistry TOTAL SCORE'
FROM StudentMarks;

In the above example, we are calculating the total bonus paid by the company so far. In the second operation, our goal is to get the cumulative grade of each student across all subjects.

Run the above line of code in any browser compatible with MySQL. It will display the following result:

+------------+
| TotalBonus |
+------------+
| $123445663 |
+------------+
1 row in set (0.00 sec)

+------------+-----------+
| TotalMarks | studentId |
+------------+-----------+
| 278        |       1   |
| 256        |       2   |
| 289        |       3   |
+------------+-----------+
3 row in set (0.01 sec)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:77 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

MySQL sorts data alphabetically

Publish Date:2025/04/23 Views:129 Category:MySQL

In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t

Display the current database in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query

Check if a database exists in MySQL

Publish Date:2025/04/23 Views:179 Category:MySQL

In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve

Converting from datetime type to date-only in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:193 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial