Saturday, May 20, 2017

Arithmatic Expression in MySQL-Addition,subtraction Multiplication and modulo operator

Arithmatic Expression in MySQL

Arithmatic Expression are the combination of arithmatic operator,constant numeric value and column name  in the SQL statements.
A list of arithmatic opertors are given below -
1.Addition(+)
2.Subtraction(-)
3.Multiplication(*)
4.Division(/)
5.Remainder or Modulo operator(%)

Note-
1.You can use arithmatic operator in any clause in SQL statement except FROM cluase.
2.The operator priority list is given below-
  * / + and -
 Priority Rule -Multiplication and division take the priority over Addition and Division.operators of same priority can be evaluated from left to right.
Parentheses are used to define the priority explicitly to remove the ambiguity.

Example: SQL Arithmetic Operators
1.SELECT 150+10-5*5/5;

Result:
Number of Records: 1
150+10-5*5/5
155

=======

Suppose we have the empsal table with five record and its details are given below-
Create table empsal ( id nuber , name varchar2(30),salary number(8,2));
insert into empsal(id,name, salary) values(1,'Ajay', 10000);
insert into empsal (id,name, salary) values(2,'Amit', 11000);
insert into empsal (id,name, salary) values(3,'Ashok', 7000);
insert into empsal (id,name, salary) values(4,'Aarif', 6000);
insert into empsal (id,name, salary) values(5,'Vinod', 5000);
2. verify the record
Result:
Number of Records: 5
id name salary
1 Ajay 10000
2 Amit 11000
3 Ashok 7000
4 Aarif 6000
5 Vinod 5000

SQL plus (+) operator:It is used to add two or more numbers/expressions
Example-
1.For Number computation
SELECT 150+10;

2.Write a query to find the the increment of the salary of employee by Rs 300.
SELECT Id, name , salary+300 as " Salary" from empsal;
Result:
Number of Records: 5
id name Salary
1 Ajay 10300
2 Amit 11300
3 Ashok 7300
4 Aarif 6300
5 Vinod 5300

2.SQL minus (-) operator -subtract one expression /number from another expression /number.

Example-
1.select 150-5;

Number of Records: 1
150-5
145

2.Write a query to display the Ashok monthly salary after PF deduction of Rs 1500.
 SELECT name, salary-1500 "Ashok In hand Salary" FROM empsal where id=3;

Result:
Number of Records: 1
name Ashok In hand Salary
Ashok 5500

3.SQL multiply ( * ) operator  used to multiply two or more expressions or numbers.

Example-

1.Display the Ashok's Annual salary.
SELECT Id, name , salary as "Monthly Salary", 12*salary as "Yearly Salary" from empsal

Result:
Number of Records: 1
id name Monthly Salary Yearly Salary
3 Ashok 7000         84000

4.SQL divide ( / ) operator-divide one expressions /numbers by another.
1.Write a query to share the Ashok's Annual salary among his four friends equally.
SELECT Id, name ,(12*salary)/4 as "Ashok's friend share" from empsal where id=3

Result:
Number of Records: 1
id name Ashok's friend share
3 Ashok 21000