Open In App

SQL Query to Find Monthly Salary of Employee If Annual Salary is Given

Improve
Improve
Like Article
Like
Save
Share
Report

SQL stands for Structured Query Language, which used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. Similarly, when we want any data from the database then we write the query in SQL to get that data. In this article, we are talking about how we can find the monthly salary of employees if annual salary is given.

Creating database

To create a database there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,

CREATE DATABASE database_name;

For example,

CREATE DATABASE GeeksforGeeks_salary;

Using the database

To use the database there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,

USE database_name;

For example:

USE GeeksforGeeks_salary;

Add tables in the database

To create tables in a database there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,

CREATE TABLE table_name(
column1 type(size),
column2 type(size),
.
.
.
columnN type(size)
);

For example,

CREATE TABLE GFG_salary(
emp_ID INT,
emp_name VARCHAR(50),
emp_course_mentor VARCHAR(30),
emp_An_salary INT
);

To see the table use the below:

DESC GFG_salary;

If we use the Microsoft SQL server then we need to use EXEC sp_help in place of DESC. In the Microsoft SQL server, the DESC command is not an SQL command, it is used in Oracle.

Add value into the table

To add value to the table there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The command is,

INSERT INTO table_name(
value1,
value2,
value3
.
.
.

valueN);

For example, here the query will be,

INSERT INTO `GFG_salary` (`emp_ID`, `emp_name`, `emp_course_mentor`, `emp_An_salary`)
VALUES
(1, 'EmpABC', 'C++', '480000'),
(2, 'EmpDEF', 'JAVA', '540000'),
(3, 'EmpXYZ', 'DSA', '600000'),
(4, 'EmpIJK', 'Python', '650000');

Data present in the table after insertion

SELECT * FROM GFG_salary;

Now we have to find the monthly salary of employees from the table where the annual salary is given,

To find this, we have to divide the annual salary by 12 and make an alias column as Monthly Salary to view the monthly salary o each employee. And to view other details present in the table select those columns in the select statement.

SELECT emp_name, (emp_An_salary/12) AS 'Monthly Salary' ,
emp_An_Salary AS 'Annual Salary' FROM GFG_salary ;

Now round of the salary by 2 decimal point, to do that we have used round function, see below

SELECT emp_name, round(emp_An_salary/12,2) AS 'Monthly Salary' ,
emp_An_Salary AS 'Annual Salary' FROM GFG_salary

To find the monthly salary of particular employees, then use the where clause with a condition, see below,

SELECT  emp_name, round(emp_An_salary/12,2) AS 'Monthly Salary' , emp_An_Salary AS 'Annual Salary'
FROM gfg_salary
WHERE
emp_ID = 1 
OR
emp_name = 'EmpABC' ;


Last Updated : 13 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads