
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to get sum of each column where every column has same number of values?
You can use aggregate function SUM() for this. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstValue int, SecondValue int, ThirdValue int ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstValue,SecondValue,ThirdValue) values(10,20,30); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstValue,SecondValue,ThirdValue) values(60,50,40); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstValue,SecondValue,ThirdValue) values(80,90,100); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(FirstValue,SecondValue,ThirdValue) values(20,10,0); Query OK, 1 row affected (0.23 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+------------+-------------+------------+ | Id | FirstValue | SecondValue | ThirdValue | +----+------------+-------------+------------+ | 1 | 10 | 20 | 30 | | 2 | 60 | 50 | 40 | | 3 | 80 | 90 | 100 | | 4 | 20 | 10 | 0 | +----+------------+-------------+------------+ 4 rows in set (0.00 sec)
Here is the query to get the sum of each column where every column has same count of values −
mysql> select sum(FirstValue),sum(SecondValue),sum(ThirdValue) from DemoTable;
This will produce the following output −
+-----------------+------------------+-----------------+ | sum(FirstValue) | sum(SecondValue) | sum(ThirdValue) | +-----------------+------------------+-----------------+ | 170 | 170 | 170 | +-----------------+------------------+-----------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to find sum of fields with same column value?
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to count number of duplicate values in a table column
- MySQL query to increment one of the column values
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- MySQL query to select column values ending with certain character/number?
- MySQL query to get first two highest column values from a table?
- Get sum of column with conditions in MySQL
- MySQL query to display the column and its values using OR in WHERE statement
- How to get the sum for every distinct value in another column in MySQL?
- Setting column values as column names in the MySQL query result?
- Get multiple count in a single MySQL query for specific column values
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to remove special characters from column values?

Advertisements