Sum the values in a column in MySQL?

MySQLMySQLi Database

For this, use the aggregate function SUM(). Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> € int
   -> );
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(€) values(10);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(€) values(200);
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable(€) values(190);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+------+
| Id | €    |
+----+------+
| 1  | 10   |
| 2  | 200  |
| 3  | 190  |
+----+------+
3 rows in set (0.00 sec)

Following is the query to get the sum in MySQL −

mysql> select sum(€) AS Total from DemoTable;

output

+-------+
| Total |
+-------+
| 400   |
+-------+
1 row in set (0.00 sec)
raja
Published on 09-Jul-2019 12:04:28
Advertisements