
- 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
Get the sum of a column in all MySQL rows?
Use aggregate function SUM() to get the sum of a column in al rows. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Amount int ); Query OK, 0 rows affected (0.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Amount) values(60); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Amount) values(70); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+ | Id | Amount | +----+--------+ | 1 | 50 | | 2 | 60 | | 3 | 70 | +----+--------+ 3 rows in set (0.00 sec)
Following is the query to get the sum of all rows in MySQL.
mysql> select sum(Amount) from DemoTable;
This will produce the following output −
+-------------+ | sum(Amount) | +-------------+ | 180 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- Get the count of only unique rows in a MySQL column?
- Get the sum of last 3 digits from all the values in a column with MySQL
- Get sum of column with conditions in MySQL
- Finding the sum of integers from multiple MySQL rows in same column?
- How to get values of all rows in a particular column in openpyxl in Python?
- How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
- Get all rows apart from first and last in MySQL
- How can we get all the unique rows in MySQL result set?
- Get the substring of a column in MySQL
- Get the sum of multiple row (not all) values from a MySQL table?
- Get the size of selected rows in MySQL
- MySQL query to get the character length for all the values in a column?
- Update all varchar column rows to display values before slash in MySQL?
- Sum if all rows are not null else return null in MySQL?
- Sum the values in a column in MySQL?

Advertisements