Divide a column to get monthly salary of employees in a MySQL Query?


Let us first create a table −

mysql> create table DemoTable1957
   (
   EmployeeId int,
   EmployeeName varchar(20),
   EmployeeSalary int
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1957 values(1,'Chris',240000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1957 values(2,'Bob',120000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1957 values(3,'David',180000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1957 values(4,'Mike',650000);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1957;

This will produce the following output −

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris        |         240000 |
|          2 | Bob          |         120000 |
|          3 | David        |         180000 |
|          4 | Mike         |         650000 |
+------------+--------------+----------------+
4 rows in set (0.00 sec)

Here is the query to get monthly salary of employee:

mysql> select EmployeeName,EmployeeSalary/12 As MonthlySalary from DemoTable1957;

This will produce the following output −

+--------------+---------------+
| EmployeeName | MonthlySalary |
+--------------+---------------+
| Chris        |    20000.0000 |
| Bob          |    10000.0000 |
| David        |    15000.0000 |
| Mike         |    54166.6667 |
+--------------+---------------+
4 rows in set (0.00 sec)

Updated on: 31-Dec-2019

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements