Syntax error MySQL query to calculate the total amount from column values with Cost and Quantity?

MySQL query to calculate the total amount from column values with Cost and Quantity?



Let us first create a table −

mysql> create table DemoTable
(
   Cost int,
   Quantity int
);
Query OK, 0 rows affected (0.80 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(65,2);
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values(290,4);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(40,3);
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 −

+------+----------+
| Cost | Quantity |
+------+----------+
| 65   |        2 |
| 290  |        4 |
| 40   |        3 |
+------+----------+
3 rows in set (0.00 sec)

Following is the query to calculate the total amount −

mysql> select Cost,Quantity,Cost*Quantity AS Total_Amount from DemoTable;

This will produce the following output −

+------+----------+--------------+
| Cost | Quantity | Total_Amount |
+------+----------+--------------+
|   65 |        2 |          130 |
|  290 |        4 |         1160 |
|   40 |        3 |          120 |
+------+----------+--------------+
3 rows in set (0.02 sec)
Updated on: 2019-10-01T08:37:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements